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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.converter.ResolvedSchema;
import io.swagger.v3.core.oas.models.ModelWithArrayOfSubclasses;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Json31;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import io.swagger.v3.core.util.ResourceUtils;
import org.testng.annotations.Test;

import java.nio.file.Files;
import java.nio.file.Paths;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;

Expand All @@ -21,7 +22,7 @@ public class ArrayOfSubclassTest {
public void extractSubclassArray_oas31() throws Exception {
ResolvedSchema schema = ModelConverters.getInstance(true).readAllAsResolvedSchema(ModelWithArrayOfSubclasses.Holder.class);
assertNotNull(schema);
String expectedJson = new String(Files.readAllBytes(Paths.get("src/test/resources/converting/ArrayOfSubclassTest_expected31.json")));
String expectedJson = ResourceUtils.loadClassResource(getClass(), "converting/ArrayOfSubclassTest_expected31.json");
String actualJson = Json31.pretty(schema);
ObjectMapper mapper = new ObjectMapper();
JsonNode expectedNode = mapper.readTree(expectedJson);
Expand All @@ -33,8 +34,8 @@ public void extractSubclassArray_oas31() throws Exception {
public void extractSubclassArray_oas30() throws Exception {
ResolvedSchema schema = ModelConverters.getInstance(false).readAllAsResolvedSchema(ModelWithArrayOfSubclasses.Holder.class);
assertNotNull(schema);
String expectedJson = new String(Files.readAllBytes(Paths.get("src/test/resources/converting/ArrayOfSubclassTest_expected30.json")));
String actualJson = Json31.pretty(schema);
String expectedJson = ResourceUtils.loadClassResource(getClass(), "converting/ArrayOfSubclassTest_expected30.json");
String actualJson = Json.pretty(schema);
ObjectMapper mapper = new ObjectMapper();
JsonNode expectedNode = mapper.readTree(expectedJson);
JsonNode actualNode = mapper.readTree(actualJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.converter.ResolvedSchema;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.util.Configuration;
import io.swagger.v3.core.util.Json;
Expand All @@ -10,6 +12,7 @@
import org.testng.annotations.Test;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;

import static org.testng.Assert.*;
Expand All @@ -18,11 +21,11 @@
* Reproduces GitHub Issue #5001
* Native support for @Nullable annotations to generate proper nullable types
*
* Tests that @Nullable annotation is recognized and generates appropriate nullable output:
* <p>Tests that @Nullable annotation is recognized and generates appropriate nullable output:
* - OAS 3.0: nullable keyword
* - OAS 3.1: type array with "null"
*
* Note: This test uses javax.annotation.Nullable which is automatically transformed to
* <p>Note: This test uses javax.annotation.Nullable which is automatically transformed to
* jakarta.annotation.Nullable in the swagger-core-jakarta module via the Eclipse Transformer.
*
* @see <a href="https://github.com/swagger-api/swagger-core/issues/5001">...</a>
Expand All @@ -33,7 +36,7 @@ public class Issue5001Test {
* Tests @Nullable annotation with OAS 3.1 (type array output)
*/
@Test
public void testNullableWithOAS31() throws Exception {
public void testNullableWithOAS31() {
final ModelResolver modelResolver = new ModelResolver(Json31.mapper());
Configuration configuration = new Configuration();
configuration.setOpenAPI31(true);
Expand Down Expand Up @@ -72,7 +75,7 @@ public void testNullableWithOAS31() throws Exception {
* Tests @Nullable annotation with OAS 3.0 (nullable keyword output)
*/
@Test
public void testNullableWithOAS30() throws Exception {
public void testNullableWithOAS30() {
final ModelResolver modelResolver = new ModelResolver(Json.mapper());
Configuration configuration = new Configuration();
configuration.setOpenAPI31(false);
Expand Down Expand Up @@ -103,7 +106,7 @@ public void testNullableWithOAS30() throws Exception {
* Tests explicit @Schema annotations with OAS 3.1
*/
@Test
public void testExplicitSchemaAnnotationsWithOAS31() throws Exception {
public void testExplicitSchemaAnnotationsWithOAS31() {
final ModelResolver modelResolver = new ModelResolver(Json31.mapper());
Configuration configuration = new Configuration();
configuration.setOpenAPI31(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.swagger.v3.core.issues;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.converter.ResolvedSchema;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Json31;
import io.swagger.v3.core.util.ResourceUtils;
import org.testng.annotations.Test;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;

import static org.testng.Assert.assertEquals;

/**
* Reproduces GitHub Issue #5077
* Issue using Nullable annotation for OAS 3.1
*
* <p>Tests that @Nullable annotation does not affect the item in a list:
*
* <p>Note: This test uses javax.annotation.Nullable which is automatically transformed to
* jakarta.annotation.Nullable in the swagger-core-jakarta module via the Eclipse Transformer.
*
*/
public class Issue5077Test {

@Test
public void testOAS31() throws IOException {
ResolvedSchema schema = ModelConverters.getInstance(true)
.readAllAsResolvedSchema(ModelWithObjectAndList.class);

String expectedJson = ResourceUtils.loadClassResource(getClass(), "specFiles/NullableFieldsOAS31.json");
String actualJson = Json31.pretty(schema);
ObjectMapper mapper = new ObjectMapper();
JsonNode expectedNode = mapper.readTree(expectedJson);
JsonNode actualNode = mapper.readTree(actualJson);
assertEquals(actualNode, expectedNode);
}

@Test
public void testOAS30() throws IOException {
ResolvedSchema schema = ModelConverters.getInstance()
.readAllAsResolvedSchema(ModelWithObjectAndList.class);

String expectedJson = ResourceUtils.loadClassResource(getClass(), "specFiles/NullableFieldsOAS30.json");
String actualJson = Json.pretty(schema);
ObjectMapper mapper = new ObjectMapper();
JsonNode expectedNode = mapper.readTree(expectedJson);
JsonNode actualNode = mapper.readTree(actualJson);
assertEquals(actualNode, expectedNode);
}

public static class ModelWithObjectAndList {

@Nullable
private String nullableString;

private String requiredString;

@Nullable
private Model nullableModel;

@Nullable
private List<String> strings;

@Nullable
private List<Model> models;

public String getNullableString() {
return nullableString;
}

public void setNullableString(String nullableString) {
this.nullableString = nullableString;
}

public String getRequiredString() {
return requiredString;
}

public void setRequiredString(String requiredString) {
this.requiredString = requiredString;
}

public Model getNullableModel() {
return nullableModel;
}

public void setNullableModel(Model model) {
this.nullableModel = model;
}

public List<Model> getModels() {
return models;
}

public void setModels(List<Model> models) {
this.models = models;
}

@Nullable
public List<String> getStrings() {
return strings;
}

public void setStrings(@Nullable List<String> strings) {
this.strings = strings;
}
}

public static class Model {
private String field;

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schema" : {
"description" : "The holder",
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
Expand All @@ -9,34 +9,36 @@
"type" : "string"
},
"baseArray" : {
"minItems" : 0,
"uniqueItems" : true,
"type" : "array",
"description" : "Thingy",
"items" : {
"$ref" : "#/components/schemas/Base"
},
"minItems" : 0,
"uniqueItems" : true
}
}
}
},
"description" : "The holder"
},
"referencedSchemas" : {
"Base" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
}
},
"description" : "Stuff",
"discriminator" : {
"propertyName" : "name",
"mapping" : {
"a" : "#/components/schemas/SubA",
"b" : "#/components/schemas/SubB"
}
},
"properties" : {
"name" : {
"type" : "string"
}
}
},
"Holder" : {
"description" : "The holder",
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
Expand All @@ -45,18 +47,19 @@
"type" : "string"
},
"baseArray" : {
"minItems" : 0,
"uniqueItems" : true,
"type" : "array",
"description" : "Thingy",
"items" : {
"$ref" : "#/components/schemas/Base"
},
"minItems" : 0,
"uniqueItems" : true
}
}
}
},
"description" : "The holder"
},
"SubA" : {
"description" : "The SubA class",
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
Expand All @@ -65,18 +68,20 @@
"type" : "integer",
"format" : "int64"
}
}
},
"description" : "The SubA class"
},
"SubB" : {
"description" : "The SubB class",
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
},
"friend" : {
"type" : "string"
}
}
},
"description" : "The SubB class"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"schema" : {
"type" : "object",
"properties" : {
"nullableString" : {
"type" : "string",
"nullable" : true
},
"requiredString" : {
"type" : "string"
},
"nullableModel" : {
"$ref" : "#/components/schemas/Model"
},
"strings" : {
"type" : "array",
"nullable" : true,
"items" : {
"type" : "string"
}
},
"models" : {
"type" : "array",
"nullable" : true,
"items" : {
"$ref" : "#/components/schemas/Model"
}
}
}
},
"referencedSchemas" : {
"Model" : {
"type" : "object",
"properties" : {
"field" : {
"type" : "string"
}
}
},
"ModelWithObjectAndList" : {
"type" : "object",
"properties" : {
"nullableString" : {
"type" : "string",
"nullable" : true
},
"requiredString" : {
"type" : "string"
},
"nullableModel" : {
"$ref" : "#/components/schemas/Model"
},
"strings" : {
"type" : "array",
"nullable" : true,
"items" : {
"type" : "string"
}
},
"models" : {
"type" : "array",
"nullable" : true,
"items" : {
"$ref" : "#/components/schemas/Model"
}
}
}
}
}
}
Loading