Skip to content

Commit 052b47e

Browse files
refactor: simplify schema annotation type introspection
1 parent 9aaee38 commit 052b47e

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
public abstract class AnnotationsUtils {
6262

6363
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationsUtils.class);
64+
private static final String NULL_TYPE = "null";
65+
private static final String STRING_TYPE = "string";
6466
public static final String COMPONENTS_REF = Components.COMPONENTS_SCHEMAS_REF;
6567

6668
public static boolean hasSchemaAnnotation(io.swagger.v3.oas.annotations.media.Schema schema) {
@@ -1003,7 +1005,7 @@ public static List<Object> parseExamplesArray(io.swagger.v3.oas.annotations.medi
10031005
// Only parse "null" as null value when nullable=true
10041006
if (node.isNull() && schema.nullable()) {
10051007
parsedExamples.add(null);
1006-
} else if (schemaObject == null && "string".equals(schema.type())) {
1008+
} else if (schemaObject == null && STRING_TYPE.equals(schema.type())) {
10071009
parsedExamples.add(trimmed);
10081010
} else if (shouldUseNodeAsExample(node, schemaObject)) {
10091011
parsedExamples.add(node);
@@ -1081,7 +1083,7 @@ public static Schema resolveSchemaFromType(Class<?> schemaImplementation,
10811083
}
10821084
if (StringUtils.isBlank(existingSchemaObject.get$ref()) && StringUtils.isBlank(existingSchemaObject.getType())) {
10831085
// default to string
1084-
existingSchemaObject.setType("string");
1086+
existingSchemaObject.setType(STRING_TYPE);
10851087
}
10861088
return existingSchemaObject;
10871089
}
@@ -1711,7 +1713,7 @@ public static Type getSchemaType(io.swagger.v3.oas.annotations.media.Schema sche
17111713
}
17121714
case "boolean":
17131715
return Boolean.class;
1714-
case "string":
1716+
case STRING_TYPE:
17151717
return String.class;
17161718
default:
17171719
if (nullIfNotFound) {
@@ -1949,26 +1951,28 @@ public static Optional<? extends Schema> getSchema(io.swagger.v3.oas.annotations
19491951
} else {
19501952
Optional<Schema> schemaFromAnnotation = AnnotationsUtils.getSchemaFromAnnotation(schemaAnnotation, components, jsonViewAnnotation, openapi31, null, context);
19511953
if (schemaFromAnnotation.isPresent()) {
1952-
if (StringUtils.isBlank(schemaFromAnnotation.get().get$ref()) && StringUtils.isBlank(schemaFromAnnotation.get().getType()) && !(schemaFromAnnotation.get() instanceof ComposedSchema)) {
1954+
Schema schema = schemaFromAnnotation.get();
1955+
if (StringUtils.isBlank(schema.get$ref()) && StringUtils.isBlank(schema.getType()) && !(schema instanceof ComposedSchema)) {
19531956
// default to string
1954-
schemaFromAnnotation.get().setType("string");
1957+
schema.setType(STRING_TYPE);
19551958
}
1956-
return Optional.of(schemaFromAnnotation.get());
1959+
return Optional.of(schema);
19571960
} else {
19581961
Optional<Schema> arraySchemaFromAnnotation = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null, false, context);
19591962
if (arraySchemaFromAnnotation.isPresent()) {
1960-
if (arraySchemaFromAnnotation.get().getItems() != null && StringUtils.isBlank(arraySchemaFromAnnotation.get().getItems().get$ref()) && StringUtils.isBlank(arraySchemaFromAnnotation.get().getItems().getType())) {
1963+
Schema schema = arraySchemaFromAnnotation.get();
1964+
Schema schemaItems = schema.getItems();
1965+
if (schemaItems != null && StringUtils.isBlank(schemaItems.get$ref()) && StringUtils.isBlank(schemaItems.getType())) {
19611966
// default to string
1962-
arraySchemaFromAnnotation.get().getItems().setType("string");
1967+
schemaItems.setType(STRING_TYPE);
19631968
}
1964-
return Optional.of(arraySchemaFromAnnotation.get());
1969+
return Optional.of(schema);
19651970
}
19661971
}
19671972
}
19681973
return Optional.empty();
19691974
}
19701975

1971-
19721976
public static void applyTypes(String[] classTypes, String[] methodTypes, Content content, MediaType mediaType) {
19731977
if (methodTypes != null && methodTypes.length > 0) {
19741978
for (String value : methodTypes) {

0 commit comments

Comments
 (0)