Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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 @@ -951,7 +951,10 @@ public static Schema resolveSchemaFromType(Class<?> schemaImplementation, Compon
existingSchemaObject = reResolvedSchema.get();
}
}
if (StringUtils.isBlank(existingSchemaObject.get$ref()) && StringUtils.isBlank(existingSchemaObject.getType())) {
boolean doesSchemaHaveTypes = existingSchemaObject.getTypes() == null || existingSchemaObject.getTypes().isEmpty();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the method here is misleading. It returns true when empty/ null. Please rename it

if (StringUtils.isBlank(existingSchemaObject.get$ref())
&& StringUtils.isBlank(existingSchemaObject.getType())
&& doesSchemaHaveTypes) {
// default to string
existingSchemaObject.setType("string");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string");
return new JsonSchema().typesItem("string").type("string");
}
},
BOOLEAN(Boolean.class, "boolean") {
Expand All @@ -48,7 +48,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("boolean");
return new JsonSchema().typesItem("boolean").type("boolean");
}
},
BYTE(Byte.class, "byte") {
Expand All @@ -63,7 +63,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("byte");
return new JsonSchema().typesItem("string").type("string").format("byte");
}
},
BINARY(Byte.class, "binary") {
Expand All @@ -78,7 +78,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("binary");
return new JsonSchema().typesItem("string").type("string").format("binary");
}
},
URI(java.net.URI.class, "uri") {
Expand All @@ -88,7 +88,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("uri");
return new JsonSchema().typesItem("string").type("string").format("uri");
}
},
URL(java.net.URL.class, "url") {
Expand All @@ -98,7 +98,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("url");
return new JsonSchema().typesItem("string").type("string").format("url");
}
},
EMAIL(String.class, "email") {
Expand All @@ -108,7 +108,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("email");
return new JsonSchema().typesItem("string").type("string").format("email");
}
},
UUID(java.util.UUID.class, "uuid") {
Expand All @@ -118,7 +118,7 @@ public UUIDSchema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("uuid");
return new JsonSchema().typesItem("string").type("string").format("uuid");
}
},
INT(Integer.class, "integer") {
Expand All @@ -128,7 +128,7 @@ public IntegerSchema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("integer").format("int32");
return new JsonSchema().typesItem("integer").type("integer").format("int32");
}
},
LONG(Long.class, "long") {
Expand All @@ -138,7 +138,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("integer").format("int64");
return new JsonSchema().typesItem("integer").type("integer").format("int64");
}
},
FLOAT(Float.class, "float") {
Expand All @@ -148,7 +148,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("number").format("float");
return new JsonSchema().typesItem("number").type("number").format("float");
}
},
DOUBLE(Double.class, "double") {
Expand All @@ -158,7 +158,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("number").format("double");
return new JsonSchema().typesItem("number").type("number").format("double");
}
},
INTEGER(java.math.BigInteger.class) {
Expand All @@ -168,7 +168,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("integer");
return new JsonSchema().typesItem("integer").type("integer");
}
},
DECIMAL(java.math.BigDecimal.class, "number") {
Expand All @@ -178,7 +178,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("number");
return new JsonSchema().typesItem("number").type("number");
}
},
NUMBER(Number.class, "number") {
Expand All @@ -188,7 +188,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("number");
return new JsonSchema().typesItem("number").type("number");
}
},
DATE(DateStub.class, "date") {
Expand All @@ -198,7 +198,7 @@ public DateSchema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("date");
return new JsonSchema().typesItem("object").type("object").format("date");
}
},
DATE_TIME(java.util.Date.class, "date-time") {
Expand All @@ -208,7 +208,7 @@ public DateTimeSchema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("date-time");
return new JsonSchema().typesItem("object").type("object").format("date-time");
}
},
PARTIAL_TIME(java.time.LocalTime.class, "partial-time") {
Expand All @@ -218,7 +218,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("partial-time");
return new JsonSchema().typesItem("object").type("object").format("partial-time");
}
},
FILE(java.io.File.class, "file") {
Expand All @@ -228,7 +228,7 @@ public FileSchema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema().typesItem("string").format("binary");
return new JsonSchema().typesItem("string").type("string").format("binary");
}
},
OBJECT(Object.class) {
Expand All @@ -239,7 +239,7 @@ public Schema createProperty() {
}
@Override
public Schema createProperty31() {
return new JsonSchema();
return new JsonSchema().typesItem("object").type("object");
}
};

