Skip to content

Commit

Permalink
Merge pull request #418 from netdied/master
Browse files Browse the repository at this point in the history
fix #415
  • Loading branch information
shalousun authored Feb 3, 2023
2 parents 72e5b73 + d826f57 commit 1961f51
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
@SuppressWarnings("all")
public abstract class AbstractOpenApiBuilder {

private String componentKey;

public String getComponentKey() {
return componentKey;
}

public void setComponentKey(String componentKey) {
this.componentKey = componentKey;
}

abstract String getModuleName();

/**
* Create OpenAPI definition
*
Expand Down Expand Up @@ -100,22 +112,22 @@ public abstract class AbstractOpenApiBuilder {
public Map<String, Object> buildPaths(ApiConfig apiConfig, List<ApiDoc> apiDocList, Set<OpenApiTag> tags) {
Map<String, Object> pathMap = new HashMap<>(500);
apiDocList.forEach(
a -> {
tags.add(OpenApiTag.of(a.getDesc(), a.getDesc()));
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
String url = method.getPath().replace("//", "/");
Map<String, Object> request = buildPathUrls(apiConfig, method, a);
if (!pathMap.containsKey(url)) {
pathMap.put(url, request);
} else {
Map<String, Object> oldRequest = (Map<String, Object>) pathMap.get(url);
oldRequest.putAll(request);
}
}
);
}
a -> {
tags.add(OpenApiTag.of(a.getDesc(), a.getDesc()));
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
String url = method.getPath().replace("//", "/");
Map<String, Object> request = buildPathUrls(apiConfig, method, a);
if (!pathMap.containsKey(url)) {
pathMap.put(url, request);
} else {
Map<String, Object> oldRequest = (Map<String, Object>) pathMap.get(url);
oldRequest.putAll(request);
}
}
);
}
);
return pathMap;
}
Expand All @@ -140,13 +152,13 @@ public Map<String, Object> buildPathUrls(ApiConfig apiConfig, ApiMethodDoc apiMe
* @param apiMethodDoc ApiMethodDoc
* @param isRep is response
*/
public static Map<String, Object> buildContent(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc, boolean isRep, String componentKey) {
public Map<String, Object> buildContent(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc, boolean isRep) {
Map<String, Object> content = new HashMap<>(8);
String contentType = apiMethodDoc.getContentType();
if (isRep) {
contentType = "*/*";
}
content.put(contentType, buildContentBody(apiConfig, apiMethodDoc, isRep, componentKey));
content.put(contentType, buildContentBody(apiConfig, apiMethodDoc, isRep));
return content;

}
Expand All @@ -158,19 +170,20 @@ public static Map<String, Object> buildContent(ApiConfig apiConfig, ApiMethodDoc
* @param apiMethodDoc ApiMethodDoc
* @param isRep is response
*/
public static Map<String, Object> buildContentBody(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc, boolean isRep, String componentKey) {
public Map<String, Object> buildContentBody(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc, boolean isRep) {
Map<String, Object> content = new HashMap<>(8);
if(OPENAPI_2_COMPONENT_KRY.equals(componentKey) && !isRep){
content.put("name", apiMethodDoc.getName());
}
if (Objects.nonNull(apiMethodDoc.getReturnSchema()) && isRep) {
content.put("schema", apiMethodDoc.getReturnSchema());
} else if (!isRep && Objects.nonNull(apiMethodDoc.getRequestSchema())) {
content.put("schema", apiMethodDoc.getRequestSchema());
} else {
content.put("schema", buildBodySchema(apiMethodDoc, isRep, componentKey));
content.put("schema", buildBodySchema(apiMethodDoc, isRep));
}

if (OPENAPI_2_COMPONENT_KRY.equals(componentKey) && !isRep) {
content.put("name", apiMethodDoc.getName());
}
if (OPENAPI_3_COMPONENT_KRY.equals (componentKey) &&
if (OPENAPI_3_COMPONENT_KRY.equals(componentKey) &&
(!isRep && apiConfig.isRequestExample() || (isRep && apiConfig.isResponseExample()))) {
content.put("examples", buildBodyExample(apiMethodDoc, isRep));
}
Expand All @@ -184,17 +197,17 @@ public static Map<String, Object> buildContentBody(ApiConfig apiConfig, ApiMetho
* @param apiMethodDoc ApiMethodDoc
* @param isRep is response
*/
public static Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean isRep, String componentsKey) {
public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean isRep) {
Map<String, Object> schema = new HashMap<>(10);
Map<String, Object> innerScheme = new HashMap<>(10);
String requestRef;
if (apiMethodDoc.getContentType().equals(DocGlobalConstants.URL_CONTENT_TYPE)) {
requestRef = componentsKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getQueryParams());
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getQueryParams(), COMPONENT_REQUEST_SUFFIX);
} else {
requestRef = componentsKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getRequestParams());
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getRequestParams(), COMPONENT_REQUEST_SUFFIX);
}
//remove special characters in url
String responseRef = componentsKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getResponseParams());
String responseRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getResponseParams(), COMPONENT_RESPONSE_SUFFIX);

//List param
if (apiMethodDoc.getIsRequestArray() == 1) {
Expand Down Expand Up @@ -239,8 +252,8 @@ public static Map<String, Object> buildExampleData(ApiMethodDoc apiMethodDoc, bo
content.put("summary", "test data");
if (!isRep) {
content.put("value", StringUtil.isEmpty(
apiMethodDoc.getRequestExample().getJsonBody()) ? apiMethodDoc.getRequestExample().getExampleBody() :
apiMethodDoc.getRequestExample().getJsonBody());
apiMethodDoc.getRequestExample().getJsonBody()) ? apiMethodDoc.getRequestExample().getExampleBody() :
apiMethodDoc.getRequestExample().getJsonBody());
} else {
content.put("value", apiMethodDoc.getResponseUsage());
}
Expand Down Expand Up @@ -322,14 +335,14 @@ public Map<String, Object> buildResponses(ApiConfig apiConfig, ApiMethodDoc apiM
*
* @param apiDocs List of ApiDoc
*/
abstract public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs);
abstract public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs);

