44import io .swagger .v3 .core .converter .AnnotatedType ;
55import io .swagger .v3 .oas .models .media .Schema ;
66import it .aboutbits .springboot .toolbox .reflection .util .RecordReflectionUtil ;
7+ import it .aboutbits .springboot .toolbox .swagger .SwaggerMetaUtil ;
78import it .aboutbits .springboot .toolbox .type .CustomType ;
89import it .aboutbits .springboot .toolbox .type .ScaledBigDecimal ;
910import it .aboutbits .springboot .toolbox .type .identity .EntityId ;
10- import lombok .NonNull ;
1111import lombok .extern .slf4j .Slf4j ;
1212import org .springdoc .core .customizers .PropertyCustomizer ;
1313
@@ -23,116 +23,103 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
2323 if (type instanceof SimpleType simpleType ) {
2424 var rawClass = simpleType .getRawClass ();
2525
26- // We set the enum name as the description because swagger treats each usage as a new enum.
27- // This way we can preserve the information about the original enum type.
28- if ( rawClass . isEnum ()) {
29- var displayName = rawClass .getCanonicalName (). replace ( rawClass . getPackage (). getName () + "." , "" );
30- property . setDescription ( displayName );
26+ if ( rawClass . getName (). contains ( "." ) && ! rawClass . getName (). startsWith ( "java." )) {
27+ property . setDescription ( SwaggerMetaUtil . setOriginalTypeFqn (
28+ property . getDescription (),
29+ rawClass .getName ()
30+ ) );
3131 }
32- }
33-
34- if (type instanceof SimpleType simpleType && CustomType .class .isAssignableFrom (simpleType .getRawClass ())) {
35- var rawClass = simpleType .getRawClass ();
3632
37- var displayName = rawClass .getSimpleName ();
33+ if (CustomType .class .isAssignableFrom (simpleType .getRawClass ())) {
34+ Class <?> wrappedType ;
35+ if (rawClass .equals (EntityId .class )) {
36+ wrappedType = simpleType .getBindings ().getBoundType (0 ).getRawClass ();
37+ } else {
38+ var constructor = RecordReflectionUtil .getCanonicalConstructor (rawClass );
39+ wrappedType = constructor .getParameters ()[0 ].getType ();
40+ }
3841
39- Class <?> wrappedType ;
40- if (EntityId .class .isAssignableFrom (rawClass )) {
41- displayName = resolveEntityIdDisplayName (rawClass );
42- }
42+ var isIdentity = EntityId .class .isAssignableFrom (rawClass );
4343
44- if (rawClass .equals (EntityId .class )) {
45- wrappedType = simpleType .getBindings ().getBoundType (0 ).getRawClass ();
46- } else {
47- var constructor = RecordReflectionUtil .getCanonicalConstructor (rawClass );
48- wrappedType = constructor .getParameters ()[0 ].getType ();
49- }
44+ var description = SwaggerMetaUtil .setIsIdentity (property .getDescription (), isIdentity );
5045
51- if (Short .class .isAssignableFrom (wrappedType )) {
52- property .type ("integer" );
53- property .format ("" );
54- property .setDescription (displayName );
55- property .setProperties (null );
56- property .set$ref (null );
57- return property ;
58- }
59- if (Integer .class .isAssignableFrom (wrappedType )) {
60- property .type ("integer" );
61- property .format ("int32" );
62- property .setDescription (displayName );
63- property .setProperties (null );
64- property .set$ref (null );
65- return property ;
66- }
67- if (Long .class .isAssignableFrom (wrappedType )) {
68- property .type ("integer" );
69- property .format ("int64" );
70- property .setDescription (displayName );
71- property .setProperties (null );
72- property .set$ref (null );
73- return property ;
74- }
75- if (BigInteger .class .isAssignableFrom (wrappedType )) {
76- property .type ("integer" );
77- property .format ("int64" );
78- property .setDescription (displayName );
79- property .setProperties (null );
80- property .set$ref (null );
81- return property ;
82- }
83- if (Float .class .isAssignableFrom (wrappedType )) {
84- property .type ("number" );
85- property .format ("float" );
86- property .setDescription (displayName );
87- property .setProperties (null );
88- property .set$ref (null );
89- return property ;
90- }
91- if (Double .class .isAssignableFrom (wrappedType )) {
92- property .type ("number" );
93- property .format ("double" );
94- property .setDescription (displayName );
95- property .setProperties (null );
96- property .set$ref (null );
97- return property ;
98- }
99- if (BigDecimal .class .isAssignableFrom (wrappedType )) {
100- property .type ("number" );
101- property .format ("" );
102- property .setDescription (displayName );
103- property .setProperties (null );
104- property .set$ref (null );
105- return property ;
106- }
46+ if (Short .class .isAssignableFrom (wrappedType )) {
47+ property .type ("integer" );
48+ property .format ("" );
49+ property .setDescription (description );
50+ property .setProperties (null );
51+ property .set$ref (null );
52+ return property ;
53+ }
54+ if (Integer .class .isAssignableFrom (wrappedType )) {
55+ property .type ("integer" );
56+ property .format ("int32" );
57+ property .setDescription (description );
58+ property .setProperties (null );
59+ property .set$ref (null );
60+ return property ;
61+ }
62+ if (Long .class .isAssignableFrom (wrappedType )) {
63+ property .type ("integer" );
64+ property .format ("int64" );
65+ property .setDescription (description );
66+ property .setProperties (null );
67+ property .set$ref (null );
68+ return property ;
69+ }
70+ if (BigInteger .class .isAssignableFrom (wrappedType )) {
71+ property .type ("integer" );
72+ property .format ("int64" );
73+ property .setDescription (description );
74+ property .setProperties (null );
75+ property .set$ref (null );
76+ return property ;
77+ }
78+ if (Float .class .isAssignableFrom (wrappedType )) {
79+ property .type ("number" );
80+ property .format ("float" );
81+ property .setDescription (description );
82+ property .setProperties (null );
83+ property .set$ref (null );
84+ return property ;
85+ }
86+ if (Double .class .isAssignableFrom (wrappedType )) {
87+ property .type ("number" );
88+ property .format ("double" );
89+ property .setDescription (description );
90+ property .setProperties (null );
91+ property .set$ref (null );
92+ return property ;
93+ }
94+ if (BigDecimal .class .isAssignableFrom (wrappedType )) {
95+ property .type ("number" );
96+ property .format ("" );
97+ property .setDescription (description );
98+ property .setProperties (null );
99+ property .set$ref (null );
100+ return property ;
101+ }
107102
108- if (ScaledBigDecimal .class .isAssignableFrom (wrappedType )) {
109- property .type ("number" );
110- property .format ("" );
111- property .setDescription (displayName );
112- property .setProperties (null );
113- property .set$ref (null );
114- return property ;
103+ if (ScaledBigDecimal .class .isAssignableFrom (wrappedType )) {
104+ property .type ("number" );
105+ property .format ("" );
106+ property .setDescription (description );
107+ property .setProperties (null );
108+ property .set$ref (null );
109+ return property ;
110+ }
111+ if (String .class .isAssignableFrom (wrappedType )) {
112+ property .type ("string" );
113+ property .format (null );
114+ property .setDescription (description );
115+ property .setProperties (null );
116+ property .set$ref (null );
117+ return property ;
118+ }
119+ log .warn ("Property {} of type WrappedValue: Can not resolve parameter type!" , property .getName ());
115120 }
116- if (String .class .isAssignableFrom (wrappedType )) {
117- property .type ("string" );
118- property .format (null );
119- property .setDescription (displayName );
120- property .setProperties (null );
121- property .set$ref (null );
122- return property ;
123- }
124- log .warn ("Property {} of type WrappedValue: Can not resolve parameter type!" , property .getName ());
125121 }
126122
127123 return property ;
128124 }
129-
130- @ NonNull
131- private static String resolveEntityIdDisplayName (Class <?> rawClass ) {
132- var parent = rawClass .getEnclosingClass ();
133- if (parent != null ) {
134- return parent .getSimpleName () + "." + rawClass .getSimpleName ();
135- }
136- return rawClass .getSimpleName ();
137- }
138125}
0 commit comments