Skip to content

Commit db0c8c7

Browse files
committed
cleanup
1 parent f73fdd2 commit db0c8c7

4 files changed

Lines changed: 98 additions & 123 deletions

File tree

src/main/java/it/aboutbits/springboot/toolbox/swagger/SwaggerMeta.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
@Setter
1010
@JsonInclude(JsonInclude.Include.NON_NULL)
1111
public class SwaggerMeta {
12-
private String originalTypeFqn;
12+
private String originalTypeFqn = null;
1313
@JsonProperty("isIdentity")
14-
private boolean isIdentity = false;
14+
private Boolean isIdentity = null;
1515
@JsonProperty("isNestedStructure")
16-
private boolean isNestedStructure = false;
16+
private Boolean isNestedStructure = null;
1717
@JsonProperty("isMap")
18-
private boolean isMap = false;
19-
private String mapKeyTypeFqn;
18+
private Boolean isMap = null;
19+
private String mapKeyTypeFqn = null;
2020
}

src/main/java/it/aboutbits/springboot/toolbox/swagger/SwaggerMetaUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static String setMapKeyTypeFqn(@Nullable String currentMeta, @NonNull Str
2525
@SneakyThrows(JsonProcessingException.class)
2626
public static String setIsMap(@Nullable String currentMeta, boolean value) {
2727
var meta = getSwaggerMeta(currentMeta);
28-
meta.setMap(value);
28+
meta.setIsMap(!value ? null : true);
2929

3030
return OBJECT_MAPPER.writeValueAsString(meta);
3131
}
@@ -41,15 +41,15 @@ public static String setOriginalTypeFqn(@Nullable String currentMeta, @NonNull S
4141
@SneakyThrows(JsonProcessingException.class)
4242
public static String setIsIdentity(@Nullable String currentMeta, boolean value) {
4343
var meta = getSwaggerMeta(currentMeta);
44-
meta.setIdentity(value);
44+
meta.setIsIdentity(!value ? null : true);
4545

4646
return OBJECT_MAPPER.writeValueAsString(meta);
4747
}
4848

4949
@SneakyThrows(JsonProcessingException.class)
5050
public static String setIsNestedStructure(@Nullable String currentMeta, boolean value) {
5151
var meta = getSwaggerMeta(currentMeta);
52-
meta.setNestedStructure(value);
52+
meta.setIsNestedStructure(!value ? null : true);
5353

5454
return OBJECT_MAPPER.writeValueAsString(meta);
5555
}

src/main/java/it/aboutbits/springboot/toolbox/swagger/type/CustomTypeModelConverter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ public Schema<?> resolve(
3030
if (CustomType.class.isAssignableFrom(clazz)) {
3131
var constructor = RecordReflectionUtil.getCanonicalConstructor(clazz);
3232
var wrappedType = constructor.getParameters()[0].getType();
33-
var isIdentity = false;
34-
35-
if (EntityId.class.isAssignableFrom(clazz)) {
36-
isIdentity = true;
37-
}
38-
3933

4034
if (Short.class.isAssignableFrom(wrappedType)) {
4135
result = context.resolve(new AnnotatedType(Short.TYPE));
@@ -66,6 +60,8 @@ public Schema<?> resolve(
6660
}
6761

6862
if (result != null) {
63+
var isIdentity = EntityId.class.isAssignableFrom(clazz);
64+
6965
var description = SwaggerMetaUtil.setOriginalTypeFqn(
7066
result.getDescription(),
7167
((Class<?>) type).getName()

src/main/java/it/aboutbits/springboot/toolbox/swagger/type/CustomTypePropertyCustomizer.java

Lines changed: 88 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import it.aboutbits.springboot.toolbox.type.CustomType;
99
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
1010
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
11-
import lombok.NonNull;
1211
import lombok.extern.slf4j.Slf4j;
1312
import org.springdoc.core.customizers.PropertyCustomizer;
1413

@@ -24,123 +23,103 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
2423
if (type instanceof SimpleType simpleType) {
2524
var rawClass = simpleType.getRawClass();
2625

27-
// We set the enum name as the description because swagger treats each usage as a new enum.
28-
// This way we can preserve the information about the original enum type.
29-
if (rawClass.isEnum()) {
30-
var displayName = rawClass.getName(); // .replace(rawClass.getPackage().getName() + ".", "");
31-
property.setDescription(SwaggerMetaUtil.setOriginalTypeFqn(property.getDescription(), displayName));
26+
if (rawClass.getName().contains(".") && !rawClass.getName().startsWith("java.")) {
27+
property.setDescription(SwaggerMetaUtil.setOriginalTypeFqn(
28+
property.getDescription(),
29+
rawClass.getName()
30+
));
3231
}
33-
}
3432

35-
if (type instanceof SimpleType simpleType && CustomType.class.isAssignableFrom(simpleType.getRawClass())) {
36-
var rawClass = simpleType.getRawClass();
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+
}
3741

38-
var displayName = rawClass.getName(); // .getSimpleName();
39-
var isIdentity = false;
42+
var isIdentity = EntityId.class.isAssignableFrom(rawClass);
4043

41-
Class<?> wrappedType;
42-
if (EntityId.class.isAssignableFrom(rawClass)) {
43-
isIdentity = true;
44-
displayName = resolveEntityIdDisplayName(rawClass);
45-
}
46-
47-
if (rawClass.equals(EntityId.class)) {
48-
wrappedType = simpleType.getBindings().getBoundType(0).getRawClass();
49-
} else {
50-
var constructor = RecordReflectionUtil.getCanonicalConstructor(rawClass);
51-
wrappedType = constructor.getParameters()[0].getType();
52-
}
44+
var description = SwaggerMetaUtil.setIsIdentity(property.getDescription(), isIdentity);
5345

54-
var description = SwaggerMetaUtil.setOriginalTypeFqn(property.getDescription(), displayName);
55-
description = SwaggerMetaUtil.setIsIdentity(description, isIdentity);
56-
description = SwaggerMetaUtil.setIsIdentity(description, isIdentity);
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+
}
57102

58-
if (Short.class.isAssignableFrom(wrappedType)) {
59-
property.type("integer");
60-
property.format("");
61-
property.setDescription(description);
62-
property.setProperties(null);
63-
property.set$ref(null);
64-
return property;
65-
}
66-
if (Integer.class.isAssignableFrom(wrappedType)) {
67-
property.type("integer");
68-
property.format("int32");
69-
property.setDescription(description);
70-
property.setProperties(null);
71-
property.set$ref(null);
72-
return property;
73-
}
74-
if (Long.class.isAssignableFrom(wrappedType)) {
75-
property.type("integer");
76-
property.format("int64");
77-
property.setDescription(description);
78-
property.setProperties(null);
79-
property.set$ref(null);
80-
return property;
81-
}
82-
if (BigInteger.class.isAssignableFrom(wrappedType)) {
83-
property.type("integer");
84-
property.format("int64");
85-
property.setDescription(description);
86-
property.setProperties(null);
87-
property.set$ref(null);
88-
return property;
89-
}
90-
if (Float.class.isAssignableFrom(wrappedType)) {
91-
property.type("number");
92-
property.format("float");
93-
property.setDescription(description);
94-
property.setProperties(null);
95-
property.set$ref(null);
96-
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());
97120
}
98-
if (Double.class.isAssignableFrom(wrappedType)) {
99-
property.type("number");
100-
property.format("double");
101-
property.setDescription(description);
102-
property.setProperties(null);
103-
property.set$ref(null);
104-
return property;
105-
}
106-
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
107-
property.type("number");
108-
property.format("");
109-
property.setDescription(description);
110-
property.setProperties(null);
111-
property.set$ref(null);
112-
return property;
113-
}
114-
115-
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
116-
property.type("number");
117-
property.format("");
118-
property.setDescription(description);
119-
property.setProperties(null);
120-
property.set$ref(null);
121-
return property;
122-
}
123-
if (String.class.isAssignableFrom(wrappedType)) {
124-
property.type("string");
125-
property.format(null);
126-
property.setDescription(description);
127-
property.setProperties(null);
128-
property.set$ref(null);
129-
return property;
130-
}
131-
log.warn("Property {} of type WrappedValue: Can not resolve parameter type!", property.getName());
132121
}
133122

134123
return property;
135124
}
136-
137-
@NonNull
138-
private static String resolveEntityIdDisplayName(Class<?> rawClass) {
139-
return rawClass.getName();
140-
/* var parent = rawClass.getEnclosingClass();
141-
if (parent != null) {
142-
return parent.getSimpleName() + "." + rawClass.getSimpleName();
143-
}
144-
return rawClass.getSimpleName();*/
145-
}
146125
}

0 commit comments

Comments
 (0)