Skip to content

Commit 317fcc9

Browse files
committed
fix Schema/JSONSchema instance creation for oas31
1 parent bc7436e commit 317fcc9

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

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

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import io.swagger.v3.oas.annotations.media.SchemaProperty;
5454
import io.swagger.v3.oas.models.Components;
5555
import io.swagger.v3.oas.models.ExternalDocumentation;
56+
import io.swagger.v3.oas.models.SpecVersion;
5657
import io.swagger.v3.oas.models.media.ArraySchema;
5758
import io.swagger.v3.oas.models.media.ComposedSchema;
5859
import io.swagger.v3.oas.models.media.Discriminator;
@@ -230,8 +231,12 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
230231
}
231232
} else {
232233
ArraySchema schema = new ArraySchema();
234+
if (openapi31) {
235+
schema.specVersion(SpecVersion.V31);
236+
}
233237
resolveArraySchema(annotatedType, schema, resolvedArrayAnnotation);
234-
return schema.items(new Schema().$ref(resolvedSchemaAnnotation.ref()).name(name));
238+
Schema itemsSchema = openapi31 ? new JsonSchema() : new Schema();
239+
return schema.items(itemsSchema.$ref(resolvedSchemaAnnotation.ref()).name(name));
235240
}
236241
}
237242

@@ -266,6 +271,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
266271
.skipOverride(true);
267272
if (resolvedArrayAnnotation != null) {
268273
ArraySchema schema = new ArraySchema();
274+
if (openapi31) {
275+
schema.specVersion(SpecVersion.V31);
276+
}
269277
resolveArraySchema(annotatedType, schema, resolvedArrayAnnotation);
270278
Schema innerSchema = null;
271279

@@ -277,10 +285,14 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
277285
if (innerSchema != null && isObjectSchema(innerSchema) && StringUtils.isNotBlank(innerSchema.getName())) {
278286
// create a reference for the items
279287
if (context.getDefinedModels().containsKey(innerSchema.getName())) {
280-
innerSchema = new Schema().$ref(constructRef(innerSchema.getName()));
288+
String ref = constructRef(innerSchema.getName());
289+
innerSchema = openapi31 ? new JsonSchema() : new Schema();
290+
innerSchema.$ref(ref);
281291
}
282292
} else if (innerSchema != null && innerSchema.get$ref() != null) {
283-
innerSchema = new Schema().$ref(StringUtils.isNotEmpty(innerSchema.get$ref()) ? innerSchema.get$ref() : innerSchema.getName());
293+
String ref = StringUtils.isNotEmpty(innerSchema.get$ref()) ? innerSchema.get$ref() : innerSchema.getName();
294+
innerSchema = openapi31 ? new JsonSchema() : new Schema();
295+
innerSchema.$ref(ref);
284296
}
285297
}
286298
schema.setItems(innerSchema);
@@ -290,10 +302,14 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
290302
if (implSchema != null && aType.isResolveAsRef() && isObjectSchema(implSchema) && StringUtils.isNotBlank(implSchema.getName())) {
291303
// create a reference for the items
292304
if (context.getDefinedModels().containsKey(implSchema.getName())) {
293-
implSchema = new Schema().$ref(constructRef(implSchema.getName()));
305+
String ref = constructRef(implSchema.getName());
306+
implSchema = openapi31 ? new JsonSchema() : new Schema();
307+
implSchema.$ref(ref);
294308
}
295309
} else if (implSchema != null && implSchema.get$ref() != null) {
296-
implSchema = new Schema().$ref(StringUtils.isNotEmpty(implSchema.get$ref()) ? implSchema.get$ref() : implSchema.getName());
310+
String ref = StringUtils.isNotEmpty(implSchema.get$ref()) ? implSchema.get$ref() : implSchema.getName();
311+
implSchema = openapi31 ? new JsonSchema() : new Schema();
312+
implSchema.$ref(ref);
297313
}
298314
return implSchema;
299315
}
@@ -365,7 +381,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
365381
}
366382

