Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit 934bf5d

Browse files
authored
Merge pull request #807 from shalousun/master
feat: Optimize and refactor constant definitions.
2 parents 69b6093 + 23863f4 commit 934bf5d

35 files changed

Lines changed: 357 additions & 238 deletions

src/main/java/com/ly/doc/builder/PostmanJsonBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.ly.doc.builder;
2222

2323
import com.ly.doc.constants.DocGlobalConstants;
24+
import com.ly.doc.constants.MediaType;
2425
import com.ly.doc.constants.Methods;
2526
import com.ly.doc.factory.BuildTemplateFactory;
2627
import com.ly.doc.helper.JavaProjectBuilderHelper;
@@ -160,7 +161,7 @@ private static UrlBean buildUrlBean(ApiMethodDoc apiMethodDoc) {
160161

161162
List<ParamBean> queryParams = new ArrayList<>();
162163
if (!apiMethodDoc.getType().equals(Methods.POST.getValue()) ||
163-
apiMethodDoc.getContentType().contains(DocGlobalConstants.JSON_CONTENT_TYPE)) {
164+
apiMethodDoc.getContentType().contains(MediaType.APPLICATION_JSON)) {
164165
for (ApiParam apiParam : apiMethodDoc.getQueryParams()) {
165166
ParamBean queryParam = new ParamBean();
166167
queryParam.setDescription(apiParam.getDesc());
@@ -190,7 +191,7 @@ private static UrlBean buildUrlBean(ApiMethodDoc apiMethodDoc) {
190191
*/
191192
private static BodyBean buildBodyBean(ApiMethodDoc apiMethodDoc) {
192193
BodyBean bodyBean;
193-
if (apiMethodDoc.getContentType().contains(DocGlobalConstants.JSON_CONTENT_TYPE)) {
194+
if (apiMethodDoc.getContentType().contains(MediaType.APPLICATION_JSON)) {
194195
bodyBean = new BodyBean(Boolean.FALSE);// Json request
195196
bodyBean.setMode(DocGlobalConstants.POSTMAN_MODE_RAW);
196197
if (apiMethodDoc.getRequestExample() != null) {

src/main/java/com/ly/doc/builder/ProjectDocConfigBuilder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.ly.doc.builder;
2222

2323
import com.ly.doc.constants.DocGlobalConstants;
24+
import com.ly.doc.constants.HighLightJsConstants;
2425
import com.ly.doc.constants.HighlightStyle;
2526
import com.ly.doc.helper.JavaProjectBuilderHelper;
2627
import com.ly.doc.model.*;
@@ -318,31 +319,31 @@ private void checkBodyAdvice(BodyAdvice bodyAdvice) {
318319

319320
private void setHighlightStyle() {
320321
String style = apiConfig.getStyle();
321-
if (DocGlobalConstants.HIGH_LIGHT_DEFAULT_STYLE.equals(style)) {
322+
if (HighLightJsConstants.HIGH_LIGHT_DEFAULT_STYLE.equals(style)) {
322323
// use local css file
323-
apiConfig.setHighlightStyleLink(DocGlobalConstants.HIGH_LIGHT_CSS_DEFAULT);
324+
apiConfig.setHighlightStyleLink(HighLightJsConstants.HIGH_LIGHT_CSS_DEFAULT);
324325
return;
325326
}
326327
if (HighlightStyle.containsStyle(style)) {
327-
apiConfig.setHighlightStyleLink(String.format(DocGlobalConstants.HIGH_LIGHT_CSS_URL_FORMAT, style));
328+
apiConfig.setHighlightStyleLink(String.format(HighLightJsConstants.HIGH_LIGHT_CSS_URL_FORMAT, style));
328329
return;
329330
}
330331
Random random = new Random();
331-
if (DocGlobalConstants.HIGH_LIGHT_CSS_RANDOM_LIGHT.equals(style)) {
332+
if (HighLightJsConstants.HIGH_LIGHT_CSS_RANDOM_LIGHT.equals(style)) {
332333
// Eliminate styles that do not match the template
333334
style = HighlightStyle.randomLight(random);
334335
if (HighlightStyle.containsStyle(style)) {
335336
apiConfig.setStyle(style);
336-
apiConfig.setHighlightStyleLink(String.format(DocGlobalConstants.HIGH_LIGHT_CSS_URL_FORMAT, style));
337+
apiConfig.setHighlightStyleLink(String.format(HighLightJsConstants.HIGH_LIGHT_CSS_URL_FORMAT, style));
337338
} else {
338339
apiConfig.setStyle(null);
339340
}
340-
} else if (DocGlobalConstants.HIGH_LIGHT_CSS_RANDOM_DARK.equals(style)) {
341+
} else if (HighLightJsConstants.HIGH_LIGHT_CSS_RANDOM_DARK.equals(style)) {
341342
style = HighlightStyle.randomDark(random);
342-
if (DocGlobalConstants.HIGH_LIGHT_DEFAULT_STYLE.equals(style)) {
343-
apiConfig.setHighlightStyleLink(DocGlobalConstants.HIGH_LIGHT_CSS_DEFAULT);
343+
if (HighLightJsConstants.HIGH_LIGHT_DEFAULT_STYLE.equals(style)) {
344+
apiConfig.setHighlightStyleLink(HighLightJsConstants.HIGH_LIGHT_CSS_DEFAULT);
344345
} else {
345-
apiConfig.setHighlightStyleLink(String.format(DocGlobalConstants.HIGH_LIGHT_CSS_URL_FORMAT, style));
346+
apiConfig.setHighlightStyleLink(String.format(HighLightJsConstants.HIGH_LIGHT_CSS_URL_FORMAT, style));
346347
}
347348
apiConfig.setStyle(style);
348349
} else {

src/main/java/com/ly/doc/builder/openapi/AbstractOpenApiBuilder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import com.ly.doc.builder.DocBuilderTemplate;
2828
import com.ly.doc.builder.ProjectDocConfigBuilder;
2929
import com.ly.doc.constants.ComponentTypeEnum;
30-
import com.ly.doc.constants.DocGlobalConstants;
30+
import com.ly.doc.constants.MediaType;
31+
import com.ly.doc.constants.ParamTypeConstants;
3132
import com.ly.doc.factory.BuildTemplateFactory;
3233
import com.ly.doc.model.*;
3334
import com.ly.doc.model.openapi.OpenApiTag;
@@ -208,7 +209,7 @@ public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean is
208209
if (isRep) {
209210
String responseRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getResponseParams());
210211
if (apiMethodDoc.getIsResponseArray() == 1) {
211-
schema.put("type", PARAM_TYPE_ARRAY);
212+
schema.put("type", ParamTypeConstants.PARAM_TYPE_ARRAY);
212213
innerScheme.put("$ref", responseRef);
213214
schema.put("items", innerScheme);
214215
} else if (CollectionUtil.isNotEmpty(apiMethodDoc.getResponseParams())) {
@@ -220,15 +221,15 @@ public Map<String, Object> buildBodySchema(ApiMethodDoc apiMethodDoc, boolean is
220221
// for request
221222
String requestRef;
222223
String randomName = ComponentTypeEnum.getRandomName(ApiConfig.getInstance().getComponentType(), apiMethodDoc);
223-
if (apiMethodDoc.getContentType().equals(DocGlobalConstants.URL_CONTENT_TYPE)) {
224+
if (apiMethodDoc.getContentType().equals(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
224225
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getQueryParams());
225226
} else {
226227
requestRef = componentKey + OpenApiSchemaUtil.getClassNameFromParams(apiMethodDoc.getRequestParams());
227228
}
228229
// remove special characters in url
229230
if (CollectionUtil.isNotEmpty(apiMethodDoc.getRequestParams())) {
230231
if (apiMethodDoc.getIsRequestArray() == 1) {
231-
schema.put("type", PARAM_TYPE_ARRAY);
232+
schema.put("type", ParamTypeConstants.PARAM_TYPE_ARRAY);
232233
innerScheme.put("$ref", requestRef);
233234
schema.put("items", innerScheme);
234235
} else {
@@ -301,12 +302,12 @@ public Map<String, Object> buildParametersSchema(ApiParam apiParam) {
301302
if ("enum".equals(apiParam.getType())) {
302303
schema.put("enum", apiParam.getEnumValues());
303304
}
304-
} else if (PARAM_TYPE_ARRAY.equals(openApiType)) {
305+
} else if (ParamTypeConstants.PARAM_TYPE_ARRAY.equals(openApiType)) {
305306
if (CollectionUtil.isNotEmpty(apiParam.getEnumValues())) {
306307
schema.put("type", "string");
307308
schema.put("items", apiParam.getEnumValues());
308309
} else {
309-
schema.put("type", PARAM_TYPE_ARRAY);
310+
schema.put("type", ParamTypeConstants.PARAM_TYPE_ARRAY);
310311
Map<String, String> map = new HashMap<>(4);
311312
map.put("type", "string");
312313
map.put("format", "string");

src/main/java/com/ly/doc/builder/openapi/OpenApiBuilder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.ly.doc.constants.DocGlobalConstants;
2626
import com.ly.doc.constants.Methods;
27+
import com.ly.doc.constants.ParamTypeConstants;
2728
import com.ly.doc.model.*;
2829
import com.ly.doc.utils.OpenApiSchemaUtil;
2930
import com.power.common.util.CollectionUtil;
@@ -208,7 +209,7 @@ List<Map<String, Object>> buildParameters(ApiMethodDoc apiMethodDoc) {
208209
if (apiParam.isHasItems()) {
209210
parameters = getStringParams(apiParam, false);
210211
Map<String, Object> arrayMap = new HashMap<>(16);
211-
arrayMap.put("type", DocGlobalConstants.PARAM_TYPE_ARRAY);
212+
arrayMap.put("type", ParamTypeConstants.PARAM_TYPE_ARRAY);
212213
arrayMap.put("items", getStringParams(apiParam, apiParam.isHasItems()));
213214
parameters.put("schema", arrayMap);
214215
parametersList.add(parameters);
@@ -251,14 +252,14 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
251252
parameters.put("in", "query");
252253
parameters.put("schema", buildParametersSchema(apiParam));
253254
} else {
254-
if (DocGlobalConstants.PARAM_TYPE_OBJECT.equals(apiParam.getType()) ||
255-
(DocGlobalConstants.PARAM_TYPE_ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
255+
if (ParamTypeConstants.PARAM_TYPE_OBJECT.equals(apiParam.getType()) ||
256+
(ParamTypeConstants.PARAM_TYPE_ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
256257
parameters.put("type", "object");
257258
parameters.put("description", "(complex POJO please use @RequestBody)");
258259
} else {
259260
String desc = apiParam.getDesc();
260-
if (desc.contains(DocGlobalConstants.PARAM_TYPE_FILE)) {
261-
parameters.put("type", DocGlobalConstants.PARAM_TYPE_FILE);
261+
if (desc.contains(ParamTypeConstants.PARAM_TYPE_FILE)) {
262+
parameters.put("type", ParamTypeConstants.PARAM_TYPE_FILE);
262263
} else if (desc.contains("string")) {
263264
parameters.put("type", "string");
264265
} else {

src/main/java/com/ly/doc/builder/openapi/SwaggerBuilder.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
package com.ly.doc.builder.openapi;
2323

2424
import com.ly.doc.constants.DocGlobalConstants;
25+
import com.ly.doc.constants.MediaType;
26+
import com.ly.doc.constants.ParamTypeConstants;
2527
import com.ly.doc.model.*;
2628
import com.ly.doc.utils.DocUtil;
2729
import com.ly.doc.utils.OpenApiSchemaUtil;
@@ -149,7 +151,7 @@ public Map<String, Object> buildPathUrlsRequest(ApiConfig apiConfig, ApiMethodDo
149151
}
150152
if (hasFile(parameters)) {
151153
List<String> formData = new ArrayList<>();
152-
formData.add(DocGlobalConstants.FILE_CONTENT_TYPE);
154+
formData.add(MediaType.MULTIPART_FORM_DATA);
153155
request.put("consumes", formData);
154156
}
155157
request.put("parameters", parameters);
@@ -207,9 +209,9 @@ List<Map<String, Object>> buildParameters(ApiMethodDoc apiMethodDoc) {
207209
parametersList.add(parameters);
208210
}
209211
for (ApiParam apiParam : apiMethodDoc.getQueryParams()) {
210-
if (apiParam.getType().equals(DocGlobalConstants.PARAM_TYPE_ARRAY) || apiParam.isHasItems()) {
212+
if (apiParam.getType().equals(ParamTypeConstants.PARAM_TYPE_ARRAY) || apiParam.isHasItems()) {
211213
parameters = getStringParams(apiParam, false);
212-
parameters.put("type", DocGlobalConstants.PARAM_TYPE_ARRAY);
214+
parameters.put("type", ParamTypeConstants.PARAM_TYPE_ARRAY);
213215
parameters.put("items", getStringParams(apiParam, true));
214216
parametersList.add(parameters);
215217
} else {
@@ -251,12 +253,12 @@ Map<String, Object> getStringParams(ApiParam apiParam, boolean hasItems) {
251253
parameters.put("required", apiParam.isRequired());
252254
parameters.put("type", apiParam.getType());
253255
} else {
254-
if (DocGlobalConstants.PARAM_TYPE_OBJECT.equals(apiParam.getType()) || (DocGlobalConstants.PARAM_TYPE_ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
256+
if (ParamTypeConstants.PARAM_TYPE_OBJECT.equals(apiParam.getType()) || (ParamTypeConstants.PARAM_TYPE_ARRAY.equals(apiParam.getType()) && apiParam.isHasItems())) {
255257
parameters.put("type", "object(complex POJO please use @RequestBody)");
256258
} else {
257259
String desc = apiParam.getDesc();
258-
if (desc.contains(DocGlobalConstants.PARAM_TYPE_FILE)) {
259-
parameters.put("type", DocGlobalConstants.PARAM_TYPE_FILE);
260+
if (desc.contains(ParamTypeConstants.PARAM_TYPE_FILE)) {
261+
parameters.put("type", ParamTypeConstants.PARAM_TYPE_FILE);
260262
} else if (desc.contains("string")) {
261263
parameters.put("type", "string");
262264
} else {

src/main/java/com/ly/doc/constants/DocAnnotationConstants.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@
2727
*/
2828
public interface DocAnnotationConstants {
2929

30-
String SHORT_CONTROLLER = "Controller";
31-
32-
String SHORT_REST_CONTROLLER = "RestController";
33-
34-
String SHORT_PATH_VARIABLE = "PathVariable";
35-
36-
String SHORT_REQ_PARAM = "RequestParam";
3730

3831
String SHORT_JSON_IGNORE = "JsonIgnore";
3932

@@ -76,19 +69,12 @@ public interface DocAnnotationConstants {
7669

7770
String DEFAULT_VALUE_PROP = "defaultValue";
7871

79-
String REQUEST_MAPPING = "RequestMapping";
80-
8172
String DEPRECATED = "Deprecated";
8273

8374
String JSON_VALUE = "JsonValue";
8475

8576
String JSON_CREATOR = "JsonCreator";
8677

87-
String MAX = "max";
88-
89-
String SIZE = "size";
90-
91-
String LENGTH = "length";
9278

9379
String JSON_PROPERTY = "JsonProperty";
9480

@@ -103,14 +89,6 @@ public interface DocAnnotationConstants {
10389
* Fastjson JSONType annotation ignores prop
10490
*/
10591
String IGNORE_PROP = "ignores";
106-
/**
107-
* Jsr303 param message
108-
*/
109-
String MESSAGE = "message";
110-
/**
111-
* Jsr303 param regexp
112-
*/
113-
String REGEXP = "regexp";
11492

11593
/**
11694
* `@javax.websocket.OnOpen` or `@jakarta.websocket.OnOpen` annotation name

src/main/java/com/ly/doc/constants/DocGlobalConstants.java

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -147,43 +147,17 @@ public interface DocGlobalConstants {
147147

148148
String OPEN_API_JSON = "/openapi.json";
149149

150-
String CONTROLLER_FULLY = "org.springframework.stereotype.Controller";
151150

152-
String REST_CONTROLLER_FULLY = "org.springframework.web.bind.annotation.RestController";
153151

154-
String GET_MAPPING_FULLY = "org.springframework.web.bind.annotation.GetMapping";
155-
156-
String POST_MAPPING_FULLY = "org.springframework.web.bind.annotation.PostMapping";
157-
158-
String PUT_MAPPING_FULLY = "org.springframework.web.bind.annotation.PutMapping";
159-
160-
String PATCH_MAPPING_FULLY = "org.springframework.web.bind.annotation.PatchMapping";
161-
162-
String DELETE_MAPPING_FULLY = "org.springframework.web.bind.annotation.DeleteMapping";
163-
164-
String REQUEST_MAPPING_FULLY = "org.springframework.web.bind.annotation.RequestMapping";
165-
166-
String REQUEST_BODY_FULLY = "org.springframework.web.bind.annotation.RequestBody";
167152

168153
String MODE_AND_VIEW_FULLY = "org.springframework.web.servlet.ModelAndView";
169154

170155
String FEIGN_CLIENT_FULLY = "org.springframework.cloud.netflix.feign.FeignClient";
171156

172157
String FEIGN_CLIENT = "FeignClient";
173158

174-
String MULTIPART_FILE_FULLY = "org.springframework.web.multipart.MultipartFile";
175-
176-
String JAVA_OBJECT_FULLY = "java.lang.Object";
177-
178-
String JAVA_BOOLEAN = "java.lang.Boolean";
179159

180-
String JAVA_STRING_FULLY = "java.lang.String";
181160

182-
String JAVA_MAP_FULLY = "java.util.Map";
183-
184-
String JAVA_LIST_FULLY = "java.util.List";
185-
186-
String JAVA_DEPRECATED_FULLY = "java.lang.Deprecated";
187161

188162
String DEFAULT_VERSION = "-";
189163

@@ -203,14 +177,6 @@ public interface DocGlobalConstants {
203177

204178
String SPRING_WEB_ANNOTATION_PACKAGE = "org.springframework.web.bind.annotation";
205179

206-
String FILE_CONTENT_TYPE = "multipart/form-data";
207-
208-
209-
String APPLICATION_JSON = "application/json";
210-
211-
String JSON_CONTENT_TYPE = "application/json";
212-
213-
String URL_CONTENT_TYPE = "application/x-www-form-urlencoded;charset=UTF-8";
214180

215181
String POSTMAN_MODE_FORMDATA = "formdata";
216182

@@ -229,54 +195,16 @@ public interface DocGlobalConstants {
229195
String CURL_POST_PUT_JSON = "curl -X %s -H 'Content-Type: application/json;charset=UTF-8' %s -i %s --data '%s'";
230196

231197
String EMPTY = "";
232-
/**
233-
* param type enum
234-
*/
235-
String PARAM_TYPE_ENUM = "enum";
236-
/**
237-
* param type array
238-
*/
239-
String PARAM_TYPE_ARRAY = "array";
240-
/**
241-
* param type file
242-
*/
243-
String PARAM_TYPE_FILE = "file";
244-
/**
245-
* param type text
246-
*/
247-
String PARAM_TYPE_TEXT = "text";
248-
/**
249-
* param type map
250-
*/
251-
String PARAM_TYPE_MAP = "map";
252-
/**
253-
* param type object
254-
*/
255-
String PARAM_TYPE_OBJECT = "object";
256198

257-
String JSON_PROPERTY_READ_WRITE = "JsonProperty.Access.READ_WRITE";
258199

259-
String JSON_PROPERTY_READ_ONLY = "JsonProperty.Access.READ_ONLY";
260200

261-
String JSON_PROPERTY_WRITE_ONLY = "JsonProperty.Access.WRITE_ONLY";
262201

263202
String CSS_CDN_CH = "https://fonts.googleapis.cnpmjs.org";
264203

265204
String CSS_CDN = "https://fonts.googleapis.com";
266205

267206
String PATH_DELIMITER = "/";
268207

269-
270-
String HIGH_LIGHT_CSS_URL_FORMAT = "https://cdn.bootcdn.net/ajax/libs/highlight.js/10.3.2/styles/%s.min.css";
271-
272-
String HIGH_LIGHT_DEFAULT_STYLE = "xt256";
273-
274-
String HIGH_LIGHT_CSS_DEFAULT = "xt256.min.css";
275-
276-
String HIGH_LIGHT_CSS_RANDOM_LIGHT = "randomLight";
277-
278-
String HIGH_LIGHT_CSS_RANDOM_DARK = "randomDark";
279-
280208
String MULTI_URL_SEPARATOR = ";\t";
281209

282210
String PARAM_PREFIX = "└─";

0 commit comments

Comments
 (0)