Skip to content

Commit

Permalink
Merge pull request #601 from netdied/master
Browse files Browse the repository at this point in the history
feat:fix #564
  • Loading branch information
shalousun authored Sep 13, 2023
2 parents 7b3b9a6 + 089a7ba commit 3b97d8a
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/*
* smart-doc https://github.com/smart-doc-group/smart-doc
*smart-doc https://github.com/smart-doc-group/smart-doc
*
* Copyright (C) 2018-2023 smart-doc
*Copyright(C)2018-2023smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*Licensed to the Apache Software Foundation(ASF)under one
*or more contributor license agreements.See the NOTICE file
*distributed with this work for additional information
*regarding copyright ownership.The ASF licenses this file
*to you under the Apache License,Version2.0(the
*"License");you may not use this file except in compliance
*with the License.You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*Unless required by applicable law or agreed to in writing,
*software distributed under the License is distributed on an
*"AS IS"BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY
*KIND,either express or implied.See the License for the
*specific language governing permissions and limitations
*under the License.
*/

package com.power.doc.builder.openapi;
Expand All @@ -27,6 +27,7 @@
import com.power.common.util.StringUtil;
import com.power.doc.builder.DocBuilderTemplate;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.ComponentTypeEnum;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.factory.BuildTemplateFactory;
import com.power.doc.model.*;
Expand Down Expand Up @@ -164,7 +165,7 @@ public Map<String, Object> buildContentBody(ApiConfig apiConfig, ApiMethodDoc ap
} else if (!isRep && Objects.nonNull(apiMethodDoc.getRequestSchema())) {
content.put("schema", apiMethodDoc.getRequestSchema());
} else {
content.put("schema", buildBodySchema(apiMethodDoc, isRep));
content.put("schema", buildBodySchema(apiMethodDoc, ComponentTypeEnum.getComponentEnumByCode(apiConfig.getComponentType()), isRep));
}