Expand Down Expand Up @@ -337,6 +337,7 @@ public Schema createProperty31() {
addKeys(keyClasses, NUMBER, Number.class);
addKeys(keyClasses, DATE, DateStub.class);
addKeys(keyClasses, DATE_TIME, java.util.Date.class);
// addKeys(keyClasses, PARTIAL_TIME, java.time.LocalTime.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this comment

addKeys(keyClasses, FILE, java.io.File.class);
addKeys(keyClasses, OBJECT, Object.class);
KEY_CLASSES = Collections.unmodifiableMap(keyClasses);
Expand Down Expand Up @@ -374,6 +375,9 @@ public Schema createProperty31() {
addKeys(names, OBJECT, "object");
NAMES = Collections.unmodifiableMap(names);

// Add "LocalTime" class mappings
enablePartialTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not a part of the solution to the #4963. Please consider creating a feature request for this change and describing why this change should be introduced. Some analysis would be necessary to confirm it's align with JSON Schema.

For now please remove it


if (System.getenv(Schema.EXPLICIT_OBJECT_SCHEMA_PROPERTY) != null) {
explicitObjectType = Boolean.parseBoolean(System.getenv(Schema.EXPLICIT_OBJECT_SCHEMA_PROPERTY));
} else if (System.getProperty(Schema.EXPLICIT_OBJECT_SCHEMA_PROPERTY) != null) {
Expand Down Expand Up @@ -572,7 +576,7 @@ private static <K> void addMultiKeys(Map<K, Collection<PrimitiveType>> map, Prim
}
}

private static class DateStub {
public static class DateStub {
private DateStub() {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,6 @@ public void testLocalTime() throws Exception {

ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

Schema model = context
.resolve(new AnnotatedType(TestObject2992.class));

SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "LocalTime:\n" +
" type: object\n" +
" properties:\n" +
" hour:\n" +
" type: integer\n" +
" format: int32\n" +
" minute:\n" +
" type: integer\n" +
" format: int32\n" +
" second:\n" +
" type: integer\n" +
" format: int32\n" +
" nano:\n" +
" type: integer\n" +
" format: int32\n" +
"TestObject2992:\n" +
" type: object\n" +
" properties:\n" +
" name:\n" +
" type: string\n" +
" a:\n" +
" $ref: \"#/components/schemas/LocalTime\"\n" +
" b:\n" +
" $ref: \"#/components/schemas/LocalTime\"\n" +
" c:\n" +
" $ref: \"#/components/schemas/LocalTime\"\n" +
" d:\n" +
" type: string\n" +
" format: date-time\n" +
" e:\n" +
" type: string\n" +
" format: date-time\n" +
" f:\n" +
" type: string\n" +
" format: date-time");

PrimitiveType.enablePartialTime();
context = new ModelConverterContextImpl(modelResolver);

context
.resolve(new AnnotatedType(TestObject2992.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void testOAS31Fields() {
" type: number\n" +
" status:\n" +
" type:\n" +
" - object\n" +
" - string\n" +
" - number\n" +
" intValue:\n" +
Expand Down Expand Up @@ -160,7 +161,8 @@ public void testOAS31Fields() {
" creditCard:\n" +
" $ref: \"#/components/schemas/CreditCard\"\n" +
" properties:\n" +
" extraObject: {}\n" +
" extraObject: \n" +
" type: object\n" +
"MultipleBaseBean:\n" +
" type: object\n" +
" description: MultipleBaseBean\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void testOAS31JavaRecord() {
" type: number\n" +
" Status:\n" +
" type:\n" +
" - object\n" +
" - string\n" +
" - number\n";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.swagger.v3.jaxrs2;

import io.swagger.v3.jaxrs2.resources.APIResponsesResource;
import io.swagger.v3.oas.integration.SwaggerConfiguration;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;

public class APIResponsesResourceTest {
private OpenAPI openAPI;

// Prepare list of resource schema URLs
@DataProvider(name = "schemas")
public String[] getSchemas() {
return new String[] {
"/postStringOrEmailSchemaContent",
"/postBooleanSchemaContent",
"/postByteOrBinarySchemaContent",
"/postURISchemaContent",
"/postURLSchemaContent",
"/postUUIDSchemaContent",
"/postIntegerSchemaContent",
"/postLongSchemaContent",
"/postFloatSchemaContent",
"/postDoubleSchemaContent",
"/postBigIntegerSchemaContent",
"/postBigDecimalSchemaContent",
"/postNumberSchemaContent",
"/postDateStubSchemaContent",
"/postDateSchemaContent",
"/postLocalTimeSchemaContent",
"/postFileSchemaContent",
"/postObjectSchemaContent"
};
}

@BeforeMethod
public void setUp() {
SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true);
Reader reader = new Reader(config);
openAPI = reader.read(APIResponsesResource.class);
}

private Schema getResponseSchema(String path) {
Operation postOperation = openAPI.getPaths().get(path).getPost();
return postOperation.getResponses().get("200").getContent().get("*/*").getSchema();
}

@Test(dataProvider = "schemas")
public void testSchemaAPIResource31(String schema) {
Schema responseSchema = getResponseSchema(schema);

// Value of field "type" must equal value of field "types"
assertEquals(responseSchema.getType(), responseSchema.getTypes().iterator().next());
}
}
Loading
Loading