mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-20 01:37:16 +00:00
[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:
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user