Skip to content

Commit 6c0e727

Browse files
authored
[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
1 parent 5b885cd commit 6c0e727

File tree

6 files changed

+530
-0
lines changed

6 files changed

+530
-0
lines changed

modules/openapi-generator/src/test/resources/3_0/php-symfony/petstore.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,74 @@ paths:
283283
description: file to upload
284284
type: string
285285
format: binary
286+
'/pet/{petId}/downloadImage':
287+
get:
288+
tags:
289+
- pet
290+
summary: downloads an image
291+
description: response may be an image
292+
operationId: downloadFile
293+
parameters:
294+
- name: petId
295+
in: path
296+
description: ID of pet to download an image from
297+
required: true
298+
schema:
299+
type: integer
300+
format: int64
301+
responses:
302+
'200':
303+
description: successful operation
304+
content:
305+
image/png:
306+
schema:
307+
type: string
308+
format: binary
309+
'/pet/{petId}/age':
310+
get:
311+
tags:
312+
- pet
313+
summary: Get the age of the pet
314+
description: response may be an int
315+
operationId: petAge
316+
parameters:
317+
- name: petId
318+
in: path
319+
description: ID of pet
320+
required: true
321+
schema:
322+
type: integer
323+
format: int64
324+
responses:
325+
'200':
326+
description: successful operation
327+
content:
328+
text/plain:
329+
schema:
330+
type: integer
331+
format: int64
332+
'/pet/{petId}/available-for-sale':
333+
get:
334+
tags:
335+
- pet
336+
summary: Whether the pet can currently be bought
337+
description: response may be a boolean
338+
operationId: petAvailableForSale
339+
parameters:
340+
- name: petId
341+
in: path
342+
description: ID of pet
343+
required: true
344+
schema:
345+
type: integer
346+
format: int64
347+
responses:
348+
'200':
349+
description: successful operation
350+
content:
351+
text/plain:
352+
schema:
353+
type: boolean
286354
/store/inventory:
287355
get:
288356
tags:

samples/server/petstore/php-symfony/SymfonyBundle-php/Api/PetApiInterface.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ public function deletePet(
9898
array &$responseHeaders
9999
): void;
100100

101+
/**
102+
* Operation downloadFile
103+
*
104+
* downloads an image
105+
*
106+
* @param int $petId ID of pet to download an image from (required)
107+
* @param int &$responseCode The HTTP Response Code
108+
* @param array $responseHeaders Additional HTTP headers to return with the response ()
109+
*
110+
* @return mixed
111+
*/
112+
public function downloadFile(
113+
int $petId,
114+
int &$responseCode,
115+
array &$responseHeaders
116+
): mixed;
117+
101118
/**
102119
* Operation findPetsByStatus
103120
*
@@ -150,6 +167,40 @@ public function getPetById(
150167
array &$responseHeaders
151168
): array|object|null;
152169

170+
/**
171+
* Operation petAge
172+
*
173+
* Get the age of the pet
174+
*
175+
* @param int $petId ID of pet (required)
176+
* @param int &$responseCode The HTTP Response Code
177+
* @param array $responseHeaders Additional HTTP headers to return with the response ()
178+
*
179+
* @return int
180+
*/
181+
public function petAge(
182+
int $petId,
183+
int &$responseCode,
184+
array &$responseHeaders
185+
): int;
186+
187+
/**
188+
* Operation petAvailableForSale
189+
*
190+
* Whether the pet can currently be bought
191+
*
192+
* @param int $petId ID of pet (required)
193+
* @param int &$responseCode The HTTP Response Code
194+
* @param array $responseHeaders Additional HTTP headers to return with the response ()
195+
*
196+
* @return bool
197+
*/
198+
public function petAvailableForSale(
199+
int $petId,
200+
int &$responseCode,
201+
array &$responseHeaders
202+
): bool;
203+
153204
/**
154205
* Operation updatePet
155206
*

samples/server/petstore/php-symfony/SymfonyBundle-php/Controller/PetController.php

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,79 @@ public function deletePetAction(Request $request, $petId)
215215
}
216216
}
217217

218+
/**
219+
* Operation downloadFile
220+
*
221+
* downloads an image
222+
*
223+
* @param Request $request The Symfony request to handle.
224+
* @return Response The Symfony response.
225+
*/
226+
public function downloadFileAction(Request $request, $petId)
227+
{
228+
// Figure out what data format to return to the client
229+
$produces = ['image/png'];
230+
// Figure out what the client accepts
231+
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
232+
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
233+
if ($responseFormat === null) {
234+
return new Response('', 406);
235+
}
236+
237+
// Handle authentication
238+
239+
// Read out all input parameter values into variables
240+
241+
// Use the default value if no value was provided
242+
243+
// Deserialize the input values that needs it
244+
try {
245+
$petId = $this->deserialize($petId, 'int', 'string');
246+
} catch (SerializerRuntimeException $exception) {
247+
return $this->createBadRequestResponse($exception->getMessage());
248+
}
249+
250+
// Validate the input values
251+
$asserts = [];
252+
$asserts[] = new Assert\NotNull();
253+
$asserts[] = new Assert\Type("int");
254+
$response = $this->validate($petId, $asserts);
255+
if ($response instanceof Response) {
256+
return $response;
257+
}
258+
259+
260+
try {
261+
$handler = $this->getApiHandler();
262+
263+
264+
// Make the call to the business logic
265+
$responseCode = 200;
266+
$responseHeaders = [];
267+
268+
$result = $handler->downloadFile($petId, $responseCode, $responseHeaders);
269+
270+
$message = match($responseCode) {
271+
200 => 'successful operation',
272+
default => '',
273+
};
274+
275+
return new Response(
276+
$result !== null ?$this->serialize($result, $responseFormat):'',
277+
$responseCode,
278+
array_merge(
279+
$responseHeaders,
280+
[
281+
'Content-Type' => $responseFormat,
282+
'X-OpenAPI-Message' => $message
283+
]
284+
)
285+
);
286+
} catch (\Throwable $fallthrough) {
287+
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
288+
}
289+
}
290+
218291
/**
219292
* Operation findPetsByStatus
220293
*
@@ -465,6 +538,152 @@ public function getPetByIdAction(Request $request, $petId)
465538
}
466539
}
467540

541+
/**
542+
* Operation petAge
543+
*
544+
* Get the age of the pet
545+
*
546+
* @param Request $request The Symfony request to handle.
547+
* @return Response The Symfony response.
548+
*/
549+
public function petAgeAction(Request $request, $petId)
550+
{
551+
// Figure out what data format to return to the client
552+
$produces = ['text/plain'];
553+
// Figure out what the client accepts
554+
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
555+
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
556+
if ($responseFormat === null) {
557+
return new Response('', 406);
558+
}
559+
560+
// Handle authentication
561+
562+
// Read out all input parameter values into variables
563+
564+
// Use the default value if no value was provided
565+
566+
// Deserialize the input values that needs it
567+
try {
568+
$petId = $this->deserialize($petId, 'int', 'string');
569+
} catch (SerializerRuntimeException $exception) {
570+
return $this->createBadRequestResponse($exception->getMessage());
571+
}
572+
573+
// Validate the input values
574+
$asserts = [];
575+
$asserts[] = new Assert\NotNull();
576+
$asserts[] = new Assert\Type("int");
577+
$response = $this->validate($petId, $asserts);
578+
if ($response instanceof Response) {
579+
return $response;
580+
}
581+
582+
583+
try {
584+
$handler = $this->getApiHandler();
585+
586+
587+
// Make the call to the business logic
588+
$responseCode = 200;
589+
$responseHeaders = [];
590+
591+
$result = $handler->petAge($petId, $responseCode, $responseHeaders);
592+
593+
$message = match($responseCode) {
594+
200 => 'successful operation',
595+
default => '',
596+
};
597+
598+
return new Response(
599+
$result !== null ?$this->serialize($result, $responseFormat):'',
600+
$responseCode,
601+
array_merge(
602+
$responseHeaders,
603+
[
604+
'Content-Type' => $responseFormat,
605+
'X-OpenAPI-Message' => $message
606+
]
607+
)
608+
);
609+
} catch (\Throwable $fallthrough) {
610+
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
611+
}
612+
}
613+
614+
/**
615+
* Operation petAvailableForSale
616+
*
617+
* Whether the pet can currently be bought
618+
*
619+
* @param Request $request The Symfony request to handle.
620+
* @return Response The Symfony response.
621+
*/
622+
public function petAvailableForSaleAction(Request $request, $petId)
623+
{
624+
// Figure out what data format to return to the client
625+
$produces = ['text/plain'];
626+
// Figure out what the client accepts
627+
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
628+
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
629+
if ($responseFormat === null) {
630+
return new Response('', 406);
631+
}
632+
633+
// Handle authentication
634+
635+
// Read out all input parameter values into variables
636+
637+
// Use the default value if no value was provided
638+
639+
// Deserialize the input values that needs it
640+
try {
641+
$petId = $this->deserialize($petId, 'int', 'string');
642+
} catch (SerializerRuntimeException $exception) {
643+
return $this->createBadRequestResponse($exception->getMessage());
644+
}
645+
646+
// Validate the input values
647+
$asserts = [];
648+
$asserts[] = new Assert\NotNull();
649+
$asserts[] = new Assert\Type("int");
650+
$response = $this->validate($petId, $asserts);
651+
if ($response instanceof Response) {
652+
return $response;
653+
}
654+
655+
656+
try {
657+
$handler = $this->getApiHandler();
658+
659+
660+
// Make the call to the business logic
661+
$responseCode = 200;
662+
$responseHeaders = [];
663+
664+
$result = $handler->petAvailableForSale($petId, $responseCode, $responseHeaders);
665+
666+
$message = match($responseCode) {
667+
200 => 'successful operation',
668+
default => '',
669+
};
670+
671+
return new Response(
672+
$result !== null ?$this->serialize($result, $responseFormat):'',
673+
$responseCode,
674+
array_merge(
675+
$responseHeaders,
676+
[
677+
'Content-Type' => $responseFormat,
678+
'X-OpenAPI-Message' => $message
679+
]
680+
)
681+
);
682+
} catch (\Throwable $fallthrough) {
683+
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
684+
}
685+
}
686+
468687
/**
469688
* Operation updatePet
470689
*

samples/server/petstore/php-symfony/SymfonyBundle-php/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,12 @@ Class | Method | HTTP request | Description
120120
------------ | ------------- | ------------- | -------------
121121
*PetApiInterface* | [**addPet**](docs/Api/PetApiInterface.md#addpet) | **POST** /pet | Add a new pet to the store
122122
*PetApiInterface* | [**deletePet**](docs/Api/PetApiInterface.md#deletepet) | **DELETE** /pet/{petId} | Deletes a pet
123+
*PetApiInterface* | [**downloadFile**](docs/Api/PetApiInterface.md#downloadfile) | **GET** /pet/{petId}/downloadImage | downloads an image
123124
*PetApiInterface* | [**findPetsByStatus**](docs/Api/PetApiInterface.md#findpetsbystatus) | **GET** /pet/findByStatus | Finds Pets by status
124125
*PetApiInterface* | [**findPetsByTags**](docs/Api/PetApiInterface.md#findpetsbytags) | **GET** /pet/findByTags | Finds Pets by tags
125126
*PetApiInterface* | [**getPetById**](docs/Api/PetApiInterface.md#getpetbyid) | **GET** /pet/{petId} | Find pet by ID
127+
*PetApiInterface* | [**petAge**](docs/Api/PetApiInterface.md#petage) | **GET** /pet/{petId}/age | Get the age of the pet
128+
*PetApiInterface* | [**petAvailableForSale**](docs/Api/PetApiInterface.md#petavailableforsale) | **GET** /pet/{petId}/available-for-sale | Whether the pet can currently be bought
126129
*PetApiInterface* | [**updatePet**](docs/Api/PetApiInterface.md#updatepet) | **PUT** /pet | Update an existing pet
127130
*PetApiInterface* | [**updatePetWithForm**](docs/Api/PetApiInterface.md#updatepetwithform) | **POST** /pet/{petId} | Updates a pet in the store with form data
128131
*PetApiInterface* | [**uploadFile**](docs/Api/PetApiInterface.md#uploadfile) | **POST** /pet/{petId}/uploadImage | uploads an image

0 commit comments

Comments
 (0)