367383
if ("Object".equals(name)) {
368-
Schema schema = new Schema();
384+
Schema schema = openapi31 ? new JsonSchema() : new Schema();
369385
if (schemaRefFromAnnotation != null) {
370386
schema.raw$ref(schemaRefFromAnnotation);
371387
}
@@ -386,6 +402,9 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
386402
resolveSchemaMembers(model, annotatedType, context, next);
387403
if (resolvedArrayAnnotation != null) {
388404
ArraySchema schema = new ArraySchema();
405+
if (openapi31) {
406+
schema.specVersion(SpecVersion.V31);
407+
}
389408
resolveArraySchema(annotatedType, schema, resolvedArrayAnnotation);
390409
schema.setItems(model);
391410
return schema;
@@ -394,7 +413,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
394413
// Store off the ref and add the enum as a top-level model
395414
context.defineModel(name, model, annotatedType, null);
396415
// Return the model as a ref only property
397-
model = new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + name);
416+
model = openapi31 ? new JsonSchema() : new Schema();
417+
model.$ref(Components.COMPONENTS_SCHEMAS_REF + name);
398418
}
399419
if (!isComposedSchema) {
400420
if (schemaRefFromAnnotation != null && model != null) {
@@ -488,14 +508,20 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
488508
addPropertiesSchema = context.getDefinedModels().get(pName);
489509
} else {
490510
// create a reference for the items
491-
addPropertiesSchema = new Schema().$ref(constructRef(pName));
511+
addPropertiesSchema = openapi31 ? new JsonSchema() : new Schema();
512+
addPropertiesSchema.$ref(constructRef(pName));
492513
}
493514
}
494515
} else if (addPropertiesSchema.get$ref() != null) {
495-
addPropertiesSchema = new Schema().$ref(StringUtils.isNotEmpty(addPropertiesSchema.get$ref()) ? addPropertiesSchema.get$ref() : addPropertiesSchema.getName());
516+
String ref = StringUtils.isNotEmpty(addPropertiesSchema.get$ref()) ? addPropertiesSchema.get$ref() : addPropertiesSchema.getName();
517+
addPropertiesSchema = openapi31 ? new JsonSchema() : new Schema();
518+
addPropertiesSchema.$ref(ref);
496519
}
497520
}
498521
Schema mapModel = new MapSchema().additionalProperties(addPropertiesSchema);
522+
if (openapi31) {
523+
mapModel.specVersion(SpecVersion.V31);
524+
}
499525
mapModel.name(name);
500526
model = mapModel;
501527
} else if (valueType != null) {
@@ -540,15 +566,21 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
540566
items = context.getDefinedModels().get(pName);
541567
} else {
542568
// create a reference for the items
543-
items = new Schema().$ref(constructRef(pName));
569+
items = openapi31 ? new JsonSchema() : new Schema();
570+
items.$ref(constructRef(pName));
544571
}
545572
}
546573
} else if (items.get$ref() != null) {
547-
items = new Schema().$ref(StringUtils.isNotEmpty(items.get$ref()) ? items.get$ref() : items.getName());
574+
String ref = StringUtils.isNotEmpty(items.get$ref()) ? items.get$ref() : items.getName();
575+
items = openapi31 ? new JsonSchema() : new Schema();
576+
items.$ref(ref);
548577
}
549578

