Skip to content

Commit d51b98a

Browse files
committed
javadoc for class attribute ignored when in EntityModel. Fixes #2320
1 parent b0b94c3 commit d51b98a

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/customizers/JavadocPropertyCustomizer.java

+38-13
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,23 @@
2828
import java.beans.Introspector;
2929
import java.beans.PropertyDescriptor;
3030
import java.lang.reflect.Field;
31-
import java.util.*;
31+
import java.util.ArrayList;
32+
import java.util.Arrays;
33+
import java.util.Iterator;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Optional;
37+
import java.util.Set;
3238

3339
import com.fasterxml.jackson.databind.JavaType;
40+
import com.fasterxml.jackson.databind.ObjectMapper;
41+
import com.fasterxml.jackson.databind.introspect.SimpleMixInResolver;
3442
import io.swagger.v3.core.converter.AnnotatedType;
3543
import io.swagger.v3.core.converter.ModelConverter;
3644
import io.swagger.v3.core.converter.ModelConverterContext;
45+
import io.swagger.v3.core.converter.ModelConverterContextImpl;
3746
import io.swagger.v3.core.util.AnnotationsUtils;
47+
import io.swagger.v3.oas.annotations.media.SchemaProperty;
3848
import io.swagger.v3.oas.models.media.Schema;
3949
import org.apache.commons.lang3.StringUtils;
4050
import org.apache.commons.lang3.reflect.FieldUtils;
@@ -48,17 +58,19 @@
4858

4959
/**
5060
* The type Javadoc property customizer.
61+
*
5162
* @author bnasslahsen
5263
*/
5364
public record JavadocPropertyCustomizer(JavadocProvider javadocProvider,
5465
ObjectMapperProvider objectMapperProvider)
5566
implements ModelConverter {
5667

5768
private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingMethodParameter.class);
69+
5870
/**
5971
* Instantiates a new Javadoc property customizer.
6072
*
61-
* @param javadocProvider the javadoc provider
73+
* @param javadocProvider the javadoc provider
6274
* @param objectMapperProvider the object mapper provider
6375
*/
6476
public JavadocPropertyCustomizer {
@@ -67,9 +79,9 @@ public record JavadocPropertyCustomizer(JavadocProvider javadocProvider,
6779
/**
6880
* Resolve schema.
6981
*
70-
* @param type the type
82+
* @param type the type
7183
* @param context the context
72-
* @param chain the chain
84+
* @param chain the chain
7385
* @return the schema
7486
*/
7587
@Override
@@ -83,18 +95,30 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
8395
List<PropertyDescriptor> clsProperties = new ArrayList<>();
8496
try {
8597
clsProperties = Arrays.asList(Introspector.getBeanInfo(cls).getPropertyDescriptors());
86-
} catch (IntrospectionException ignored) {
98+
}
99+
catch (IntrospectionException ignored) {
87100
LOGGER.warn(ignored.getMessage());
88101
}
89102
if (!CollectionUtils.isEmpty(fields) || !CollectionUtils.isEmpty(clsProperties)) {
90103
if (!type.isSchemaProperty()) {
91104
Schema existingSchema = context.resolve(type);
92-
setJavadocDescription(cls, fields, clsProperties, existingSchema);
105+
setJavadocDescription(cls, fields, clsProperties, existingSchema, false);
93106
}
94107
else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedSchema.get$ref().contains(AnnotationsUtils.COMPONENTS_REF)) {
95108
String schemaName = resolvedSchema.get$ref().substring(21);
96109
Schema existingSchema = context.getDefinedModels().get(schemaName);
97-
setJavadocDescription(cls, fields, clsProperties, existingSchema);
110+
setJavadocDescription(cls, fields, clsProperties, existingSchema, false);
111+
}
112+
else {
113+
try {
114+
Field processedTypesField = FieldUtils.getDeclaredField(ModelConverterContextImpl.class, "processedTypes", true);
115+
Set<AnnotatedType> processedType = (Set<AnnotatedType>) processedTypesField.get(context);
116+
if(processedType.contains(type))
117+
setJavadocDescription(cls, fields, clsProperties, resolvedSchema, true);
118+
}
119+
catch (IllegalAccessException e) {
120+
LOGGER.warn(e.getMessage());
121+
}
98122
}
99123
}
100124
return resolvedSchema;
@@ -106,14 +130,15 @@ else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedS
106130
/**
107131
* Sets javadoc description.
108132
*
109-
* @param cls the cls
110-
* @param fields the fields
111-
* @param clsProperties the bean properties of cls
112-
* @param existingSchema the existing schema
133+
* @param cls the cls
134+
* @param fields the fields
135+
* @param clsProperties the bean properties of cls
136+
* @param existingSchema the existing schema
137+
* @param isProcessedType the is processed type
113138
*/
114-
public void setJavadocDescription(Class<?> cls, List<Field> fields, List<PropertyDescriptor> clsProperties, Schema existingSchema) {
139+
public void setJavadocDescription(Class<?> cls, List<Field> fields, List<PropertyDescriptor> clsProperties, Schema existingSchema, boolean isProcessedType) {
115140
if (existingSchema != null) {
116-
if (StringUtils.isBlank(existingSchema.getDescription())) {
141+
if (StringUtils.isBlank(existingSchema.getDescription()) && !isProcessedType) {
117142
String classJavadoc = javadocProvider.getClassJavadoc(cls);
118143
if (StringUtils.isNotBlank(classJavadoc)) {
119144
existingSchema.setDescription(classJavadoc);

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/java/test/org/springdoc/api/app172/JavadocPropertyCustomizerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void ifRecordObjectShouldGetField() throws IOException, ClassNotFoundException,
128128
.addProperty("name", new StringSchema().name("name"));
129129

130130
List<PropertyDescriptor> propertyDescriptors = Arrays.asList(Introspector.getBeanInfo(cls).getPropertyDescriptors());
131-
javadocPropertyCustomizer.setJavadocDescription(cls, fields, propertyDescriptors, existingSchema);
131+
javadocPropertyCustomizer.setJavadocDescription(cls, fields, propertyDescriptors, existingSchema,false);
132132

133133
assertEquals("Record Object", existingSchema.getDescription());
134134
Map<String, Schema> properties = existingSchema.getProperties();

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/resources/results/app105-3.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,12 @@
546546
"properties": {
547547
"id": {
548548
"type": "integer",
549+
"description": "The Id.",
549550
"format": "int64"
550551
},
551552
"name": {
552-
"type": "string"
553+
"type": "string",
554+
"description": "The Name."
553555
}
554556
}
555557
},
@@ -623,10 +625,12 @@
623625
"properties": {
624626
"id": {
625627
"type": "integer",
628+
"description": "The Id.",
626629
"format": "int64"
627630
},
628631
"name": {
629-
"type": "string"
632+
"type": "string",
633+
"description": "The Name."
630634
}
631635
}
632636
}

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/resources/results/app2.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,12 @@
11561156
"properties": {
11571157
"id": {
11581158
"type": "integer",
1159+
"description": "The Id.",
11591160
"format": "int64"
11601161
},
11611162
"name": {
1162-
"type": "string"
1163+
"type": "string",
1164+
"description": "The Name."
11631165
}
11641166
}
11651167
},
@@ -1214,10 +1216,12 @@
12141216
"properties": {
12151217
"id": {
12161218
"type": "integer",
1219+
"description": "The Id.",
12171220
"format": "int64"
12181221
},
12191222
"name": {
1220-
"type": "string"
1223+
"type": "string",
1224+
"description": "The Name."
12211225
}
12221226
}
12231227
},

0 commit comments

Comments
 (0)