Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #4715 - change JsonSchemaDialect to one accepted by Swagger-UI #4729

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,24 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
}
}
} else if (isComposedSchema) {
model = new ComposedSchema()
.type("object")
.name(name);
model = new ComposedSchema().name(name);
if (openapi31 && resolvedArrayAnnotation == null){
model.addType("object");
}else{
model.type("object");
}
} else {
AnnotatedType aType = ReferenceTypeUtils.unwrapReference(annotatedType);
if (aType != null) {
model = context.resolve(aType);
return model;
} else {
model = new Schema()
.type("object")
.name(name);
model = new Schema().name(name);
if (openapi31 && resolvedArrayAnnotation == null){
model.addType("object");
}else{
model.type("object");
}
}
}

Expand Down Expand Up @@ -2980,7 +2986,7 @@ public void setOpenapi31(boolean openapi31) {
}

protected boolean isObjectSchema(Schema schema) {
return "object".equals(schema.getType()) || (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
return (schema.getTypes() != null && schema.getTypes().contains("object")) || "object".equals(schema.getType()) || (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
}

protected Schema buildRefSchemaIfObject(Schema schema, ModelConverterContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class OpenAPI30To31 {

public void process(OpenAPI openAPI) {
openAPI.openapi("3.1.0")
.jsonSchemaDialect("https://json-schema.org/draft/2020-12/schema")
.jsonSchemaDialect("https://spec.openapis.org/oas/3.1/dialect/base")
.specVersion(SpecVersion.V31);

removeReservedExtensionsName(openAPI.getExtensions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Ticket4679Test extends SwaggerTestBase{
public void testCustomSchemaImplementation() {

String expectedYaml = "ModelWithCustomSchemaImplementationInProperty:\n" +
" type: object\n" +
" properties:\n" +
" exampleField:\n" +
" type: integer\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.swagger.v3.core.resolving.resources;

import io.swagger.v3.oas.annotations.media.ArraySchema;

import java.util.ArrayList;
import java.util.List;

public class TestArrayType {

private Integer id;

@ArraySchema(maxItems = 10)
private List<String> names;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public List<String> getNames() {
return names;
}

public void setNames(List<String> names) {
this.names = names;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.swagger.v3.core.resolving.resources;

public class TestObject4715 {

private String foo;

private String bar;

private Integer id;

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.SwaggerTestBase;
import io.swagger.v3.core.resolving.resources.TestArrayType;
import io.swagger.v3.core.resolving.resources.TestObject4715;
import io.swagger.v3.core.resolving.v31.model.AnnotatedArray;
import io.swagger.v3.core.resolving.v31.model.ModelWithDependentSchema;
import io.swagger.v3.core.resolving.v31.model.ModelWithOAS31Stuff;
Expand Down Expand Up @@ -39,6 +41,7 @@ public void testOAS31Fields() {
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
Schema model = context.resolve(new AnnotatedType(ModelWithOAS31Stuff.class));
SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "Address:\n" +
" type: object\n" +
" if:\n" +
" $ref: '#/components/schemas/AnnotatedCountry'\n" +
" then:\n" +
Expand All @@ -59,17 +62,20 @@ public void testOAS31Fields() {
" propertyNames:\n" +
" $ref: '#/components/schemas/PropertyNamesPattern'\n" +
"AnnotatedCountry:\n" +
" type: object\n" +
" properties:\n" +
" country:\n" +
" const: United States\n" +
"Client:\n" +
" type: object\n" +
" properties:\n" +
" name:\n" +
" type: string\n" +
" creditCard:\n" +
" type: integer\n" +
" format: int32\n" +
"CreditCard:\n" +
" type: object\n" +
" properties:\n" +
" billingAddress:\n" +
" type: string\n" +
Expand Down Expand Up @@ -124,6 +130,7 @@ public void testOAS31Fields() {
" properties:\n" +
" extraObject: {}\n" +
"MultipleBaseBean:\n" +
" type: object\n" +
" description: MultipleBaseBean\n" +
" properties:\n" +
" beanType:\n" +
Expand Down Expand Up @@ -152,14 +159,17 @@ public void testOAS31Fields() {
" format: int32\n" +
" description: MultipleSub2Bean\n" +
"PostalCodeNumberPattern:\n" +
" type: object\n" +
" properties:\n" +
" postalCode:\n" +
" pattern: \"[0-9]{5}(-[0-9]{4})?\"\n" +
"PostalCodePattern:\n" +
" type: object\n" +
" properties:\n" +
" postalCode:\n" +
" pattern: \"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]\"\n" +
"PropertyNamesPattern:\n" +
" type: object\n" +
" pattern: \"^[A-Za-z_][A-Za-z0-9_]*$\"\n");
}

Expand All @@ -170,10 +180,12 @@ public void testDependentSchemasAnnotation() {
io.swagger.v3.oas.models.media.Schema model = context.resolve(new AnnotatedType(ModelWithDependentSchema.class));

SerializationMatchers.assertEqualsToYaml31(context.getDefinedModels(), "BooleanFakeClass:\n" +
" type: object\n" +
" properties:\n" +
" type:\n" +
" type: boolean\n" +
"ModelWithDependentSchema:\n" +
" type: object\n" +
" dependentSchemas:\n" +
" value:\n" +
" properties:\n" +
Expand All @@ -187,4 +199,36 @@ public void testDependentSchemasAnnotation() {
" format: int32\n");
}

@Test(description = "Top level type:object should appear in OAS31")
public void testObjectTypeSchemaOAS31(){
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true);
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
io.swagger.v3.oas.models.media.Schema model = context.resolve(new AnnotatedType(TestObject4715.class));
SerializationMatchers.assertEqualsToYaml31(model, "type: object\n" +
"properties:\n" +
" foo:\n" +
" type: string\n" +
" bar:\n" +
" type: string\n" +
" id:\n" +
" type: integer\n" +
" format: int32");
}

@Test
public void testFieldArraySchemaAnnotation() {
final ModelResolver modelResolver = new ModelResolver(mapper()).openapi31(true);
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
io.swagger.v3.oas.models.media.Schema model = context.resolve(new AnnotatedType(TestArrayType.class));
SerializationMatchers.assertEqualsToYaml31(model, " type: object\n" +
" properties:\n" +
" id:\n" +
" type: integer\n" +
" format: int32\n" +
" names:\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" maxItems: 10");
}
}
Loading
Loading