28
28
import java .beans .Introspector ;
29
29
import java .beans .PropertyDescriptor ;
30
30
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 ;
32
38
33
39
import com .fasterxml .jackson .databind .JavaType ;
40
+ import com .fasterxml .jackson .databind .ObjectMapper ;
41
+ import com .fasterxml .jackson .databind .introspect .SimpleMixInResolver ;
34
42
import io .swagger .v3 .core .converter .AnnotatedType ;
35
43
import io .swagger .v3 .core .converter .ModelConverter ;
36
44
import io .swagger .v3 .core .converter .ModelConverterContext ;
45
+ import io .swagger .v3 .core .converter .ModelConverterContextImpl ;
37
46
import io .swagger .v3 .core .util .AnnotationsUtils ;
47
+ import io .swagger .v3 .oas .annotations .media .SchemaProperty ;
38
48
import io .swagger .v3 .oas .models .media .Schema ;
39
49
import org .apache .commons .lang3 .StringUtils ;
40
50
import org .apache .commons .lang3 .reflect .FieldUtils ;
48
58
49
59
/**
50
60
* The type Javadoc property customizer.
61
+ *
51
62
* @author bnasslahsen
52
63
*/
53
64
public record JavadocPropertyCustomizer (JavadocProvider javadocProvider ,
54
65
ObjectMapperProvider objectMapperProvider )
55
66
implements ModelConverter {
56
67
57
68
private static final Logger LOGGER = LoggerFactory .getLogger (DelegatingMethodParameter .class );
69
+
58
70
/**
59
71
* Instantiates a new Javadoc property customizer.
60
72
*
61
- * @param javadocProvider the javadoc provider
73
+ * @param javadocProvider the javadoc provider
62
74
* @param objectMapperProvider the object mapper provider
63
75
*/
64
76
public JavadocPropertyCustomizer {
@@ -67,9 +79,9 @@ public record JavadocPropertyCustomizer(JavadocProvider javadocProvider,
67
79
/**
68
80
* Resolve schema.
69
81
*
70
- * @param type the type
82
+ * @param type the type
71
83
* @param context the context
72
- * @param chain the chain
84
+ * @param chain the chain
73
85
* @return the schema
74
86
*/
75
87
@ Override
@@ -83,18 +95,30 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
83
95
List <PropertyDescriptor > clsProperties = new ArrayList <>();
84
96
try {
85
97
clsProperties = Arrays .asList (Introspector .getBeanInfo (cls ).getPropertyDescriptors ());
86
- } catch (IntrospectionException ignored ) {
98
+ }
99
+ catch (IntrospectionException ignored ) {
87
100
LOGGER .warn (ignored .getMessage ());
88
101
}
89
102
if (!CollectionUtils .isEmpty (fields ) || !CollectionUtils .isEmpty (clsProperties )) {
90
103
if (!type .isSchemaProperty ()) {
91
104
Schema existingSchema = context .resolve (type );
92
- setJavadocDescription (cls , fields , clsProperties , existingSchema );
105
+ setJavadocDescription (cls , fields , clsProperties , existingSchema , false );
93
106
}
94
107
else if (resolvedSchema != null && resolvedSchema .get$ref () != null && resolvedSchema .get$ref ().contains (AnnotationsUtils .COMPONENTS_REF )) {
95
108
String schemaName = resolvedSchema .get$ref ().substring (21 );
96
109
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
+ }
98
122
}
99
123
}
100
124
return resolvedSchema ;
@@ -106,14 +130,15 @@ else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedS
106
130
/**
107
131
* Sets javadoc description.
108
132
*
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
113
138
*/
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 ) {
115
140
if (existingSchema != null ) {
116
- if (StringUtils .isBlank (existingSchema .getDescription ())) {
141
+ if (StringUtils .isBlank (existingSchema .getDescription ()) && ! isProcessedType ) {
117
142
String classJavadoc = javadocProvider .getClassJavadoc (cls );
118
143
if (StringUtils .isNotBlank (classJavadoc )) {
119
144
existingSchema .setDescription (classJavadoc );
0 commit comments