Skip to content

Commit f73fdd2

Browse files
committed
use proper metadata for swagger additional data
1 parent 67c194b commit f73fdd2

4 files changed

Lines changed: 161 additions & 43 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package it.aboutbits.springboot.toolbox.swagger;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
11+
public class SwaggerMeta {
12+
private String originalTypeFqn;
13+
@JsonProperty("isIdentity")
14+
private boolean isIdentity = false;
15+
@JsonProperty("isNestedStructure")
16+
private boolean isNestedStructure = false;
17+
@JsonProperty("isMap")
18+
private boolean isMap = false;
19+
private String mapKeyTypeFqn;
20+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package it.aboutbits.springboot.toolbox.swagger;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import lombok.NonNull;
6+
import lombok.SneakyThrows;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.lang.Nullable;
9+
10+
@Slf4j
11+
public final class SwaggerMetaUtil {
12+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
13+
14+
private SwaggerMetaUtil() {
15+
}
16+
17+
@SneakyThrows(JsonProcessingException.class)
18+
public static String setMapKeyTypeFqn(@Nullable String currentMeta, @NonNull String value) {
19+
var meta = getSwaggerMeta(currentMeta);
20+
meta.setMapKeyTypeFqn(value);
21+
22+
return OBJECT_MAPPER.writeValueAsString(meta);
23+
}
24+
25+
@SneakyThrows(JsonProcessingException.class)
26+
public static String setIsMap(@Nullable String currentMeta, boolean value) {
27+
var meta = getSwaggerMeta(currentMeta);
28+
meta.setMap(value);
29+
30+
return OBJECT_MAPPER.writeValueAsString(meta);
31+
}
32+
33+
@SneakyThrows(JsonProcessingException.class)
34+
public static String setOriginalTypeFqn(@Nullable String currentMeta, @NonNull String value) {
35+
var meta = getSwaggerMeta(currentMeta);
36+
meta.setOriginalTypeFqn(value);
37+
38+
return OBJECT_MAPPER.writeValueAsString(meta);
39+
}
40+
41+
@SneakyThrows(JsonProcessingException.class)
42+
public static String setIsIdentity(@Nullable String currentMeta, boolean value) {
43+
var meta = getSwaggerMeta(currentMeta);
44+
meta.setIdentity(value);
45+
46+
return OBJECT_MAPPER.writeValueAsString(meta);
47+
}
48+
49+
@SneakyThrows(JsonProcessingException.class)
50+
public static String setIsNestedStructure(@Nullable String currentMeta, boolean value) {
51+
var meta = getSwaggerMeta(currentMeta);
52+
meta.setNestedStructure(value);
53+
54+
return OBJECT_MAPPER.writeValueAsString(meta);
55+
}
56+
57+
private static SwaggerMeta getSwaggerMeta(String currentMeta) {
58+
var meta = new SwaggerMeta();
59+
if (currentMeta != null) {
60+
try {
61+
meta = OBJECT_MAPPER.readValue(currentMeta, SwaggerMeta.class);
62+
} catch (JsonProcessingException e) {
63+
log.warn("error processing meta for swagger: {}", currentMeta);
64+
}
65+
}
66+
return meta;
67+
}
68+
}

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

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import io.swagger.v3.core.converter.ModelConverterContext;
66
import io.swagger.v3.oas.models.media.Schema;
77
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
8+
import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil;
89
import it.aboutbits.springboot.toolbox.type.CustomType;
910
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
11+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
1012

1113
import java.math.BigDecimal;
1214
import java.math.BigInteger;
@@ -23,36 +25,56 @@ public Schema<?> resolve(
2325

2426
var type = annotatedType.getType();
2527

26-
if (type instanceof Class<?> clazz && CustomType.class.isAssignableFrom(clazz)) {
27-
var constructor = RecordReflectionUtil.getCanonicalConstructor(clazz);
28-
var wrappedType = constructor.getParameters()[0].getType();
28+
if (type instanceof Class<?> clazz) {
29+
Schema<?> result = null;
30+
if (CustomType.class.isAssignableFrom(clazz)) {
31+
var constructor = RecordReflectionUtil.getCanonicalConstructor(clazz);
32+
var wrappedType = constructor.getParameters()[0].getType();
33+
var isIdentity = false;
2934

30-
if (Short.class.isAssignableFrom(wrappedType)) {
31-
return context.resolve(new AnnotatedType(Short.TYPE));
32-
}
33-
if (Integer.class.isAssignableFrom(wrappedType)) {
34-
return context.resolve(new AnnotatedType(Integer.TYPE));
35-
}
36-
if (Long.class.isAssignableFrom(wrappedType)) {
37-
return context.resolve(new AnnotatedType(Long.TYPE));
38-
}
39-
if (BigInteger.class.isAssignableFrom(wrappedType)) {
40-
return context.resolve(new AnnotatedType(Long.TYPE));
41-
}
42-
if (Float.class.isAssignableFrom(wrappedType)) {
43-
return context.resolve(new AnnotatedType(Float.TYPE));
44-
}
45-
if (Double.class.isAssignableFrom(wrappedType)) {
46-
return context.resolve(new AnnotatedType(Double.TYPE));
47-
}
48-
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
49-
return context.resolve(new AnnotatedType(Double.TYPE));
50-
}
51-
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
52-
return context.resolve(new AnnotatedType(Double.TYPE));
53-
}
54-
if (String.class.isAssignableFrom(wrappedType)) {
55-
return context.resolve(new AnnotatedType(String.class));
35+
if (EntityId.class.isAssignableFrom(clazz)) {
36+
isIdentity = true;
37+
}
38+
39+
40+
if (Short.class.isAssignableFrom(wrappedType)) {
41+
result = context.resolve(new AnnotatedType(Short.TYPE));
42+
}
43+
if (Integer.class.isAssignableFrom(wrappedType)) {
44+
result = context.resolve(new AnnotatedType(Integer.TYPE));
45+
}
46+
if (Long.class.isAssignableFrom(wrappedType)) {
47+
result = context.resolve(new AnnotatedType(Long.TYPE));
48+
}
49+
if (BigInteger.class.isAssignableFrom(wrappedType)) {
50+
result = context.resolve(new AnnotatedType(Long.TYPE));
51+
}
52+
if (Float.class.isAssignableFrom(wrappedType)) {
53+
result = context.resolve(new AnnotatedType(Float.TYPE));
54+
}
55+
if (Double.class.isAssignableFrom(wrappedType)) {
56+
result = context.resolve(new AnnotatedType(Double.TYPE));
57+
}
58+
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
59+
result = context.resolve(new AnnotatedType(Double.TYPE));
60+
}
61+
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
62+
result = context.resolve(new AnnotatedType(Double.TYPE));
63+
}
64+
if (String.class.isAssignableFrom(wrappedType)) {
65+
result = context.resolve(new AnnotatedType(String.class));
66+
}
67+
68+
if (result != null) {
69+
var description = SwaggerMetaUtil.setOriginalTypeFqn(
70+
result.getDescription(),
71+
((Class<?>) type).getName()
72+
);
73+
description = SwaggerMetaUtil.setIsIdentity(description, isIdentity);
74+
result.setDescription(description);
75+
76+
return result;
77+
}
5678
}
5779
}
5880

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.swagger.v3.core.converter.AnnotatedType;
55
import io.swagger.v3.oas.models.media.Schema;
66
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
7+
import it.aboutbits.springboot.toolbox.swagger.SwaggerMetaUtil;
78
import it.aboutbits.springboot.toolbox.type.CustomType;
89
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
910
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
@@ -26,18 +27,20 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
2627
// We set the enum name as the description because swagger treats each usage as a new enum.
2728
// This way we can preserve the information about the original enum type.
2829
if (rawClass.isEnum()) {
29-
var displayName = rawClass.getCanonicalName().replace(rawClass.getPackage().getName() + ".", "");
30-
property.setDescription(displayName);
30+
var displayName = rawClass.getName(); // .replace(rawClass.getPackage().getName() + ".", "");
31+
property.setDescription(SwaggerMetaUtil.setOriginalTypeFqn(property.getDescription(), displayName));
3132
}
3233
}
3334