550579
Schema arrayModel =
551580
new ArraySchema().items(items);
581+
if (openapi31) {
582+
arrayModel.specVersion(SpecVersion.V31);
583+
}
552584
if (_isSetType(type.getRawClass())) {
553585
arrayModel.setUniqueItems(true);
554586
}
@@ -560,7 +592,8 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
560592
}
561593
}
562594
} else if (isComposedSchema) {
563-
model = new ComposedSchema().name(name);
595+
model = openapi31 ? new JsonSchema() : new ComposedSchema();
596+
model.name(name);
564597
if (
565598
(openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
566599
(!openapi31 && (!Boolean.FALSE.equals(PrimitiveType.explicitObjectType)))) {
@@ -783,7 +816,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
783816
handleUnwrapped(props, innerModel, uw.prefix(), uw.suffix(), requiredProps);
784817
return null;
785818
} else {
786-
return new Schema();
819+
return openapi31 ? new JsonSchema() : new Schema();
787820
}
788821
});
789822
property = context.resolve(aType);
@@ -851,13 +884,15 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
851884
if (Schema.SchemaResolution.INLINE.equals(resolvedSchemaResolution)) {
852885
property = context.getDefinedModels().get(pName);
853886
} else if (Schema.SchemaResolution.ALL_OF.equals(resolvedSchemaResolution) && ctxProperty != null) {
854-
property = new Schema()
887+
property = openapi31 ? new JsonSchema() : new Schema();
888+
property
855889
.addAllOfItem(ctxProperty)
856-
.addAllOfItem(new Schema().$ref(constructRef(pName)));
890+
.addAllOfItem(openapi31 ? new JsonSchema().$ref(constructRef(pName)) : new Schema().$ref(constructRef(pName)));
857891
} else if (Schema.SchemaResolution.ALL_OF_REF.equals(resolvedSchemaResolution) && ctxProperty != null) {
858-
property = ctxProperty.addAllOfItem(new Schema().$ref(constructRef(pName)));
892+
property = ctxProperty.addAllOfItem(openapi31 ? new JsonSchema().$ref(constructRef(pName)) : new Schema().$ref(constructRef(pName)));
859893
} else {
860-
property = new Schema().$ref(constructRef(pName));
894+
property = openapi31 ? new JsonSchema() : new Schema();
895+
property.$ref(constructRef(pName));
861896
}
862897
property = clone(property);
863898
// TODO: why is this needed? is it not handled before?
@@ -1078,7 +1113,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
10781113
if (!composedModelPropertiesAsSibling) {
10791114
if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
10801115
if (composedSchema.getProperties() != null && !composedSchema.getProperties().isEmpty()) {
1081-
ObjectSchema propSchema = new ObjectSchema();
1116+
Schema propSchema = openapi31 ? new JsonSchema().typesItem("object") : new ObjectSchema();
10821117
propSchema.properties(composedSchema.getProperties());
10831118
composedSchema.setProperties(null);
10841119
composedSchema.addAllOfItem(propSchema);
@@ -1116,11 +1151,14 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
11161151
if (model != null && resolvedArrayAnnotation != null) {
11171152
if (!"array".equals(model.getType())) {
11181153
ArraySchema schema = new ArraySchema();
1154+
if (openapi31) {
1155+
schema.specVersion(SpecVersion.V31);
1156+
}
11191157
schema.setItems(model);
11201158
resolveArraySchema(annotatedType, schema, resolvedArrayAnnotation);
11211159
return schema;
11221160
} else {
1123-
if (model instanceof ArraySchema) {
1161+
if (isArraySchema(model)) {
11241162
resolveArraySchema(annotatedType, (ArraySchema) model, resolvedArrayAnnotation);
11251163
}
11261164
}
@@ -1589,7 +1627,8 @@ protected Schema process(Schema id, String propertyName, AnnotatedType type,
15891627
model = resolve(type, context, null);
15901628
}
15911629
model.addProperties(propertyName, id);
1592-
return new Schema().$ref(StringUtils.isNotEmpty(model.get$ref())
1630+
Schema retSchema = openapi31 ? new JsonSchema() : new Schema();
1631+
return retSchema.$ref(StringUtils.isNotEmpty(model.get$ref())
15931632
? model.get$ref() : model.getName());
15941633
}
15951634

@@ -2046,7 +2085,8 @@ private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConvert
20462085
} else {
20472086
composedSchema = (ComposedSchema) subtypeModel;
20482087
}
2049-
Schema refSchema = new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + model.getName());
2088+
Schema refSchema = openapi31 ? new JsonSchema() : new Schema();
2089+
refSchema.$ref(Components.COMPONENTS_SCHEMAS_REF + model.getName());
20502090
// allOf could have already being added during type resolving when @Schema(allOf..) is declared
20512091
if (composedSchema.getAllOf() == null || !composedSchema.getAllOf().contains(refSchema)) {
20522092
composedSchema.addAllOfItem(refSchema);
@@ -2055,7 +2095,7 @@ private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConvert
20552095
if (!composedModelPropertiesAsSibling) {
20562096
if (composedSchema.getAllOf() != null && !composedSchema.getAllOf().isEmpty()) {
20572097
if (composedSchema.getProperties() != null && !composedSchema.getProperties().isEmpty()) {
2058-
ObjectSchema propSchema = new ObjectSchema();
2098+
Schema propSchema = openapi31 ? new JsonSchema().typesItem("object") : new ObjectSchema();
20592099
propSchema.properties(composedSchema.getProperties());
20602100
composedSchema.setProperties(null);
20612101
composedSchema.addAllOfItem(propSchema);
@@ -2654,7 +2694,7 @@ protected Schema resolveWrapping(JavaType type, ModelConverterContext context, S
26542694
if (JsonTypeInfo.Id.NAME.equals(id) && name == null) {
26552695
name = type.getRawClass().getSimpleName();
26562696
}
2657-
Schema wrapperSchema = new ObjectSchema();
2697+
Schema wrapperSchema = openapi31 ? new JsonSchema().typesItem("object") : new ObjectSchema();
26582698
wrapperSchema.name(model.getName());
26592699
wrapperSchema.addProperties(name, model);
26602700
return wrapperSchema;
@@ -2890,7 +2930,7 @@ protected String resolveContentMediaType(Annotated a, Annotation[] annotations,
28902930

28912931
protected void resolveContains(AnnotatedType annotatedType, ArraySchema arraySchema, io.swagger.v3.oas.annotations.media.ArraySchema arraySchemaAnnotation) {
28922932
final io.swagger.v3.oas.annotations.media.Schema containsAnnotation = arraySchemaAnnotation.contains();
2893-
final Schema contains = new Schema();
2933+
final Schema contains = openapi31 ? new JsonSchema() : new Schema();
28942934
if (containsAnnotation.types().length > 0) {
28952935
for (String type : containsAnnotation.types()) {
28962936
contains.addType(type);
@@ -2911,7 +2951,7 @@ protected void resolveContains(AnnotatedType annotatedType, ArraySchema arraySch
29112951

29122952
protected void resolveUnevaluatedItems(AnnotatedType annotatedType, ArraySchema arraySchema, io.swagger.v3.oas.annotations.media.ArraySchema arraySchemaAnnotation) {
29132953
final io.swagger.v3.oas.annotations.media.Schema unevaluatedItemsAnnotation = arraySchemaAnnotation.unevaluatedItems();
2914-
final Schema unevaluatedItems = new Schema();
2954+
final Schema unevaluatedItems = openapi31 ? new JsonSchema() : new Schema();
29152955
if (StringUtils.isNotBlank(unevaluatedItemsAnnotation.type())) {
29162956
unevaluatedItems.addType(unevaluatedItemsAnnotation.type());
29172957
}
@@ -3388,7 +3428,7 @@ private void resolveArraySchema(AnnotatedType annotatedType, ArraySchema schema,
33883428
}
33893429
if (resolvedArrayAnnotation.prefixItems().length > 0) {
33903430
for (io.swagger.v3.oas.annotations.media.Schema prefixItemAnnotation : resolvedArrayAnnotation.prefixItems()) {
3391-
final Schema prefixItem = new Schema();
3431+
final Schema prefixItem = new JsonSchema();
33923432
if (StringUtils.isNotBlank(prefixItemAnnotation.type())) {
33933433
prefixItem.addType(prefixItemAnnotation.type());
33943434
}
@@ -3497,7 +3537,8 @@ protected Schema buildRefSchemaIfObject(Schema schema, ModelConverterContext con
34973537
Schema result = schema;
34983538
if (isObjectSchema(schema) && StringUtils.isNotBlank(schema.getName())) {
34993539
if (context.getDefinedModels().containsKey(schema.getName())) {
3500-
result = new Schema().$ref(constructRef(schema.getName()));
3540+
result = openapi31 ? new JsonSchema() : new Schema();
3541+
result.$ref(constructRef(schema.getName()));
35013542
}
35023543
}
35033544
return result;

0 commit comments

Comments
 (0)