Skip to content

Commit f603955

Browse files
committed
fix: Fix ClassCastException on resolving JsonSchema. This solves issue #4904
1 parent 52c993a commit f603955

File tree

2 files changed

+325
-21
lines changed

2 files changed

+325
-21
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
391391
}
392392

393393
List<Class<?>> composedSchemaReferencedClasses = getComposedSchemaReferencedClasses(type.getRawClass(), annotatedType.getCtxAnnotations(), resolvedSchemaAnnotation);
394-
boolean isComposedSchema = composedSchemaReferencedClasses != null;
394+
boolean hasCompositionKeywords = composedSchemaReferencedClasses != null;
395395

396396
if (isPrimitive) {
397397
XML xml = resolveXml(beanDesc.getClassInfo(), annotatedType.getCtxAnnotations(), resolvedSchemaAnnotation);
@@ -418,7 +418,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
418418
model = openapi31 ? new JsonSchema() : new Schema();
419419
model.$ref(Components.COMPONENTS_SCHEMAS_REF + name);
420420
}
421-
if (!isComposedSchema) {
421+
if (!hasCompositionKeywords) {
422422
if (schemaRefFromAnnotation != null && model != null) {
423423
model.raw$ref(schemaRefFromAnnotation);
424424
}
@@ -464,7 +464,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
464464

465465
if (type.isContainerType()) {
466466
// TODO currently a MapSchema or ArraySchema don't also support composed schema props (oneOf,..)
467-
isComposedSchema = false;
467+
hasCompositionKeywords = false;
468468
JavaType keyType = type.getKeyType();
469469
JavaType valueType = type.getContentType();
470470
String pName = null;
@@ -593,7 +593,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
593593
return null;
594594
}
595595
}
596-
} else if (isComposedSchema) {
596+
} else if (hasCompositionKeywords) {
597597
model = openapi31 ? new JsonSchema() : new ComposedSchema();
598598
model.name(name);
599599
if (
@@ -1041,9 +1041,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
10411041
}
10421042
}
10431043

1044-
if (isComposedSchema) {
1044+
if (hasCompositionKeywords) {
10451045

1046-
ComposedSchema composedSchema = (ComposedSchema) model;
1046+
Schema schemaWithCompositionKeys = model;
10471047

10481048
Class<?>[] allOf = resolvedSchemaAnnotation.allOf();
10491049
Class<?>[] anyOf = resolvedSchemaAnnotation.anyOf();
@@ -1061,12 +1061,12 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
10611061
refSchema = allOfRef;
10621062
}
10631063
// allOf could have already being added during subtype resolving
1064-
if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) {
1065-
composedSchema.addAllOfItem(refSchema);
1064+
if (schemaWithCompositionKeys.getAllOf() == null || !schemaWithCompositionKeys.getAllOf().contains(refSchema)) {
1065+
schemaWithCompositionKeys.addAllOfItem(refSchema);
10661066
}
10671067
// remove shared properties defined in the parent
10681068
if (isSubtype(beanDesc.getClassInfo(), c)) {
1069-
removeParentProperties(composedSchema, allOfRef);
1069+
removeParentProperties(schemaWithCompositionKeys, allOfRef);
10701070
}
10711071
});
10721072

@@ -1079,14 +1079,14 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
10791079
Schema anyOfRef = context.resolve(new AnnotatedType().components(annotatedType.getComponents()).type(c).jsonViewAnnotation(annotatedType.getJsonViewAnnotation()));
10801080
if (anyOfRef != null) {
10811081
if (StringUtils.isNotBlank(anyOfRef.getName())) {
1082-
composedSchema.addAnyOfItem(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + anyOfRef.getName()));
1082+
schemaWithCompositionKeys.addAnyOfItem(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + anyOfRef.getName()));
10831083
} else {
1084-
composedSchema.addAnyOfItem(anyOfRef);
1084+
schemaWithCompositionKeys.addAnyOfItem(anyOfRef);
10851085
}
10861086
}
10871087
// remove shared properties defined in the parent
10881088
if (isSubtype(beanDesc.getClassInfo(), c)) {
1089-
removeParentProperties(composedSchema, anyOfRef);
1089+
removeParentProperties(schemaWithCompositionKeys, anyOfRef);
10901090
}
10911091

10921092
});
@@ -1100,25 +1100,25 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
11001100
Schema oneOfRef = context.resolve(new AnnotatedType().components(annotatedType.getComponents()).type(c).jsonViewAnnotation(annotatedType.getJsonViewAnnotation()));
11011101
if (oneOfRef != null) {
11021102
if (StringUtils.isBlank(oneOfRef.getName())) {
1103-
composedSchema.addOneOfItem(oneOfRef);
1103+
schemaWithCompositionKeys.addOneOfItem(oneOfRef);
11041104
} else {
1105-
composedSchema.addOneOfItem(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + oneOfRef.getName()));
1105+
schemaWithCompositionKeys.addOneOfItem(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + oneOfRef.getName()));
11061106
}
11071107
// remove shared properties defined in the parent
11081108
if (isSubtype(beanDesc.getClassInfo(), c)) {
1109-
removeParentProperties(composedSchema, oneOfRef);
1109+
removeParentProperties(schemaWithCompositionKeys, oneOfRef);
11101110
}
11111111
}
11121112

11131113
});
11141114

11151115
if (!composedModelPropertiesAsSibling) {
1116-
if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
1117-
if (composedSchema.getProperties() != null && !composedSchema.getProperties().isEmpty()) {
1116+
if (schemaWithCompositionKeys.getAllOf() != null && !schemaWithCompositionKeys.getAllOf().isEmpty()) {
1117+
if (schemaWithCompositionKeys.getProperties() != null && !schemaWithCompositionKeys.getProperties().isEmpty()) {
11181118
Schema propSchema = openapi31 ? new JsonSchema().typesItem("object") : new ObjectSchema();
1119-
propSchema.properties(composedSchema.getProperties());
1120-
composedSchema.setProperties(null);
1121-
composedSchema.addAllOfItem(propSchema);
1119+
propSchema.properties(schemaWithCompositionKeys.getProperties());
1120+
schemaWithCompositionKeys.setProperties(null);
1121+
schemaWithCompositionKeys.addAllOfItem(propSchema);
11221122
}
11231123
}
11241124
}
@@ -1139,7 +1139,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
11391139
Schema.SchemaResolution resolvedSchemaResolution = AnnotationsUtils.resolveSchemaResolution(this.schemaResolution, resolvedSchemaAnnotation);
11401140

11411141
if (model != null && annotatedType.isResolveAsRef() &&
1142-
(isComposedSchema || isObjectSchema(model) || implicitObject) &&
1142+
(hasCompositionKeywords || isObjectSchema(model) || implicitObject) &&
11431143
StringUtils.isNotBlank(model.getName())) {
11441144
if (context.getDefinedModels().containsKey(model.getName())) {
11451145
if (!Schema.SchemaResolution.INLINE.equals(resolvedSchemaResolution)) {

0 commit comments

Comments
 (0)