Skip to content

Commit 2b2ea25

Browse files
committed
update swagger tooling for code generator
1 parent caec21a commit 2b2ea25

15 files changed

Lines changed: 474 additions & 159 deletions

src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/swagger/RegisterCustomTypesWithSwagger.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class SwaggerMeta {
1212
private String originalTypeFqn = null;
1313
@JsonProperty("isIdentity")
1414
private Boolean isIdentity = null;
15+
@JsonProperty("isCustomType")
16+
private Boolean isCustomType = null;
1517
@JsonProperty("isNestedStructure")
1618
private Boolean isNestedStructure = null;
1719
@JsonProperty("isMap")

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public static String setIsIdentity(@Nullable String currentMeta, boolean value)
4646
return OBJECT_MAPPER.writeValueAsString(meta);
4747
}
4848

49+
@SneakyThrows(JsonProcessingException.class)
50+
public static String setIsCustomType(@Nullable String currentMeta, boolean value) {
51+
var meta = getSwaggerMeta(currentMeta);
52+
meta.setIsCustomType(!value ? null : true);
53+
54+
return OBJECT_MAPPER.writeValueAsString(meta);
55+
}
56+
4957
@SneakyThrows(JsonProcessingException.class)
5058
public static String setIsNestedStructure(@Nullable String currentMeta, boolean value) {
5159
var meta = getSwaggerMeta(currentMeta);

src/main/java/it/aboutbits/springboot/toolbox/swagger/annotations/SwaggerScopedAuth.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/authorization_docs/AuthorizationDescriptor.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/java/it/aboutbits/springboot/toolbox/swagger/type/CustomTypeModelConverter.java renamed to src/main/java/it/aboutbits/springboot/toolbox/swagger/customization/custom_type/CustomTypeModelConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package it.aboutbits.springboot.toolbox.swagger.type;
1+
package it.aboutbits.springboot.toolbox.swagger.customization.custom_type;
22

33
import io.swagger.v3.core.converter.AnnotatedType;
44
import io.swagger.v3.core.converter.ModelConverter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package it.aboutbits.springboot.toolbox.swagger.customization.custom_type;
2+
3+
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.media.Schema;
5+
import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil;
6+
import it.aboutbits.springboot.toolbox.type.CustomType;
7+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springdoc.core.customizers.OpenApiCustomizer;
10+
11+
import java.util.Optional;
12+
13+
@Slf4j
14+
public class CustomTypeOpenApiCustomizerForCodeGenerator implements OpenApiCustomizer {
15+
@Override
16+
public void customise(OpenAPI openApi) {
17+
openApi.getComponents().getSchemas().forEach(this::updateSchema);
18+
}
19+
20+
public Optional<Class<?>> getClassFromSchemaReference(String schemaRef) {
21+
try {
22+
var className = schemaRef.replace("#/components/schemas/", "");
23+
return Optional.of(Class.forName(className));
24+
} catch (ClassNotFoundException e) {
25+
return Optional.empty();
26+
}
27+
}
28+
29+
private void updateSchema(String fqn, Schema<?> schema) {
30+
var type = getClassFromSchemaReference(fqn);
31+
32+
if (type.isEmpty()) {
33+
log.debug("Can not resolve type for schema reference: {}", fqn);
34+
return;
35+
}
36+
37+
var clazz = type.get();
38+
39+
if (CustomType.class.isAssignableFrom(clazz)) {
40+
41+
var isIdentity = EntityId.class.isAssignableFrom(clazz);
42+
43+
var description = SwaggerMetaUtil.setOriginalTypeFqn(
44+
schema.getDescription(),
45+
fqn
46+
);
47+
description = SwaggerMetaUtil.setIsIdentity(description, isIdentity);
48+
description = SwaggerMetaUtil.setIsCustomType(description, true);
49+
schema.setDescription(description);
50+
}
51+
52+
}
53+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package it.aboutbits.springboot.toolbox.swagger.customization.custom_type;
2+
3+
import io.swagger.v3.oas.models.media.ArraySchema;
4+
import io.swagger.v3.oas.models.media.ObjectSchema;
5+
import io.swagger.v3.oas.models.parameters.Parameter;
6+
import it.aboutbits.springboot.toolbox.reflection.util.CustomTypeReflectionUtil;
7+
import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil;
8+
import it.aboutbits.springboot.toolbox.type.CustomType;
9+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
10+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
11+
import lombok.SneakyThrows;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.springdoc.core.customizers.ParameterCustomizer;
14+
import org.springframework.core.MethodParameter;
15+
16+
import java.math.BigDecimal;
17+
import java.math.BigInteger;
18+
import java.util.Optional;
19+
import java.util.UUID;
20+
21+
@Slf4j
22+
public class CustomTypeParameterCustomizer implements ParameterCustomizer {
23+
24+
public Optional<Class<?>> getClassFromSchemaReference(String schemaRef) {
25+
try {
26+
var className = schemaRef.replace("#/components/schemas/", "");
27+
return Optional.of(Class.forName(className));
28+
} catch (ClassNotFoundException e) {
29+
// Log the error if needed
30+
return Optional.empty();
31+
}
32+
}
33+
34+
35+
@SuppressWarnings("checkstyle:MethodLength")
36+
@Override
37+
@SneakyThrows(NoSuchMethodException.class)
38+
public Parameter customize(Parameter parameter, MethodParameter methodParameter) {
39+
if (parameter == null) {
40+
return null;
41+
}
42+
43+
String schemaRef = null;
44+
var property = parameter.getSchema();
45+
46+
if (parameter.getSchema() instanceof ArraySchema
47+
&& parameter.getSchema().getItems() != null
48+
&& parameter.getSchema().getItems().get$ref() != null
49+
) {
50+
schemaRef = parameter.getSchema().getItems().get$ref();
51+
property = parameter.getSchema().getItems();
52+
}
53+
if (parameter.getSchema() instanceof ObjectSchema
54+
&& parameter.getSchema().get$ref() != null
55+
) {
56+
schemaRef = parameter.getSchema().getItems().get$ref();
57+
}
58+
59+
if (schemaRef == null) {
60+
return parameter;
61+
}
62+
63+
var optionalClass = getClassFromSchemaReference(schemaRef);
64+
65+
if (optionalClass.isEmpty()) {
66+
log.error("Can not resolve type for schema reference: {}", schemaRef);
67+
return parameter; // throw new RuntimeException();
68+
}
69+
70+
71+
var rawClass = optionalClass.get();
72+
73+
if (rawClass.getName().contains(".") && !rawClass.getName().startsWith("java.")) {
74+
property.setDescription(SwaggerMetaUtil.setOriginalTypeFqn(
75+
property.getDescription(),
76+
rawClass.getName()
77+
));
78+
}
79+
80+
if (CustomType.class.isAssignableFrom(rawClass)) {
81+
Class<?> wrappedType = null;
82+
if (!rawClass.equals(EntityId.class)) {
83+
wrappedType = CustomTypeReflectionUtil.getWrappedType(
84+
(Class<? extends CustomType<?>>) rawClass
85+
);
86+
}
87+
88+
var isIdentity = EntityId.class.isAssignableFrom(rawClass);
89+
90+
var description = SwaggerMetaUtil.setIsIdentity(property.getDescription(), isIdentity);
91+
92+
if (wrappedType == null) {
93+
return parameter;
94+
}
95+
96+
if (Boolean.class.isAssignableFrom(wrappedType)) {
97+
property.type("boolean");
98+
property.format(null);
99+
property.setDescription(description);
100+
property.setProperties(null);
101+
property.set$ref(null);
102+
return parameter;
103+
}
104+
if (Byte.class.isAssignableFrom(wrappedType)) {
105+
property.type("integer");
106+
property.format("");
107+
property.setDescription(description);
108+
property.setProperties(null);
109+
property.set$ref(null);
110+
return parameter;
111+
}
112+
if (Short.class.isAssignableFrom(wrappedType)) {
113+
property.type("integer");
114+
property.format("");
115+
property.setDescription(description);
116+
property.setProperties(null);
117+
property.set$ref(null);
118+
return parameter;
119+
}
120+
if (Integer.class.isAssignableFrom(wrappedType)) {
121+
property.type("integer");
122+
property.format("int32");
123+
property.setDescription(description);
124+
property.setProperties(null);
125+
property.set$ref(null);
126+
return parameter;
127+
}
128+
if (Long.class.isAssignableFrom(wrappedType)) {
129+
property.type("integer");
130+
property.format("int64");
131+
property.setDescription(description);
132+
property.setProperties(null);
133+
property.set$ref(null);
134+
return parameter;
135+
}
136+
if (BigInteger.class.isAssignableFrom(wrappedType)) {
137+
property.type("integer");
138+
property.format("");
139+
property.setDescription(description);
140+
property.setProperties(null);
141+
property.set$ref(null);
142+
return parameter;
143+
}
144+
if (Float.class.isAssignableFrom(wrappedType)) {
145+
property.type("number");
146+
property.format("float");
147+
property.setDescription(description);
148+
property.setProperties(null);
149+
property.set$ref(null);
150+
return parameter;
151+
}
152+
if (Double.class.isAssignableFrom(wrappedType)) {
153+
property.type("number");
154+
property.format("double");
155+
property.setDescription(description);
156+
property.setProperties(null);
157+
property.set$ref(null);
158+
return parameter;
159+
}
160+
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
161+
property.type("number");
162+
property.format("");
163+
property.setDescription(description);
164+
property.setProperties(null);
165+
property.set$ref(null);
166+
return parameter;
167+
}
168+
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
169+
property.type("number");
170+
property.format("");
171+
property.setDescription(description);
172+
property.setProperties(null);
173+
property.set$ref(null);
174+
return parameter;
175+
}
176+
if (String.class.isAssignableFrom(wrappedType)) {
177+
property.type("string");
178+
property.format(null);
179+
property.setDescription(description);
180+
property.setProperties(null);
181+
property.set$ref(null);
182+
return parameter;
183+
}
184+
if (Character.class.isAssignableFrom(wrappedType)) {
185+
property.type("string");
186+
property.format(null);
187+
property.minLength(1);
188+
property.maxLength(1);
189+
property.setDescription(description);
190+
property.setProperties(null);
191+
property.set$ref(null);
192+
return parameter;
193+
}
194+
if (UUID.class.isAssignableFrom(wrappedType)) {
195+
property.type("string");
196+
property.format("uuid");
197+
property.minLength(36);
198+
property.maxLength(36);
199+
property.setDescription(description);
200+
property.setProperties(null);
201+
property.set$ref(null);
202+
return parameter;
203+
}
204+
205+
log.warn("Property {} of type WrappedValue: Can not resolve parameter type!", property.getName());
206+
}
207+
208+
return parameter;
209+
}
210+
}

0 commit comments

Comments
 (0)