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

Commit 0efe395

Browse files
committed
refactor: ♻️ optimize custom field logic and update related models
- Move custom field retrieval logic to getCustomFieldInfo method - Add customResponseField and customRequestField properties to CustomFieldInfo - Update JsonBuildHelper and ParamsBuildHelper to use new CustomFieldInfo structure - Improve code readability and maintainability by reducing duplicate logic
1 parent a1e79e5 commit 0efe395

File tree

4 files changed

+95
-26
lines changed

4 files changed

+95
-26
lines changed

src/main/java/com/ly/doc/helper/BaseHelper.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2024 smart-doc
2+
* Copyright (C) 2018-2025 smart-doc
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -227,15 +227,18 @@ else if (CollectionUtil.isEmpty(groupClasses)) {
227227
* Get the custom field information for a given field.
228228
* @param projectBuilder the project builder
229229
* @param docField the doc of java field
230-
* @param customResponseField the custom response field
231-
* @param customRequestField the custom request field
232230
* @param isResp the response flag for the parameter
233231
* @param simpleName the simple name of the field
232+
* @param fieldName the name of the field
234233
* @return the custom field information {@link CustomFieldInfo}
235-
*
236234
*/
237235
protected static CustomFieldInfo getCustomFieldInfo(ProjectDocConfigBuilder projectBuilder, DocJavaField docField,
238-
CustomField customResponseField, CustomField customRequestField, boolean isResp, String simpleName) {
236+
boolean isResp, String simpleName, String fieldName) {
237+
CustomField.Key key = CustomField.Key.create(docField.getDeclaringClassName(), fieldName);
238+
239+
CustomField customResponseField = CustomField.nameEquals(key, projectBuilder.getCustomRespFieldMap());
240+
CustomField customRequestField = CustomField.nameEquals(key, projectBuilder.getCustomReqFieldMap());
241+
239242
CustomFieldInfo customFieldInfo = new CustomFieldInfo();
240243

241244
// ignore custom field, if true return quickly
@@ -244,6 +247,8 @@ protected static CustomFieldInfo getCustomFieldInfo(ProjectDocConfigBuilder proj
244247
return customFieldInfo;
245248
}
246249

250+
customFieldInfo.setCustomResponseField(customResponseField).setCustomRequestField(customRequestField);
251+
247252
// cover response value
248253
if (Objects.nonNull(customResponseField) && isResp && Objects.nonNull(customResponseField.getValue())
249254
&& JavaClassUtil.isTargetChildClass(simpleName, customResponseField.getOwnerClassName())) {

src/main/java/com/ly/doc/helper/JsonBuildHelper.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2024 smart-doc
2+
* Copyright (C) 2018-2025 smart-doc
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -24,12 +24,33 @@
2424
import com.ly.doc.constants.DocGlobalConstants;
2525
import com.ly.doc.constants.DocTags;
2626
import com.ly.doc.constants.JavaTypeConstants;
27-
import com.ly.doc.model.*;
28-
import com.ly.doc.utils.*;
27+
import com.ly.doc.model.ApiReturn;
28+
import com.ly.doc.model.CustomField;
29+
import com.ly.doc.model.CustomFieldInfo;
30+
import com.ly.doc.model.DocJavaField;
31+
import com.ly.doc.model.DocJavaMethod;
32+
import com.ly.doc.model.FieldJsonAnnotationInfo;
33+
import com.ly.doc.utils.DocClassUtil;
34+
import com.ly.doc.utils.DocUtil;
35+
import com.ly.doc.utils.JavaClassUtil;
36+
import com.ly.doc.utils.JavaClassValidateUtil;
37+
import com.ly.doc.utils.JavaFieldUtil;
38+
import com.ly.doc.utils.JsonUtil;
2939
import com.power.common.util.StringUtil;
30-
import com.thoughtworks.qdox.model.*;
31-
32-
import java.util.*;
40+
import com.thoughtworks.qdox.model.DocletTag;
41+
import com.thoughtworks.qdox.model.JavaAnnotation;
42+
import com.thoughtworks.qdox.model.JavaClass;
43+
import com.thoughtworks.qdox.model.JavaField;
44+
import com.thoughtworks.qdox.model.JavaMethod;
45+
import com.thoughtworks.qdox.model.JavaType;
46+
47+
import java.util.Collections;
48+
import java.util.HashMap;
49+
import java.util.LinkedHashMap;
50+
import java.util.List;
51+
import java.util.Map;
52+
import java.util.Objects;
53+
import java.util.Set;
3354

3455
import static com.ly.doc.constants.DocTags.IGNORE_RESPONSE_BODY_ADVICE;
3556

@@ -276,8 +297,6 @@ else if (JavaClassValidateUtil.isReactor(typeName)) {
276297
if (Boolean.TRUE.equals(annotationInfo.getIgnore())) {
277298
continue;
278299
}
279-
// the param type from @JsonFormat
280-
String fieldJsonFormatType = annotationInfo.getFieldJsonFormatType();
281300
// the param value from @JsonFormat
282301
String fieldJsonFormatValue = annotationInfo.getFieldJsonFormatValue();
283302
// has Annotation @JsonSerialize And using ToStringSerializer
@@ -288,12 +307,9 @@ else if (JavaClassValidateUtil.isReactor(typeName)) {
288307

289308
String typeSimpleName = docField.getTypeSimpleName();
290309
String fieldGicName = docField.getTypeGenericCanonicalName();
291-
CustomField.Key key = CustomField.Key.create(docField.getDeclaringClassName(), fieldName);
292310

293-
CustomField customResponseField = CustomField.nameEquals(key, projectBuilder.getCustomRespFieldMap());
294-
CustomField customRequestField = CustomField.nameEquals(key, projectBuilder.getCustomReqFieldMap());
295-
CustomFieldInfo customFieldInfo = getCustomFieldInfo(projectBuilder, docField, customResponseField,
296-
customRequestField, isResp, typeSimpleName);
311+
CustomFieldInfo customFieldInfo = getCustomFieldInfo(projectBuilder, docField, isResp, typeSimpleName,
312+
fieldName);
297313
// ignore custom field
298314
if (Boolean.TRUE.equals(customFieldInfo.getIgnore())) {
299315
continue;
@@ -322,6 +338,9 @@ else if (JavaClassValidateUtil.isReactor(typeName)) {
322338
: valueByTypeAndFieldName;
323339
}
324340
}
341+
342+
CustomField customResponseField = customFieldInfo.getCustomResponseField();
343+
CustomField customRequestField = customFieldInfo.getCustomRequestField();
325344
if (Objects.nonNull(customRequestField) && !isResp
326345
&& typeName.equals(customRequestField.getOwnerClassName())) {
327346
JavaFieldUtil.buildCustomField(result, typeSimpleName, customRequestField);

src/main/java/com/ly/doc/helper/ParamsBuildHelper.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* smart-doc https://github.com/smart-doc-group/smart-doc
33
*
4-
* Copyright (C) 2018-2024 smart-doc
4+
* Copyright (C) 2018-2025 smart-doc
55
*
66
* Licensed to the Apache Software Foundation (ASF) under one
77
* or more contributor license agreements. See the NOTICE file
@@ -32,7 +32,6 @@
3232
import com.ly.doc.model.ApiConfig;
3333
import com.ly.doc.model.ApiDataDictionary;
3434
import com.ly.doc.model.ApiParam;
35-
import com.ly.doc.model.CustomField;
3635
import com.ly.doc.model.CustomFieldInfo;
3736
import com.ly.doc.model.DocJavaField;
3837
import com.ly.doc.model.FieldJsonAnnotationInfo;
@@ -271,13 +270,9 @@ private static List<ApiParam> processFields(String className, String pre, int le
271270
}
272271

273272
boolean strRequired = false;
274-
CustomField.Key key = CustomField.Key.create(docField.getDeclaringClassName(), fieldName);
275273

276-
CustomField customResponseField = CustomField.nameEquals(key, projectBuilder.getCustomRespFieldMap());
277-
CustomField customRequestField = CustomField.nameEquals(key, projectBuilder.getCustomReqFieldMap());
278-
279-
CustomFieldInfo customFieldInfo = getCustomFieldInfo(projectBuilder, docField, customResponseField,
280-
customRequestField, isResp, simpleName);
274+
CustomFieldInfo customFieldInfo = getCustomFieldInfo(projectBuilder, docField, isResp, typeSimpleName,
275+
fieldName);
281276
// ignore custom field
282277
if (Boolean.TRUE.equals(customFieldInfo.getIgnore())) {
283278
continue;

src/main/java/com/ly/doc/model/CustomFieldInfo.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/*
2+
* smart-doc https://github.com/smart-doc-group/smart-doc
3+
*
4+
* Copyright (C) 2018-2025 smart-doc
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one
7+
* or more contributor license agreements. See the NOTICE file
8+
* distributed with this work for additional information
9+
* regarding copyright ownership. The ASF licenses this file
10+
* to you under the Apache License, Version 2.0 (the
11+
* "License"); you may not use this file except in compliance
12+
* with the License. You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing,
17+
* software distributed under the License is distributed on an
18+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19+
* KIND, either express or implied. See the License for the
20+
* specific language governing permissions and limitations
21+
* under the License.
22+
*/
123
package com.ly.doc.model;
224

325
import java.io.Serializable;
@@ -15,6 +37,16 @@ public class CustomFieldInfo implements Serializable {
1537
*/
1638
private static final long serialVersionUID = -7310122325722122250L;
1739

40+
/**
41+
* custom response field
42+
*/
43+
private CustomField customResponseField;
44+
45+
/**
46+
* custom request field
47+
*/
48+
private CustomField customRequestField;
49+
1850
/**
1951
* ignore
2052
*/
@@ -40,6 +72,24 @@ public class CustomFieldInfo implements Serializable {
4072
*/
4173
private String comment;
4274

75+
public CustomField getCustomResponseField() {
76+
return customResponseField;
77+
}
78+
79+
public CustomFieldInfo setCustomResponseField(CustomField customResponseField) {
80+
this.customResponseField = customResponseField;
81+
return this;
82+
}
83+
84+
public CustomField getCustomRequestField() {
85+
return customRequestField;
86+
}
87+
88+
public CustomFieldInfo setCustomRequestField(CustomField customRequestField) {
89+
this.customRequestField = customRequestField;
90+
return this;
91+
}
92+
4393
public Boolean getIgnore() {
4494
return ignore;
4595
}

0 commit comments

Comments
 (0)