@@ -90,6 +90,24 @@ public void setComponentKey(String componentKey) {
9090 */
9191 abstract Map <String , Object > buildResponsesBody (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc );
9292
93+ /**
94+ * Build request parameters
95+ *
96+ * @param apiMethodDoc API data for the method
97+ */
98+ abstract List <Map <String , Object >> buildParameters (ApiMethodDoc apiMethodDoc );
99+
100+ abstract Map <String , Object > getStringParams (ApiParam apiParam , boolean hasItems );
101+
102+
103+ /**
104+ * component schema
105+ *
106+ * @param apiSchema API schema
107+ * @return component schema Map
108+ */
109+ abstract public Map <String , Object > buildComponentsSchema (ApiSchema <ApiDoc > apiSchema );
110+
93111 protected static final Map <String , String > STRING_COMPONENT = new HashMap <>();
94112
95113 static {
@@ -281,14 +299,6 @@ public static Map<String, Object> buildExampleData(ApiMethodDoc apiMethodDoc, bo
281299
282300 }
283301
284- /**
285- * Build request parameters
286- *
287- * @param apiMethodDoc API data for the method
288- */
289- abstract List <Map <String , Object >> buildParameters (ApiMethodDoc apiMethodDoc );
290-
291- abstract Map <String , Object > getStringParams (ApiParam apiParam , boolean hasItems );
292302
293303 /**
294304 * If it is a get request or @PathVariable set the request parameters
@@ -352,18 +362,40 @@ public static Map<String, Object> buildParametersSchema(ApiReqParam header) {
352362 * @return response info
353363 */
354364 public Map <String , Object > buildResponses (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc , List <ApiExceptionStatus > apiExceptionStatuses ) {
355- Map <String , Object > response = new HashMap <>(10 );
365+ Map <String , Object > response = new LinkedHashMap <>(8 );
356366 response .put ("200" , buildResponsesBody (apiConfig , apiMethodDoc ));
367+ if (CollectionUtil .isNotEmpty (apiExceptionStatuses )) {
368+ for (ApiExceptionStatus apiExceptionStatus : apiExceptionStatuses ) {
369+ response .put (apiExceptionStatus .getStatus (), buildEexcetionResponsesBody (apiConfig , apiExceptionStatus ));
370+ }
371+ }
357372 return response ;
358373 }
359374
360- /**
361- * component schema
362- *
363- * @param apiDocs List of ApiDoc
364- * @return component schema Map
365- */
366- abstract public Map <String , Object > buildComponentsSchema (List <ApiDoc > apiDocs );
375+ public Map <String , Object > buildEexcetionResponsesBody (ApiConfig apiConfig , ApiExceptionStatus apiExceptionStatus ) {
376+ Map <String , Object > responseBody = new HashMap <>(8 );
377+ responseBody .put ("description" , apiExceptionStatus .getDesc ());
378+ Map <String , Object > content = new HashMap <>(8 );
379+ Map <String , Object > mediaTypeContent = new HashMap <>(8 );
380+ Map <String , Object > schema = new HashMap <>(8 );
381+ String responseRef = componentKey + OpenApiSchemaUtil .getClassNameFromParams (apiExceptionStatus .getExceptionResponseParams ());
382+ if (CollectionUtil .isNotEmpty (apiExceptionStatus .getExceptionResponseParams ())) {
383+ schema .put ("$ref" , responseRef );
384+ }
385+ mediaTypeContent .put ("schema" , schema );
386+ if (OPENAPI_3_COMPONENT_KRY .equals (componentKey ) && apiConfig .isResponseExample ()) {
387+ Map <String , Object > json = new HashMap <>(8 );
388+ Map <String , Object > jsonData = new HashMap <>(8 );
389+ jsonData .put ("summary" , "response example" );
390+ jsonData .put ("value" , apiExceptionStatus .getResponseUsage ());
391+ json .put ("json" , jsonData );
392+ mediaTypeContent .put ("examples" , json );
393+ }
394+ content .put ("*/*" , mediaTypeContent );
395+ responseBody .put ("content" , content );
396+ return responseBody ;
397+ }
398+
367399
368400 /**
369401 * component schema properties
@@ -483,13 +515,13 @@ private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, O
483515 * This method iterates through all API documentation entries to extract request and response parameter information,
484516 * and organizes them into OpenAPI component schemas.
485517 *
486- * @param apiDocs List of API documentation, containing multiple API method descriptions .
518+ * @param apiSchema The API documentation schema .
487519 * @return Returns a map containing all component schemas.
488520 */
489- public Map <String , Object > buildComponentData (List <ApiDoc > apiDocs ) {
521+ public Map <String , Object > buildComponentData (ApiSchema <ApiDoc > apiSchema ) {
490522 Map <String , Object > component = new HashMap <>(16 );
491523 component .put (DocGlobalConstants .DEFAULT_PRIMITIVE , STRING_COMPONENT );
492- apiDocs .forEach (
524+ apiSchema . getApiDatas () .forEach (
493525 a -> {
494526 List <ApiMethodDoc > apiMethodDocs = a .getList ();
495527 apiMethodDocs .forEach (
@@ -507,6 +539,14 @@ public Map<String, Object> buildComponentData(List<ApiDoc> apiDocs) {
507539 );
508540 }
509541 );
542+ // excption response components
543+ if (Objects .nonNull (apiSchema .getApiExceptionStatuses ())) {
544+ apiSchema .getApiExceptionStatuses ().forEach (e -> {
545+ List <ApiParam > responseParams = e .getExceptionResponseParams ();
546+ String responseSchemaName = OpenApiSchemaUtil .getClassNameFromParams (e .getExceptionResponseParams ());
547+ component .put (responseSchemaName , buildProperties (responseParams , component , true ));
548+ });
549+ }
510550 component .remove (OpenApiSchemaUtil .NO_BODY_PARAM );
511551 return component ;
512552 }
@@ -528,9 +568,4 @@ public ApiSchema<ApiDoc> getOpenApiDocs(ApiConfig config, JavaProjectBuilder pro
528568 Objects .requireNonNull (docBuildTemplate , "doc build template is null" );
529569 return docBuildTemplate .getApiData (configBuilder );
530570 }
531-
532- public static Map <String , Object > buildErrorStatusResponse (ApiConfig apiConfig , List <ApiExceptionStatus > apiExceptionStatuses ) {
533- // TODO
534- return null ;
535- }
536571}
0 commit comments