@@ -60,6 +60,7 @@ final class OpenApiFactory implements OpenApiFactoryInterface
60
60
use NormalizeOperationNameTrait;
61
61
62
62
public const BASE_URL = 'base_url ' ;
63
+ public const OVERRIDE_OPENAPI_RESPONSES = 'open_api_override_responses ' ;
63
64
private readonly Options $ openApiOptions ;
64
65
private readonly PaginationOptions $ paginationOptions ;
65
66
private ?RouteCollection $ routeCollection = null ;
@@ -284,37 +285,40 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
284
285
}
285
286
286
287
$ existingResponses = $ openapiOperation ?->getResponses() ?: [];
287
- // Create responses
288
- switch ($ method ) {
289
- case 'GET ' :
290
- $ successStatus = (string ) $ operation ->getStatus () ?: 200 ;
291
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s %s ' , $ resourceShortName , $ operation instanceof CollectionOperationInterface ? 'collection ' : 'resource ' ), $ openapiOperation , $ operation , $ responseMimeTypes , $ operationOutputSchemas );
292
- break ;
293
- case 'POST ' :
294
- $ successStatus = (string ) $ operation ->getStatus () ?: 201 ;
295
-
296
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s resource created ' , $ resourceShortName ), $ openapiOperation , $ operation , $ responseMimeTypes , $ operationOutputSchemas , $ resourceMetadataCollection );
297
-
298
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '400 ' , 'Invalid input ' , $ openapiOperation );
299
-
300
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '422 ' , 'Unprocessable entity ' , $ openapiOperation );
301
- break ;
302
- case 'PATCH ' :
303
- case 'PUT ' :
304
- $ successStatus = (string ) $ operation ->getStatus () ?: 200 ;
305
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s resource updated ' , $ resourceShortName ), $ openapiOperation , $ operation , $ responseMimeTypes , $ operationOutputSchemas , $ resourceMetadataCollection );
306
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '400 ' , 'Invalid input ' , $ openapiOperation );
307
- if (!isset ($ existingResponses [400 ])) {
308
- $ openapiOperation = $ openapiOperation ->withResponse (400 , new Response ('Invalid input ' ));
309
- }
310
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '422 ' , 'Unprocessable entity ' , $ openapiOperation );
311
- break ;
312
- case 'DELETE ' :
313
- $ successStatus = (string ) $ operation ->getStatus () ?: 204 ;
314
-
315
- $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s resource deleted ' , $ resourceShortName ), $ openapiOperation );
316
-
317
- break ;
288
+ $ overrideResponses = $ operation ->getExtraProperties ()[self ::OVERRIDE_OPENAPI_RESPONSES ] ?? $ this ->openApiOptions ->getOverrideResponses ();
289
+ if ($ overrideResponses || !$ existingResponses ) {
290
+ // Create responses
291
+ switch ($ method ) {
292
+ case 'GET ' :
293
+ $ successStatus = (string ) $ operation ->getStatus () ?: 200 ;
294
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s %s ' , $ resourceShortName , $ operation instanceof CollectionOperationInterface ? 'collection ' : 'resource ' ), $ openapiOperation , $ operation , $ responseMimeTypes , $ operationOutputSchemas );
295
+ break ;
296
+ case 'POST ' :
297
+ $ successStatus = (string ) $ operation ->getStatus () ?: 201 ;
298
+
299
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s resource created ' , $ resourceShortName ), $ openapiOperation , $ operation , $ responseMimeTypes , $ operationOutputSchemas , $ resourceMetadataCollection );
300
+
301
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '400 ' , 'Invalid input ' , $ openapiOperation );
302
+
303
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '422 ' , 'Unprocessable entity ' , $ openapiOperation );
304
+ break ;
305
+ case 'PATCH ' :
306
+ case 'PUT ' :
307
+ $ successStatus = (string ) $ operation ->getStatus () ?: 200 ;
308
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s resource updated ' , $ resourceShortName ), $ openapiOperation , $ operation , $ responseMimeTypes , $ operationOutputSchemas , $ resourceMetadataCollection );
309
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '400 ' , 'Invalid input ' , $ openapiOperation );
310
+ if (!isset ($ existingResponses [400 ])) {
311
+ $ openapiOperation = $ openapiOperation ->withResponse (400 , new Response ('Invalid input ' ));
312
+ }
313
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , '422 ' , 'Unprocessable entity ' , $ openapiOperation );
314
+ break ;
315
+ case 'DELETE ' :
316
+ $ successStatus = (string ) $ operation ->getStatus () ?: 204 ;
317
+
318
+ $ openapiOperation = $ this ->buildOpenApiResponse ($ existingResponses , $ successStatus , sprintf ('%s resource deleted ' , $ resourceShortName ), $ openapiOperation );
319
+
320
+ break ;
321
+ }
318
322
}
319
323
320
324
if (!$ operation instanceof CollectionOperationInterface && 'POST ' !== $ operation ->getMethod ()) {
@@ -594,7 +598,8 @@ private function getFiltersParameters(CollectionOperationInterface|HttpOperation
594
598
$ data ['openapi ' ]['explode ' ] ?? ('array ' === $ schema ['type ' ]),
595
599
$ data ['openapi ' ]['allowReserved ' ] ?? false ,
596
600
$ data ['openapi ' ]['example ' ] ?? null ,
597
- isset ($ data ['openapi ' ]['examples ' ]
601
+ isset (
602
+ $ data ['openapi ' ]['examples ' ]
598
603
) ? new \ArrayObject ($ data ['openapi ' ]['examples ' ]) : null
599
604
);
600
605
}
0 commit comments