diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4838Test.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4838Test.java index a3c85a46b5..d14f53a6db 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4838Test.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/issues/Issue4838Test.java @@ -1,6 +1,5 @@ package io.swagger.v3.core.issues; -import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.core.converter.AnnotatedType; import io.swagger.v3.core.converter.ModelConverterContextImpl; import io.swagger.v3.core.jackson.ModelResolver; @@ -8,6 +7,7 @@ import io.swagger.v3.core.util.Json; import io.swagger.v3.core.util.Json31; import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.models.SpecVersion; import org.checkerframework.checker.nullness.qual.NonNull; import org.testng.annotations.Test; @@ -22,7 +22,7 @@ public class Issue4838Test { @Test public void defaultValueShouldBeNullForEmptyStringFieldInOas30() { - final ModelConverterContextImpl context = getModelConverterContext(Json.mapper(), false); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V30); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableStringModel.class)); @@ -37,7 +37,7 @@ public void defaultValueShouldBeNullForEmptyStringFieldInOas30() { @Test public void defaultValueShouldBeNullForIntegerFieldInOas30() { - final ModelConverterContextImpl context = getModelConverterContext(Json.mapper(), false); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V30); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableIntegerModel.class)); @@ -54,7 +54,7 @@ public void defaultValueShouldBeNullForIntegerFieldInOas30() { @Test public void defaultValueShouldBeProvidedFromBigDecimalSchemaForOas30() { - final ModelConverterContextImpl context = getModelConverterContext(Json.mapper(), false); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V30); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableBigDecimalModel.class)); @@ -70,7 +70,7 @@ public void defaultValueShouldBeProvidedFromBigDecimalSchemaForOas30() { @Test public void defaultValueShouldBeNullProvidedFromBooleanSchemaForOas30() { - final ModelConverterContextImpl context = getModelConverterContext(Json.mapper(), false); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V30); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableBooleanModel.class)); @@ -86,7 +86,7 @@ public void defaultValueShouldBeNullProvidedFromBooleanSchemaForOas30() { @Test public void defaultValueShouldBeNullForEmptyStringFieldInOas31() { - final ModelConverterContextImpl context = getModelConverterContext(Json31.mapper(), true); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V31); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableStringModel.class)); @@ -101,7 +101,7 @@ public void defaultValueShouldBeNullForEmptyStringFieldInOas31() { @Test public void defaultValueShouldBeNullForIntegerFieldInOas31() { - final ModelConverterContextImpl context = getModelConverterContext(Json31.mapper(), true); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V31); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableIntegerModel.class)); @@ -117,7 +117,7 @@ public void defaultValueShouldBeNullForIntegerFieldInOas31() { @Test public void defaultValueShouldBeProvidedFromBigDecimalSchemaForOas31() { - final ModelConverterContextImpl context = getModelConverterContext(Json31.mapper(), true); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V31); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableBigDecimalModel.class)); @@ -134,7 +134,7 @@ public void defaultValueShouldBeProvidedFromBigDecimalSchemaForOas31() { @Test public void defaultValueShouldBeNullProvidedFromBooleanSchemaForOas31() { - final ModelConverterContextImpl context = getModelConverterContext(Json31.mapper(), true); + final ModelConverterContextImpl context = getModelConverterContext(SpecVersion.V31); final io.swagger.v3.oas.models.media.Schema model = context .resolve(new AnnotatedType(NullableBooleanModel.class)); @@ -148,10 +148,10 @@ public void defaultValueShouldBeNullProvidedFromBooleanSchemaForOas31() { assertNull(nullableBooleanField.getDefault()); } - private static @NonNull ModelConverterContextImpl getModelConverterContext(ObjectMapper mapper, boolean openAPI31) { - final ModelResolver modelResolver = new ModelResolver(mapper); + private static @NonNull ModelConverterContextImpl getModelConverterContext(SpecVersion specVersion) { + final ModelResolver modelResolver = new ModelResolver(specVersion.equals(SpecVersion.V31) ? Json31.mapper() : Json.mapper()); Configuration configuration = new Configuration(); - configuration.setOpenAPI31(openAPI31); + configuration.setOpenAPI31(specVersion.equals(SpecVersion.V31)); modelResolver.setConfiguration(configuration); return new ModelConverterContextImpl(modelResolver); } diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/PetResourceTest.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/PetResourceTest.java index fcdad85b6a..62f777eac7 100644 --- a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/PetResourceTest.java +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/PetResourceTest.java @@ -48,6 +48,7 @@ import io.swagger.v3.jaxrs2.petstore.tags.TagOperationResource; import io.swagger.v3.oas.integration.SwaggerConfiguration; import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.SpecVersion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.annotations.Test; @@ -128,7 +129,7 @@ public void testOpenAPIDefinitionResource() { } @Test(description = "Test RequestBody resource)") - public void tetRequestBodyResource() { + public void testRequestBodyResource() { compare(RequestBodyResource.class, REQUEST_BODIES_SOURCE); compare(RequestBodyParameterPriorityResource.class, REQUEST_BODIES_SOURCE); compare(RequestBodyMethodPriorityResource.class, REQUEST_BODIES_SOURCE); @@ -268,37 +269,37 @@ public void testEmptyPet31Resource() { @Test(description = "Test a resource with Links and Content)") public void testLinksAndContent31Resource() { - compare(LinksAndContent31Resource.class, LINKS_SOURCE, true); + compare(LinksAndContent31Resource.class, LINKS_SOURCE, SpecVersion.V31); } @Test(description = "Test OpenAPIDefinition resource)") public void testOpenAPI31DefinitionResource() { - compare(OpenAPI31DefinitionResource.class, PETSTORE_SOURCE, true); + compare(OpenAPI31DefinitionResource.class, PETSTORE_SOURCE, SpecVersion.V31); } @Test(description = "Test Parameters resources)") public void testParameters31Resource() { - compare(Parameters31Resource.class, PARAMETERS_SOURCE, true); + compare(Parameters31Resource.class, PARAMETERS_SOURCE, SpecVersion.V31); } @Test(description = "Test some resources with Callbacks)") public void testCallBacks31Resources() { - compare(ComplexCallback31Resource.class, CALLBACKS_SOURCE, true); + compare(ComplexCallback31Resource.class, CALLBACKS_SOURCE, SpecVersion.V31); } @Test(description = "Test some resources with Request Body)") public void testRequestBody31Resources() { - compare(RequestBody31Resource.class, REQUEST_BODIES_SOURCE, true); + compare(RequestBody31Resource.class, REQUEST_BODIES_SOURCE, SpecVersion.V31); } @Test(description = "Test webhook resources") public void testWebhooksResource() { - compare(WebHookResource.class, PETSTORE_SOURCE, true); + compare(WebHookResource.class, PETSTORE_SOURCE, SpecVersion.V31); } @Test(description = "Test method resources with array annotations") public void testMethodArrayResponseResource() { - compare(MethodArrayResponseResource.class, RESPONSES_SOURCE, true); + compare(MethodArrayResponseResource.class, RESPONSES_SOURCE, SpecVersion.V31); } /** @@ -308,13 +309,13 @@ public void testMethodArrayResponseResource() { * @param source where is the yaml. */ private void compare(final Class clazz, final String source) { - compare(clazz, source, false); + compare(clazz, source, SpecVersion.V30); } - private void compare(final Class clazz, final String source, boolean openapi31) { + private void compare(final Class clazz, final String source, SpecVersion specVersion) { final String file = source + clazz.getSimpleName() + YAML_EXTENSION; try { - if (openapi31) { + if (specVersion.equals(SpecVersion.V31)) { compareAsYamlOAS31(clazz, getOpenAPIAsString(file)); } else { compareAsYaml(clazz, getOpenAPIAsString(file)); diff --git a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java index 5dd7e1de3f..fec06c26c0 100644 --- a/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java +++ b/modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java @@ -3168,7 +3168,7 @@ public void testSchemaAdditionalPropertiesBoolean() { @Test(description = "Test ArraySchema implementation annotations") public void testArraySchemaImplementation() { - SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI()); + SwaggerConfiguration config = swaggerConfiguration31(); Reader reader = new Reader(config); OpenAPI openAPI = reader.read(ArraySchemaImplementationResource.class); @@ -3317,7 +3317,7 @@ public void test4412PathWildcards() { @Test public void testOas31Petstore() { - SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI()); + SwaggerConfiguration config = swaggerConfiguration31(); Reader reader = new Reader(config); OpenAPI openAPI = reader.read(PetResource.class); @@ -3610,7 +3610,7 @@ public void testOas31Petstore() { @Test public void test31RefSiblings() { - SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI()); + SwaggerConfiguration config = swaggerConfiguration31(); Reader reader = new Reader(config); OpenAPI openAPI = reader.read(TagResource.class); @@ -3660,7 +3660,7 @@ public void test31RefSiblings() { @Test public void testSiblings() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); OpenAPI openAPI = reader.read(SiblingsResource.class); String yaml = "openapi: 3.1.0\n" + @@ -3696,7 +3696,7 @@ public void testSiblings() { @Test public void testSiblingsOnResource() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); OpenAPI openAPI = reader.read(SiblingsResourceSimple.class); String yaml = "openapi: 3.1.0\n" + @@ -3734,7 +3734,7 @@ public void testSiblingsOnResource() { @Test public void testSiblingsOnResourceResponse() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); OpenAPI openAPI = reader.read(SiblingsResourceResponse.class); String yaml = "openapi: 3.1.0\n" + @@ -3782,7 +3782,7 @@ public void testSiblingsOnResourceResponse() { @Test public void testSiblingsOnResourceRequestBody() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); OpenAPI openAPI = reader.read(SiblingsResourceRequestBody.class); String yaml = "openapi: 3.1.0\n" + @@ -3827,7 +3827,7 @@ public void testSiblingsOnResourceRequestBody() { @Test public void testSiblingsOnResourceRequestBodyMultiple() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); OpenAPI openAPI = reader.read(SiblingsResourceRequestBodyMultiple.class); String yaml = "openapi: 3.1.0\n" + @@ -3903,7 +3903,7 @@ public void testSiblingsOnResourceRequestBodyMultiple() { @Test public void testSiblingsOnProperty() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); Set> classes = new HashSet<>(Arrays.asList(SiblingPropResource.class, WebHookResource.class)); OpenAPI openAPI = reader.read(classes); String yaml = "openapi: 3.1.0\n" + @@ -3992,7 +3992,7 @@ public void testSiblingsOnProperty() { @Test public void testMisc31() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); Set> classes = new HashSet<>(Arrays.asList(Misc31Resource.class)); OpenAPI openAPI = reader.read(classes); String yaml = "openapi: 3.1.0\n" + @@ -4167,7 +4167,7 @@ public void test4446CyclicProp() { @Test public void testParameterMaximumValue() { - Reader reader = new Reader(new SwaggerConfiguration().openAPI(new OpenAPI()).openAPI31(true)); + Reader reader = new Reader(swaggerConfiguration31()); OpenAPI openAPI = reader.read(ParameterMaximumValueResource.class); String yaml = "openapi: 3.1.0\n" + @@ -5679,4 +5679,8 @@ public boolean isOpenAPI31Filter() { return true; } } + + private SwaggerConfiguration swaggerConfiguration31() { + return new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI()); + } }