Skip to content
Open
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
@@ -1,13 +1,13 @@
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;
import io.swagger.v3.core.util.Configuration;
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;

Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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<Class<?>> classes = new HashSet<>(Arrays.asList(SiblingPropResource.class, WebHookResource.class));
OpenAPI openAPI = reader.read(classes);
String yaml = "openapi: 3.1.0\n" +
Expand Down Expand Up @@ -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<Class<?>> classes = new HashSet<>(Arrays.asList(Misc31Resource.class));
OpenAPI openAPI = reader.read(classes);
String yaml = "openapi: 3.1.0\n" +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -5679,4 +5679,8 @@ public boolean isOpenAPI31Filter() {
return true;
}
}

private SwaggerConfiguration swaggerConfiguration31() {
return new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI());
}
}