3435
if (type instanceof SimpleType simpleType && CustomType.class.isAssignableFrom(simpleType.getRawClass())) {
3536
var rawClass = simpleType.getRawClass();
3637

37-
var displayName = rawClass.getSimpleName();
38+
var displayName = rawClass.getName(); // .getSimpleName();
39+
var isIdentity = false;
3840

3941
Class<?> wrappedType;
4042
if (EntityId.class.isAssignableFrom(rawClass)) {
43+
isIdentity = true;
4144
displayName = resolveEntityIdDisplayName(rawClass);
4245
}
4346

@@ -48,58 +51,62 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
4851
wrappedType = constructor.getParameters()[0].getType();
4952
}
5053

54+
var description = SwaggerMetaUtil.setOriginalTypeFqn(property.getDescription(), displayName);
55+
description = SwaggerMetaUtil.setIsIdentity(description, isIdentity);
56+
description = SwaggerMetaUtil.setIsIdentity(description, isIdentity);
57+
5158
if (Short.class.isAssignableFrom(wrappedType)) {
5259
property.type("integer");
5360
property.format("");
54-
property.setDescription(displayName);
61+
property.setDescription(description);
5562
property.setProperties(null);
5663
property.set$ref(null);
5764
return property;
5865
}
5966
if (Integer.class.isAssignableFrom(wrappedType)) {
6067
property.type("integer");
6168
property.format("int32");
62-
property.setDescription(displayName);
69+
property.setDescription(description);
6370
property.setProperties(null);
6471
property.set$ref(null);
6572
return property;
6673
}
6774
if (Long.class.isAssignableFrom(wrappedType)) {
6875
property.type("integer");
6976
property.format("int64");
70-
property.setDescription(displayName);
77+
property.setDescription(description);
7178
property.setProperties(null);
7279
property.set$ref(null);
7380
return property;
7481
}
7582
if (BigInteger.class.isAssignableFrom(wrappedType)) {
7683
property.type("integer");
7784
property.format("int64");
78-
property.setDescription(displayName);
85+
property.setDescription(description);
7986
property.setProperties(null);
8087
property.set$ref(null);
8188
return property;
8289
}
8390
if (Float.class.isAssignableFrom(wrappedType)) {
8491
property.type("number");
8592
property.format("float");
86-
property.setDescription(displayName);
93+
property.setDescription(description);
8794
property.setProperties(null);
8895
property.set$ref(null);
8996
return property;
9097
}
9198
if (Double.class.isAssignableFrom(wrappedType)) {
9299
property.type("number");
93100
property.format("double");
94-
property.setDescription(displayName);
101+
property.setDescription(description);
95102
property.setProperties(null);
96103
property.set$ref(null);
97104
return property;
98105
}
99106
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
100107
property.type("number");
101108
property.format("");
102-
property.setDescription(displayName);
109+
property.setDescription(description);
103110
property.setProperties(null);
104111
property.set$ref(null);
105112
return property;
@@ -108,15 +115,15 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
108115
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
109116
property.type("number");
110117
property.format("");
111-
property.setDescription(displayName);
118+
property.setDescription(description);
112119
property.setProperties(null);
113120
property.set$ref(null);
114121
return property;
115122
}
116123
if (String.class.isAssignableFrom(wrappedType)) {
117124
property.type("string");
118125
property.format(null);
119-
property.setDescription(displayName);
126+
property.setDescription(description);
120127
property.setProperties(null);
121128
property.set$ref(null);
122129
return property;
@@ -129,10 +136,11 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
129136

130137
@NonNull
131138
private static String resolveEntityIdDisplayName(Class<?> rawClass) {
132-
var parent = rawClass.getEnclosingClass();
139+
return rawClass.getName();
140+
/* var parent = rawClass.getEnclosingClass();
133141
if (parent != null) {
134142
return parent.getSimpleName() + "." + rawClass.getSimpleName();
135143
}
136-
return rawClass.getSimpleName();
144+
return rawClass.getSimpleName();*/
137145
}
138146
}

0 commit comments

Comments
 (0)