22
33namespace App \Helpers \Swagger ;
44
5+ use App \Exceptions \InternalServerException ;
56use App \Helpers \MetaFormats \FormatCache ;
67use App \Helpers \MetaFormats \MetaFormatHelper ;
78use App \V1Module \Router \MethodRoute ;
@@ -289,8 +290,8 @@ private static function annotationParameterDataToAnnotationData(
289290 string $ methodName ,
290291 HttpMethods $ httpMethod ,
291292 array $ params ,
293+ array $ statusToParamsMap ,
292294 ?string $ description ,
293- ?array $ responseParams = null ,
294295 ): AnnotationData {
295296 $ pathParams = [];
296297 $ queryParams = [];
@@ -319,8 +320,8 @@ private static function annotationParameterDataToAnnotationData(
319320 $ queryParams ,
320321 $ bodyParams ,
321322 $ fileParams ,
323+ $ statusToParamsMap ,
322324 $ description ,
323- $ responseParams ,
324325 );
325326 }
326327
@@ -350,6 +351,7 @@ public static function extractStandardAnnotationData(
350351 $ methodName ,
351352 $ httpMethod ,
352353 $ params ,
354+ [], // there are no reponse params defined in the old annotations
353355 $ description
354356 );
355357 }
@@ -379,14 +381,23 @@ public static function extractAttributeData(string $className, string $methodNam
379381 $ attributeData = array_merge ($ attributeData , FormatCache::getFieldDefinitions ($ format ));
380382 }
381383
382- // if the endpoint uses a response format , extract its parameters
383- $ responseFormat = MetaFormatHelper::extractResponseFormatFromAttribute ($ reflectionMethod );
384- $ responseParams = null ;
385- if ($ responseFormat !== null ) {
386- $ responseFieldDefinitions = FormatCache::getFieldDefinitions ($ responseFormat );
384+ // if the endpoint uses response formats , extract their parameters
385+ $ responseAttributes = MetaFormatHelper::extractResponseFormatFromAttribute ($ reflectionMethod );
386+ $ statusToParamsMap = [] ;
387+ foreach ($ responseAttributes as $ responseAttribute ) {
388+ $ responseFieldDefinitions = FormatCache::getFieldDefinitions ($ responseAttribute -> format );
387389 $ responseParams = array_map (function ($ data ) {
388390 return $ data ->toAnnotationParameterData ();
389391 }, $ responseFieldDefinitions );
392+
393+ // check if all response status codes are unique
394+ if (array_key_exists ($ responseAttribute ->statusCode , $ statusToParamsMap )) {
395+ throw new InternalServerException (
396+ "The method " . $ reflectionMethod ->name . " contains duplicate response codes. "
397+ );
398+ }
399+
400+ $ statusToParamsMap [$ responseAttribute ->statusCode ] = $ responseParams ;
390401 }
391402
392403 $ params = array_map (function ($ data ) {
@@ -399,8 +410,8 @@ public static function extractAttributeData(string $className, string $methodNam
399410 $ methodName ,
400411 $ httpMethod ,
401412 $ params ,
413+ $ statusToParamsMap ,
402414 $ description ,
403- $ responseParams ,
404415 );
405416 }
406417
0 commit comments