2626
2727import com .ly .doc .builder .DocBuilderTemplate ;
2828import com .ly .doc .builder .ProjectDocConfigBuilder ;
29- import com .ly .doc .constants .ComponentTypeEnum ;
30- import com .ly .doc .constants .MediaType ;
31- import com .ly .doc .constants .Methods ;
32- import com .ly .doc .constants .ParamTypeConstants ;
29+ import com .ly .doc .constants .*;
3330import com .ly .doc .factory .BuildTemplateFactory ;
3431import com .ly .doc .model .*;
3532import com .ly .doc .model .openapi .OpenApiTag ;
@@ -68,20 +65,21 @@ public void setComponentKey(String componentKey) {
6865 /**
6966 * Create OpenAPI definition
7067 *
71- * @param apiConfig Configuration of smart-doc
72- * @param apiDocList List of API DOC
68+ * @param apiConfig Configuration of smart-doc
69+ * @param apiSchema Project API schema
7370 */
74- abstract void openApiCreate (ApiConfig apiConfig , List <ApiDoc > apiDocList );
71+ abstract void openApiCreate (ApiConfig apiConfig , ApiSchema <ApiDoc > apiSchema );
7572
7673 /**
7774 * Build request
7875 *
79- * @param apiConfig Configuration of smart-doc
80- * @param apiMethodDoc Data of method
81- * @param apiDoc singe api doc
76+ * @param apiConfig Configuration of smart-doc
77+ * @param apiMethodDoc Data of method
78+ * @param apiDoc singe api doc
79+ * @param apiExceptionStatuses Exception status list
8280 * @return Map of request urls
8381 */
84- abstract Map <String , Object > buildPathUrlsRequest (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc , ApiDoc apiDoc );
82+ abstract Map <String , Object > buildPathUrlsRequest (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc , ApiDoc apiDoc , List < ApiExceptionStatus > apiExceptionStatuses );
8583
8684 /**
8785 * response body
@@ -102,19 +100,19 @@ public void setComponentKey(String componentKey) {
102100 /**
103101 * Build openapi paths
104102 *
105- * @param apiConfig Configuration of smart-doc
106- * @param apiDocList List of API DOC
107- * @param tags tags
103+ * @param apiConfig Configuration of smart-doc
104+ * @param apiSchema Project API schema
105+ * @param tags tags
108106 * @return Map of paths
109107 */
110- public Map <String , Object > buildPaths (ApiConfig apiConfig , List <ApiDoc > apiDocList , Set <OpenApiTag > tags ) {
108+ public Map <String , Object > buildPaths (ApiConfig apiConfig , ApiSchema <ApiDoc > apiSchema , Set <OpenApiTag > tags ) {
111109 Map <String , Object > pathMap = new HashMap <>(500 );
112110 Set <ApiMethodDoc > methodDocs = DocMapping .METHOD_DOCS ;
113111 for (ApiMethodDoc methodDoc : methodDocs ) {
114112 String [] paths = methodDoc .getPath ().split (";" );
115113 for (String path : paths ) {
116114 path = path .trim ();
117- Map <String , Object > request = buildPathUrls (apiConfig , methodDoc , methodDoc .getClazzDoc ());
115+ Map <String , Object > request = buildPathUrls (apiConfig , methodDoc , methodDoc .getClazzDoc (), apiSchema . getApiExceptionStatuses () );
118116 if (!pathMap .containsKey (path )) {
119117 pathMap .put (path , request );
120118 } else {
@@ -142,9 +140,9 @@ public Map<String, Object> buildPaths(ApiConfig apiConfig, List<ApiDoc> apiDocLi
142140 * @param apiDoc ApiDoc
143141 * @return Map of path urls
144142 */
145- public Map <String , Object > buildPathUrls (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc , ApiDoc apiDoc ) {
143+ public Map <String , Object > buildPathUrls (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc , ApiDoc apiDoc , List < ApiExceptionStatus > apiExceptionStatuses ) {
146144 Map <String , Object > request = new HashMap <>(4 );
147- request .put (apiMethodDoc .getType ().toLowerCase (), buildPathUrlsRequest (apiConfig , apiMethodDoc , apiDoc ));
145+ request .put (apiMethodDoc .getType ().toLowerCase (), buildPathUrlsRequest (apiConfig , apiMethodDoc , apiDoc , apiExceptionStatuses ));
148146 return request ;
149147 }
150148
@@ -348,11 +346,12 @@ public static Map<String, Object> buildParametersSchema(ApiReqParam header) {
348346 /**
349347 * build response
350348 *
351- * @param apiMethodDoc ApiMethodDoc
352- * @param apiConfig ApiConfig
349+ * @param apiMethodDoc ApiMethodDoc
350+ * @param apiConfig ApiConfig
351+ * @param apiExceptionStatuses apiExceptionStatuses
353352 * @return response info
354353 */
355- public Map <String , Object > buildResponses (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc ) {
354+ public Map <String , Object > buildResponses (ApiConfig apiConfig , ApiMethodDoc apiMethodDoc , List < ApiExceptionStatus > apiExceptionStatuses ) {
356355 Map <String , Object > response = new HashMap <>(10 );
357356 response .put ("200" , buildResponsesBody (apiConfig , apiMethodDoc ));
358357 return response ;
@@ -457,22 +456,19 @@ private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, O
457456 propertiesData .put ("format" , "binary" );
458457 }
459458 if ("object" .equals (apiParam .getType ())) {
459+ propertiesData .put ("description" , apiParam .getDesc () + "(object)" );
460460 if (CollectionUtil .isNotEmpty (apiParam .getChildren ())) {
461- propertiesData .put ("type" , "object" );
462- propertiesData .put ("description" , apiParam .getDesc () + "(object)" );
463461 if (!apiParam .isSelfReferenceLoop ()) {
464462 String childSchemaName = OpenApiSchemaUtil .getClassNameFromParams (apiParam .getChildren ());
465463 if (childSchemaName .contains (OpenApiSchemaUtil .NO_BODY_PARAM )) {
466464 propertiesData .put ("type" , "object" );
467- propertiesData .put ("description" , apiParam .getDesc () + "(object)" );
468465 } else {
469466 component .put (childSchemaName , buildProperties (apiParam .getChildren (), component , isResp ));
470467 propertiesData .put ("$ref" , componentKey + childSchemaName );
471468 }
472469 }
473470 } else {
474471 propertiesData .put ("type" , "object" );
475- propertiesData .put ("description" , apiParam .getDesc () + "(object)" );
476472 }
477473 }
478474 if (apiParam .getExtensions () != null && !apiParam .getExtensions ().isEmpty ()) {
@@ -482,14 +478,47 @@ private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, O
482478 return propertiesData ;
483479 }
484480
481+ /**
482+ * Builds component data for API documentation.
483+ * This method iterates through all API documentation entries to extract request and response parameter information,
484+ * and organizes them into OpenAPI component schemas.
485+ *
486+ * @param apiDocs List of API documentation, containing multiple API method descriptions.
487+ * @return Returns a map containing all component schemas.
488+ */
489+ public Map <String , Object > buildComponentData (List <ApiDoc > apiDocs ) {
490+ Map <String , Object > component = new HashMap <>(16 );
491+ component .put (DocGlobalConstants .DEFAULT_PRIMITIVE , STRING_COMPONENT );
492+ apiDocs .forEach (
493+ a -> {
494+ List <ApiMethodDoc > apiMethodDocs = a .getList ();
495+ apiMethodDocs .forEach (
496+ method -> {
497+ //request components
498+ String requestSchema = OpenApiSchemaUtil .getClassNameFromParams (method .getRequestParams ());
499+ List <ApiParam > requestParams = method .getRequestParams ();
500+ Map <String , Object > prop = buildProperties (requestParams , component , false );
501+ component .put (requestSchema , prop );
502+ //response components
503+ List <ApiParam > responseParams = method .getResponseParams ();
504+ String responseSchemaName = OpenApiSchemaUtil .getClassNameFromParams (method .getResponseParams ());
505+ component .put (responseSchemaName , buildProperties (responseParams , component , true ));
506+ }
507+ );
508+ }
509+ );
510+ component .remove (OpenApiSchemaUtil .NO_BODY_PARAM );
511+ return component ;
512+ }
513+
485514 /**
486515 * Get a list of OpenAPI's document data
487516 *
488517 * @param config Configuration of smart-doc
489518 * @param projectBuilder JavaDocBuilder of QDox
490519 * @return List of OpenAPI's document data
491520 */
492- public List <ApiDoc > getOpenApiDocs (ApiConfig config , JavaProjectBuilder projectBuilder ) {
521+ public ApiSchema <ApiDoc > getOpenApiDocs (ApiConfig config , JavaProjectBuilder projectBuilder ) {
493522 config .setShowJavaType (false );
494523 DocBuilderTemplate builderTemplate = new DocBuilderTemplate ();
495524 builderTemplate .checkAndInit (config , Boolean .TRUE );
@@ -499,4 +528,9 @@ public List<ApiDoc> getOpenApiDocs(ApiConfig config, JavaProjectBuilder projectB
499528 Objects .requireNonNull (docBuildTemplate , "doc build template is null" );
500529 return docBuildTemplate .getApiData (configBuilder );
501530 }
531+
532+ public static Map <String , Object > buildErrorStatusResponse (ApiConfig apiConfig , List <ApiExceptionStatus > apiExceptionStatuses ) {
533+ // TODO
534+ return null ;
535+ }
502536}
0 commit comments