[Php-Symfony] showcase recent features in petstore.yaml (#21286)

PR #21261 added support for endpoint with response of type text/plain
or even image/png.

This commit adds such endpoint so that:
- the way those are supported is clearer (as it is now directly visible
  in the generated sample files)
- if a future commit impacts this part of the generation it will be easier
  to assess that impact
This commit is contained in:
Guillaume Turri
2025-05-16 04:32:52 +02:00
committed by GitHub
parent 5b885cd3db
commit 6c0e7274ea
6 changed files with 530 additions and 0 deletions

View File

@@ -215,6 +215,79 @@ class PetController extends Controller
}
}
/**
* Operation downloadFile
*
* downloads an image
*
* @param Request $request The Symfony request to handle.
* @return Response The Symfony response.
*/
public function downloadFileAction(Request $request, $petId)
{
// Figure out what data format to return to the client
$produces = ['image/png'];
// Figure out what the client accepts
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
if ($responseFormat === null) {
return new Response('', 406);
}
// Handle authentication
// Read out all input parameter values into variables
// Use the default value if no value was provided
// Deserialize the input values that needs it
try {
$petId = $this->deserialize($petId, 'int', 'string');
} catch (SerializerRuntimeException $exception) {
return $this->createBadRequestResponse($exception->getMessage());
}
// Validate the input values
$asserts = [];
$asserts[] = new Assert\NotNull();
$asserts[] = new Assert\Type("int");
$response = $this->validate($petId, $asserts);
if ($response instanceof Response) {
return $response;
}
try {
$handler = $this->getApiHandler();
// Make the call to the business logic
$responseCode = 200;
$responseHeaders = [];
$result = $handler->downloadFile($petId, $responseCode, $responseHeaders);
$message = match($responseCode) {
200 => 'successful operation',
default => '',
};
return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
$responseCode,
array_merge(
$responseHeaders,
[
'Content-Type' => $responseFormat,
'X-OpenAPI-Message' => $message
]
)
);
} catch (\Throwable $fallthrough) {
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
}
}
/**
* Operation findPetsByStatus
*
@@ -465,6 +538,152 @@ class PetController extends Controller
}
}
/**
* Operation petAge
*
* Get the age of the pet
*
* @param Request $request The Symfony request to handle.
* @return Response The Symfony response.
*/
public function petAgeAction(Request $request, $petId)
{
// Figure out what data format to return to the client
$produces = ['text/plain'];
// Figure out what the client accepts
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
if ($responseFormat === null) {
return new Response('', 406);
}
// Handle authentication
// Read out all input parameter values into variables
// Use the default value if no value was provided
// Deserialize the input values that needs it
try {
$petId = $this->deserialize($petId, 'int', 'string');
} catch (SerializerRuntimeException $exception) {
return $this->createBadRequestResponse($exception->getMessage());
}
// Validate the input values
$asserts = [];
$asserts[] = new Assert\NotNull();
$asserts[] = new Assert\Type("int");
$response = $this->validate($petId, $asserts);
if ($response instanceof Response) {
return $response;
}
try {
$handler = $this->getApiHandler();
// Make the call to the business logic
$responseCode = 200;
$responseHeaders = [];
$result = $handler->petAge($petId, $responseCode, $responseHeaders);
$message = match($responseCode) {
200 => 'successful operation',
default => '',
};
return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
$responseCode,
array_merge(
$responseHeaders,
[
'Content-Type' => $responseFormat,
'X-OpenAPI-Message' => $message
]
)
);
} catch (\Throwable $fallthrough) {
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
}
}
/**
* Operation petAvailableForSale
*
* Whether the pet can currently be bought
*
* @param Request $request The Symfony request to handle.
* @return Response The Symfony response.
*/
public function petAvailableForSaleAction(Request $request, $petId)
{
// Figure out what data format to return to the client
$produces = ['text/plain'];
// Figure out what the client accepts
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
if ($responseFormat === null) {
return new Response('', 406);
}
// Handle authentication
// Read out all input parameter values into variables
// Use the default value if no value was provided
// Deserialize the input values that needs it
try {
$petId = $this->deserialize($petId, 'int', 'string');
} catch (SerializerRuntimeException $exception) {
return $this->createBadRequestResponse($exception->getMessage());
}
// Validate the input values
$asserts = [];
$asserts[] = new Assert\NotNull();
$asserts[] = new Assert\Type("int");
$response = $this->validate($petId, $asserts);
if ($response instanceof Response) {
return $response;
}
try {
$handler = $this->getApiHandler();
// Make the call to the business logic
$responseCode = 200;
$responseHeaders = [];
$result = $handler->petAvailableForSale($petId, $responseCode, $responseHeaders);
$message = match($responseCode) {
200 => 'successful operation',
default => '',
};
return new Response(
$result !== null ?$this->serialize($result, $responseFormat):'',
$responseCode,
array_merge(
$responseHeaders,
[
'Content-Type' => $responseFormat,
'X-OpenAPI-Message' => $message
]
)
);
} catch (\Throwable $fallthrough) {
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
}
}
/**
* Operation updatePet
*