if (OPENAPI_2_COMPONENT_KRY.equals(componentKey) && !isRep) {
Expand All @@ -184,17 +185,18 @@ public Map<String, Object> buildContentBody(ApiConfig apiConfig, ApiMethodDoc ap
* @param apiMethodDoc ApiMethodDoc
* @param isRep is response
*/
public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean isRep) {
public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, ComponentTypeEnum componentTypeEnum, boolean isRep) {
Map<String, Object> schema = new HashMap<>(10);
Map<String, Object> innerScheme = new HashMap<>(10);
String requestRef;
String randomName = ComponentTypeEnum.getRandomName(componentTypeEnum, apiMethodDoc);
if (apiMethodDoc.getContentType().equals(DocGlobalConstants.URL_CONTENT_TYPE)) {
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getQueryParams(), COMPONENT_REQUEST_SUFFIX);
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getQueryParams());
} else {
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getRequestParams(), COMPONENT_REQUEST_SUFFIX);
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getRequestParams());
}
//remove special characters in url
String responseRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getResponseParams(), COMPONENT_RESPONSE_SUFFIX);
String responseRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getResponseParams());
if (!isRep && CollectionUtil.isNotEmpty(apiMethodDoc.getRequestParams())) {
if (apiMethodDoc.getIsRequestArray() == 1) {
schema.put("type", ARRAY);
Expand Down Expand Up @@ -303,7 +305,7 @@ public static Map<String, Object> buildParametersSchema(ApiReqParam header) {
Map<String, Object> schema = new HashMap<>(10);
String openApiType = DocUtil.javaTypeToOpenApiTypeConvert(header.getType());
schema.put("type", openApiType);
schema.put("format", "int16".equals(header.getType()) ? "int32" : header.getType());
schema.put("format", openApiType);
return schema;
}

Expand All @@ -324,7 +326,7 @@ 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, ComponentTypeEnum componentTypeEnum);

/**
* component schema properties
Expand Down Expand Up @@ -390,8 +392,7 @@ private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, O
if (CollectionUtil.isNotEmpty(apiParam.getChildren())) {
if (!apiParam.isSelfReferenceLoop()) {
Map<String, Object> arrayRef = new HashMap<>(4);
String suffix = isResp ? COMPONENT_RESPONSE_SUFFIX : COMPONENT_REQUEST_SUFFIX;
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren(), suffix);
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren());
if (childSchemaName.contains(OpenApiSchemaUtil.NO_BODY_PARAM)) {
propertiesData.put("type", "object");
propertiesData.put("description", apiParam.getDesc() + "(object)");
Expand All @@ -417,9 +418,8 @@ private Map<String, Object> buildPropertiesData(ApiParam apiParam, Map<String, O
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(), suffix);
String childSchemaName = OpenApiSchemaUtil.getClassNameFromParams(apiParam.getChildren());
if (childSchemaName.contains(OpenApiSchemaUtil.NO_BODY_PARAM)) {
propertiesData.put("type", "object");
propertiesData.put("description", apiParam.getDesc() + "(object)");
Expand Down Expand Up @@ -451,4 +451,4 @@ public List<ApiDoc> getOpenApiDocs(ApiConfig config, JavaProjectBuilder projectB
IDocBuildTemplate docBuildTemplate = BuildTemplateFactory.getDocBuildTemplate(config.getFramework());
return docBuildTemplate.getApiData(configBuilder);
}
}
}
11 changes: 6 additions & 5 deletions src/main/java/com/power/doc/builder/openapi/OpenApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.power.common.util.CollectionUtil;
import com.power.common.util.FileUtil;
import com.power.doc.constants.ComponentTypeEnum;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.Methods;
import com.power.doc.helper.JavaProjectBuilderHelper;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void openApiCreate(ApiConfig config, List<ApiDoc> apiDocList) {
Set<OpenApiTag> tags = new HashSet<>();
json.put("tags", tags);
json.put("paths", buildPaths(config, apiDocList, tags));
json.put("components", buildComponentsSchema(apiDocList));
json.put("components", buildComponentsSchema(apiDocList, ComponentTypeEnum.getComponentEnumByCode(config.getComponentType())));

String filePath = config.getOutPath();
filePath = filePath + DocGlobalConstants.OPEN_API_JSON;
Expand Down Expand Up @@ -265,23 +266,23 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
}

@Override
public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs) {
public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs, ComponentTypeEnum componentTypeEnum) {
Map<String, Object> schemas = new HashMap<>(4);
Map<String, Object> component = new HashMap<>();
component.put("string", STRING_COMPONENT);
component.put(DEFAULT_PRIMITIVE, STRING_COMPONENT);
apiDocs.forEach(
a -> {
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
//request components
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams(), COMPONENT_REQUEST_SUFFIX);
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams());
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);
String schemaName = OpenApiSchemaUtil.getClassNameFromParams(method.getResponseParams());
component.put(schemaName, buildProperties(responseParams, component, true));
}
);
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/power/doc/builder/openapi/SwaggerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.power.common.util.CollectionUtil;
import com.power.common.util.FileUtil;
import com.power.common.util.StringUtil;
import com.power.doc.constants.ComponentTypeEnum;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.helper.JavaProjectBuilderHelper;
import com.power.doc.model.*;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void openApiCreate(ApiConfig config, List<ApiDoc> apiDocList) {
Set<OpenApiTag> tags = new HashSet<>();
json.put("tags", tags);
json.put("paths", buildPaths(config, apiDocList, tags));
json.put("definitions", buildComponentsSchema(apiDocList));
json.put("definitions", buildComponentsSchema(apiDocList, ComponentTypeEnum.getComponentEnumByCode(config.getComponentType())));

String filePath = config.getOutPath();
filePath = filePath + DocGlobalConstants.OPEN_API_JSON;
Expand Down Expand Up @@ -277,22 +278,22 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
}

@Override
public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs) {
public Map<String, Object> buildComponentsSchema(List<ApiDoc> apiDocs, ComponentTypeEnum componentTypeEnum) {
Map<String, Object> component = new HashMap<>();
component.put("string", STRING_COMPONENT);
component.put(DEFAULT_PRIMITIVE, STRING_COMPONENT);
apiDocs.forEach(
a -> {
List<ApiMethodDoc> apiMethodDocs = a.getList();
apiMethodDocs.forEach(
method -> {
//request components
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams(), COMPONENT_REQUEST_SUFFIX);
String requestSchema = OpenApiSchemaUtil.getClassNameFromParams(method.getRequestParams());
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);
String schemaName = OpenApiSchemaUtil.getClassNameFromParams(method.getResponseParams());
component.put(schemaName, buildProperties(responseParams, component, true));
}
);
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/power/doc/constants/ComponentTypeEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.power.doc.constants;

import com.power.doc.model.ApiMethodDoc;
import org.apache.commons.lang3.StringUtils;

/**
* @author xingzi
* Date 2023/9/10 14:47
*/
public enum ComponentTypeEnum {
/**
* support @Validated
*/
RANDOM("RANDOM"),
/**
* don't support @Validated,
* for openapi generator
*/
NORMAL("NORMAL");

ComponentTypeEnum(String componentType) {
this.componentType = componentType;
}

/**
* openapi component generator Key type
*/
private final String componentType;

public static String getRandomName(ComponentTypeEnum componentTypeEnum, ApiMethodDoc apiMethodDoc) {
if (componentTypeEnum.equals(RANDOM)) {
return apiMethodDoc.getUrl();
}
return StringUtils.EMPTY;
}

public String getComponentType() {
return componentType;
}

public static ComponentTypeEnum getComponentEnumByCode(String code) {
for (ComponentTypeEnum typeEnum : ComponentTypeEnum.values()) {
if (typeEnum.getComponentType().equals(code)) {
return typeEnum;
}
}
return RANDOM;
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/power/doc/constants/DocGlobalConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ public interface DocGlobalConstants {

String OPENAPI_3_COMPONENT_KRY = "#/components/schemas/";

String COMPONENT_REQUEST_SUFFIX = "_request";

String COMPONENT_RESPONSE_SUFFIX = "_response";

String SWAGGER_FILE_TAG = "formData";

Expand All @@ -262,4 +259,6 @@ public interface DocGlobalConstants {
String DEFAULT_FILTER_METHOD = "*";

String JAR_TEMP = "./smart-temp/";

String DEFAULT_PRIMITIVE = "defaultPrimitive";
}
24 changes: 24 additions & 0 deletions src/main/java/com/power/doc/model/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
*/
public class ApiConfig {

private final static ApiConfig apiConfig = new ApiConfig();

private ApiConfig() {
}

public static ApiConfig getInstance() {
return apiConfig;
}

/**
* Web server base url
*/
Expand Down Expand Up @@ -364,6 +373,21 @@ public class ApiConfig {
*/
private boolean randomMock;

/**
* build random component for openApi
* @see com.power.doc.constants.ComponentTypeEnum
*/
private String componentType;

public String getComponentType() {
return componentType;
}

public ApiConfig setComponentType(String componentType) {
this.componentType = componentType;
return this;
}

public String getCodePath() {
return codePath;
}
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/power/doc/utils/OpenApiSchemaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@
import java.util.regex.Pattern;

import com.power.common.util.CollectionUtil;
import com.power.common.util.MD6Util;
import com.power.common.util.StringUtil;
import com.power.doc.constants.ComponentTypeEnum;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiMethodDoc;
import com.power.doc.model.ApiParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import static com.power.doc.constants.TornaConstants.GSON;

/**
* @author yu 2020/11/29.
Expand Down Expand Up @@ -81,16 +90,22 @@ public static Map<String, Object> returnSchema(String returnGicName) {
return map;
}

public static String getClassNameFromParams(List<ApiParam> apiParams, String suffix) {
public static String getClassNameFromParams(List<ApiParam> apiParams) {
ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.getComponentEnumByCode(ApiConfig.getInstance().getComponentType());
// if array[Primitive] or Primitive
if (CollectionUtil.isNotEmpty(apiParams) && apiParams.size() == 1
&& StringUtil.isEmpty(apiParams.get(0).getClassName())
&& CollectionUtil.isEmpty(apiParams.get(0).getChildren())) {
return "string";
return DocGlobalConstants.DEFAULT_PRIMITIVE;
}
//random name
if (componentTypeEnum.equals(ComponentTypeEnum.RANDOM)) {
return DigestUtils.md5Hex(GSON.toJson(apiParams));
}
//className
for (ApiParam a : apiParams) {
if (StringUtil.isNotEmpty(a.getClassName())) {
return OpenApiSchemaUtil.delClassName(a.getClassName()) + suffix;
return OpenApiSchemaUtil.delClassName(a.getClassName());
}
}
return NO_BODY_PARAM;
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/power/doc/ApiDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public class ApiDocTest {
/**
* 包括设置请求头,缺失注释的字段批量在文档生成期使用定义好的注释
*/
@Deprecated
@Test
public void testBuilderControllersApi() {
ApiConfig config = new ApiConfig();
@Deprecated
ApiConfig config = ApiConfig.getInstance();
config.setServerUrl("http://127.0.0.1:8899");
// config.setStrict(true);
config.setOpenUrl("http://localhost:7700/api");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/power/doc/qbox/QboxScanSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void scanError() {
String outPath = Paths.get("target").toAbsolutePath().toString();

// config and scan
ApiConfig config = new ApiConfig();
ApiConfig config = ApiConfig.getInstance();
config.setServerUrl("HSF://127.0.0.1:8088");
config.setOpenUrl("http://demo.torna.cn/api");
config.setDebugEnvName("测试环境");
Expand Down

0 comments on commit 3b97d8a

Please sign in to comment.