/**
* component schema properties
*
* @param apiParam list of ApiParam
*/
public static Map<String, Object> buildProperties(List<ApiParam> apiParam, Map<String, Object> component,String commpentKey) {
public Map<String, Object> buildProperties(List<ApiParam> apiParam, Map<String, Object> component, boolean isResp) {
Map<String, Object> properties = new HashMap<>();
Map<String, Object> propertiesData = new LinkedHashMap<>();
List<String> requiredList = new ArrayList<>();
Expand All @@ -346,7 +359,7 @@ public static Map<String, Object> buildProperties(List<ApiParam> apiParam, Map<S
continue;
}
String field = param.getField();
propertiesData.put(field, buildPropertiesData(param, component, commpentKey));
propertiesData.put(field, buildPropertiesData(param, component, isResp));
}
if (!propertiesData.isEmpty()) {
properties.put("properties", propertiesData);
Expand All @@ -366,12 +379,12 @@ public static Map<String, Object> buildProperties(List<ApiParam> apiParam, Map<S
*
* @param apiParam ApiParam
*/
private static Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, Object> component, String componentKey) {
private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, Object> component, boolean isResp) {
Map<String, Object> propertiesData = new HashMap<>();
String openApiType = DocUtil.javaTypeToOpenApiTypeConvert(apiParam.getType());
//array object file map
propertiesData.put("description", apiParam.getDesc());
propertiesData.put("example",StringUtil.removeDoubleQuotes(apiParam.getValue()));
propertiesData.put("example", StringUtil.removeDoubleQuotes(apiParam.getValue()));

if (!"object".equals(openApiType)) {
propertiesData.put("type", openApiType);
Expand All @@ -386,25 +399,24 @@ private static Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<St
if (CollectionUtil.isNotEmpty(apiParam.getChildren())) {
if (!apiParam.isSelfReferenceLoop()) {
Map<String, Object> arrayRef = new HashMap<>(4);
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren());
String suffix = isResp ? COMPONENT_RESPONSE_SUFFIX : COMPONENT_REQUEST_SUFFIX;
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren(), suffix);
if (childSchemaName.contains(OpenApiSchemaUtil.NO_BODY_PARAM)) {
propertiesData.put("type", "object");
propertiesData.put("description", apiParam.getDesc() + "(object)");
} else {
component.put(childSchemaName, buildProperties(apiParam.getChildren(), component,componentKey));
component.put(childSchemaName, buildProperties(apiParam.getChildren(), component, isResp));
arrayRef.put("$ref", componentKey + childSchemaName);
propertiesData.put("items", arrayRef);
}
}
}
//基础数据类型
else{
else {
Map<String, Object> arrayRef = new HashMap<>(4);
arrayRef.put("type", "string");
propertiesData.put("items", arrayRef);
}


}
if ("file".equals(apiParam.getType())) {
propertiesData.put("type", "string");
Expand All @@ -414,13 +426,14 @@ private static Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<St
if (CollectionUtil.isNotEmpty(apiParam.getChildren())) {
propertiesData.put("type", "object");
propertiesData.put("description", apiParam.getDesc() + "(object)");
String suffix = isResp ? COMPONENT_RESPONSE_SUFFIX : COMPONENT_REQUEST_SUFFIX;
if (!apiParam.isSelfReferenceLoop()) {
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren());
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren(), suffix);
if (childSchemaName.contains(OpenApiSchemaUtil.NO_BODY_PARAM)) {
propertiesData.put("type", "object");
propertiesData.put("description", apiParam.getDesc() + "(object)");
} else {
component.put(childSchemaName, buildProperties(apiParam.getChildren(), component,componentKey));
component.put(childSchemaName, buildProperties(apiParam.getChildren(), component, isResp));
propertiesData.put("$ref", componentKey + childSchemaName);
}
}
Expand Down
79 changes: 40 additions & 39 deletions src/main/java/com/power/doc/builder/openapi/OpenApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@
import com.power.common.util.CollectionUtil;
import com.power.common.util.FileUtil;
import com.power.common.util.StringUtil;
import com.power.doc.builder.DocBuilderTemplate;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.Methods;
import com.power.doc.factory.BuildTemplateFactory;
import com.power.doc.helper.JavaProjectBuilderHelper;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiDoc;
import com.power.doc.model.ApiMethodDoc;
import com.power.doc.model.ApiParam;
import com.power.doc.model.ApiReqParam;
import com.power.doc.model.openapi.OpenApiTag;
import com.power.doc.template.IDocBuildTemplate;
import com.power.doc.utils.JsonUtil;
import com.power.doc.utils.OpenApiSchemaUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;

import static com.power.doc.constants.DocGlobalConstants.ARRAY;
import static com.power.doc.constants.DocGlobalConstants.OPENAPI_3_COMPONENT_KRY;
import static com.power.doc.constants.DocGlobalConstants.*;

/**
* @author xingzi
*/
public class OpenApiBuilder extends AbstractOpenApiBuilder {

@Override
String getModuleName() {
return OPENAPI_3_COMPONENT_KRY;
}

private static final OpenApiBuilder INSTANCE = new OpenApiBuilder();

/**
* For unit testing
* For unit testing
*
* @param config Configuration of smart-doc
*/
Expand All @@ -76,7 +76,7 @@ public static void buildOpenApi(ApiConfig config) {
* @param projectBuilder JavaDocBuilder of QDox
*/
public static void buildOpenApi(ApiConfig config, JavaProjectBuilder projectBuilder) {
List<ApiDoc> apiDocList = INSTANCE.getOpenApiDocs(config,projectBuilder);
List<ApiDoc> apiDocList = INSTANCE.getOpenApiDocs(config, projectBuilder);
INSTANCE.openApiCreate(config, apiDocList);
}

Expand All @@ -88,6 +88,7 @@ public static void buildOpenApi(ApiConfig config, JavaProjectBuilder projectBuil
*/
@Override
public void openApiCreate(ApiConfig config, List<ApiDoc> apiDocList) {
this.setComponentKey(getModuleName());
Map<String, Object> json = new HashMap<>(8);
json.put("openapi", "3.0.3");
json.put("info", buildInfo(config));
Expand Down Expand Up @@ -145,7 +146,7 @@ public Map<String, Object> buildPathUrlsRequest(ApiConfig apiConfig, ApiMethodDo
} else {
request.put("tags", new String[]{apiDoc.getDesc()});
}
request.put("requestBody", buildRequestBody(apiConfig, apiMethodDoc, DocGlobalConstants.OPENAPI_3_COMPONENT_KRY));
request.put("requestBody", buildRequestBody(apiConfig, apiMethodDoc));
request.put("parameters", buildParameters(apiMethodDoc));
request.put("responses", buildResponses(apiConfig, apiMethodDoc));
request.put("deprecated", apiMethodDoc.isDeprecated());
Expand All @@ -159,14 +160,14 @@ public Map<String, Object> buildPathUrlsRequest(ApiConfig apiConfig, ApiMethodDo
*
* @param apiMethodDoc ApiMethodDoc
*/
private static Map<String, Object> buildRequestBody(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc, String componentKey) {
private Map<String, Object> buildRequestBody(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc) {
Map<String, Object> requestBody = new HashMap<>(8);
boolean isPost = (apiMethodDoc.getType().equals(Methods.POST.getValue())
|| apiMethodDoc.getType().equals(Methods.PUT.getValue()) ||
apiMethodDoc.getType().equals(Methods.PATCH.getValue()));
|| apiMethodDoc.getType().equals(Methods.PUT.getValue()) ||
apiMethodDoc.getType().equals(Methods.PATCH.getValue()));
//add content of post method
if (isPost) {
requestBody.put("content", buildContent(apiConfig, apiMethodDoc, false, componentKey));
requestBody.put("content", buildContent(apiConfig, apiMethodDoc, false));
return requestBody;
}
return null;
Expand All @@ -183,7 +184,7 @@ private static Map<String, Object> buildRequestBody(ApiConfig apiConfig, ApiMeth
public Map<String, Object> buildResponsesBody(ApiConfig apiConfig, ApiMethodDoc apiMethodDoc) {
Map<String, Object> responseBody = new HashMap<>(10);
responseBody.put("description", "OK");
responseBody.put("content", buildContent(apiConfig, apiMethodDoc, true, DocGlobalConstants.OPENAPI_3_COMPONENT_KRY));
responseBody.put("content", buildContent(apiConfig, apiMethodDoc, true));
return responseBody;
}

Expand Down Expand Up @@ -261,31 +262,31 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {

@Override
public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs) {
Map<String, Object> schemas = new HashMap<>(4);
Map<String, Object> component = new HashMap<>();
component.put("string", STRING_COMPONENT);
apiDocs.forEach(
a -> {
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
//request components
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams());
List<ApiParam> requestParams = method.getRequestParams();
Map<String, Object> prop = buildProperties(requestParams, component,OPENAPI_3_COMPONENT_KRY);
component.put(requestSchema, prop);
//response components
List<ApiParam> responseParams = method.getResponseParams();
String schemaName = OpenApiSchemaUtil.getClassNameFromParams(method.getResponseParams());
component.put(schemaName, buildProperties(responseParams, component,OPENAPI_3_COMPONENT_KRY));
}
);
}
);
component.remove(OpenApiSchemaUtil.NO_BODY_PARAM);
schemas.put("schemas", component);
return schemas;
}
Map<String, Object> schemas = new HashMap<>(4);
Map<String, Object> component = new HashMap<>();
component.put("string", STRING_COMPONENT);
apiDocs.forEach(
a -> {
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
//request components
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams(), COMPONENT_REQUEST_SUFFIX);
List<ApiParam> requestParams = method.getRequestParams();
Map<String, Object> prop = buildProperties(requestParams, component, false);
component.put(requestSchema, prop);
//response components
List<ApiParam> responseParams = method.getResponseParams();
String schemaName = OpenApiSchemaUtil.getClassNameFromParams(method.getResponseParams(), COMPONENT_RESPONSE_SUFFIX);
component.put(schemaName, buildProperties(responseParams, component, true));
}
);
}
);
component.remove(OpenApiSchemaUtil.NO_BODY_PARAM);
schemas.put("schemas", component);
return schemas;
}


}
Loading

0 comments on commit 1961f51

Please sign in to comment.