Skip to content

Commit 7294019

Browse files
committed
refactor logic relative to OAS 3.1 resolving
1 parent 5af0cde commit 7294019

File tree

16 files changed

+873
-81
lines changed

16 files changed

+873
-81
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
638638
propSchemaName = ctxSchema.name();
639639
}
640640
}
641+
io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema = AnnotationsUtils.getArraySchemaAnnotation(annotations);
641642
if (propSchemaName == null) {
642-
io.swagger.v3.oas.annotations.media.ArraySchema ctxArraySchema = AnnotationsUtils.getArraySchemaAnnotation(annotations);
643643
if (AnnotationsUtils.hasArrayAnnotation(ctxArraySchema)) {
644644
if (AnnotationsUtils.hasSchemaAnnotation(ctxArraySchema.schema())) {
645645
if (!StringUtils.isBlank(ctxArraySchema.schema().name())) {
@@ -662,10 +662,25 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
662662
io.swagger.v3.oas.annotations.media.Schema.AccessMode accessMode = resolveAccessMode(propDef, type, propResolvedSchemaAnnotation);
663663
io.swagger.v3.oas.annotations.media.Schema.RequiredMode requiredMode = resolveRequiredMode(propResolvedSchemaAnnotation);
664664

665+
Annotation[] ctxAnnotation31 = null;
666+
667+
if (openapi31) {
668+
List<Annotation> ctxAnnotations31List = new ArrayList<>();
669+
if (annotations != null) {
670+
for (Annotation a : annotations) {
671+
if (
672+
!(a instanceof io.swagger.v3.oas.annotations.media.Schema) &&
673+
!(a instanceof io.swagger.v3.oas.annotations.media.ArraySchema)) {
674+
ctxAnnotations31List.add(a);
675+
}
676+
}
677+
ctxAnnotation31 = ctxAnnotations31List.toArray(new Annotation[ctxAnnotations31List.size()]);
678+
}
679+
}
665680

666681
AnnotatedType aType = new AnnotatedType()
667682
.type(propType)
668-
.ctxAnnotations(annotations)
683+
.ctxAnnotations(openapi31 ? ctxAnnotation31 : annotations)
669684
.parent(model)
670685
.resolveAsRef(annotatedType.isResolveAsRef())
671686
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
@@ -688,12 +703,18 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
688703
return new Schema();
689704
}
690705
});
691-
property = clone(context.resolve(aType));
706+
property = context.resolve(aType);
707+
property = clone(property);
692708
if (openapi31) {
693-
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property);
709+
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, context);
694710
if (reResolvedProperty.isPresent()) {
695711
property = reResolvedProperty.get();
696712
}
713+
reResolvedProperty = AnnotationsUtils.getArraySchema(ctxArraySchema, annotatedType.getComponents(), null, openapi31, property);
714+
if (reResolvedProperty.isPresent()) {
715+
property = reResolvedProperty.get();
716+
}
717+
697718
}
698719
if (property != null) {
699720
Boolean required = md.getRequired();
@@ -732,6 +753,17 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
732753

733754
if (context.getDefinedModels().containsKey(pName)) {
734755
property = new Schema().$ref(constructRef(pName));
756+
property = clone(property);
757+
if (openapi31) {
758+
Optional<Schema> reResolvedProperty = AnnotationsUtils.getSchemaFromAnnotation(ctxSchema, annotatedType.getComponents(), null, openapi31, property, context);
759+
if (reResolvedProperty.isPresent()) {
760+
property = reResolvedProperty.get();
761+
}
762+
reResolvedProperty = AnnotationsUtils.getArraySchema(ctxArraySchema, annotatedType.getComponents(), null, openapi31, property);
763+
if (reResolvedProperty.isPresent()) {
764+
property = reResolvedProperty.get();
765+
}
766+
}
735767
}
736768
} else if (property.get$ref() != null) {
737769
if (!openapi31) {
@@ -844,7 +876,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
844876
}
845877

846878
if (openapi31) {
847-
Map<String, Schema> dependentSchemas = resolveDependentSchemas(type, annotatedType.getCtxAnnotations(), context);
879+
Map<String, Schema> dependentSchemas = resolveDependentSchemas(type, annotatedType.getCtxAnnotations(), context, annotatedType.getComponents(), annotatedType.getJsonViewAnnotation(), openapi31);
848880
if (model != null && dependentSchemas != null && !dependentSchemas.isEmpty()) {
849881
if (model.getDependentSchemas() == null) {
850882
model.dependentSchemas(dependentSchemas);
@@ -1029,20 +1061,7 @@ protected Type findJsonValueType(final BeanDescription beanDesc) {
10291061
}
10301062

10311063
private Schema clone(Schema property) {
1032-
if(property == null)
1033-
return property;
1034-
try {
1035-
String cloneName = property.getName();
1036-
if(openapi31) {
1037-
property = Json31.mapper().readValue(Json31.pretty(property), Schema.class);
1038-
} else {
1039-
property = Json.mapper().readValue(Json.pretty(property), Schema.class);
1040-
}
1041-
property.setName(cloneName);
1042-
} catch (IOException e) {
1043-
LOGGER.error("Could not clone property", e);
1044-
}
1045-
return property;
1064+
return AnnotationsUtils.clone(property, openapi31);
10461065
}
10471066

10481067
private boolean isSubtype(AnnotatedClass childClass, Class<?> parentClass) {
@@ -1788,7 +1807,7 @@ protected Map<String, Schema> resolveSchemaProperties(JavaType a, Annotation[] a
17881807
return schemaProperties;
17891808
}
17901809

1791-
protected Map<String, Schema> resolveDependentSchemas(JavaType a, Annotation[] annotations, ModelConverterContext context) {
1810+
protected Map<String, Schema> resolveDependentSchemas(JavaType a, Annotation[] annotations, ModelConverterContext context, Components components, JsonView jsonViewAnnotation, boolean openapi31) {
17921811
final Map<String, DependentSchema> dependentSchemaMap = new LinkedHashMap<>();
17931812

17941813
DependentSchemas dependentSchemasAnnotation = a.getRawClass().getAnnotation(DependentSchemas.class);
@@ -1827,13 +1846,15 @@ protected Map<String, Schema> resolveDependentSchemas(JavaType a, Annotation[] a
18271846
continue;
18281847
}
18291848
Annotation[] propAnnotations = new Annotation[]{dependentSchemaAnnotation.schema(), dependentSchemaAnnotation.array()};
1830-
AnnotatedType propType = new AnnotatedType()
1831-
.type(Object.class)
1832-
.ctxAnnotations(propAnnotations)
1833-
.resolveAsRef(true);
1834-
Schema resolvedPropSchema = context.resolve(propType);
1835-
if (resolvedPropSchema != null) {
1836-
dependentSchemas.put(name, resolvedPropSchema);
1849+
Schema existingSchema = null;
1850+
Optional<Schema> resolvedPropSchema = AnnotationsUtils.getSchemaFromAnnotation(dependentSchemaAnnotation.schema(), components, jsonViewAnnotation, openapi31, null, context);
1851+
if (resolvedPropSchema.isPresent()) {
1852+
existingSchema = resolvedPropSchema.get();
1853+
dependentSchemas.put(name, existingSchema);
1854+
}
1855+
resolvedPropSchema = AnnotationsUtils.getArraySchema(dependentSchemaAnnotation.array(), components, jsonViewAnnotation, openapi31, existingSchema);
1856+
if (resolvedPropSchema.isPresent()) {
1857+
dependentSchemas.put(name, resolvedPropSchema.get());
18371858
}
18381859
}
18391860

0 commit comments

Comments
 (0)