-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: resolve PrimitiveType casting for OpenAPI 3.1 schemas (#4963) #4989
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
base: master
Are you sure you want to change the base?
Changes from all commits
f4884c4
debfed1
7c0a6af
4baa804
e8cfe79
5c655c6
02179c1
23253a2
26ab407
ecb8929
b7791fa
e4d7b45
75bc935
935dacd
1e1711a
1f0d050
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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) { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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") { | ||
|
@@ -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) { | ||
|
@@ -239,7 +239,7 @@ public Schema createProperty() { | |
} | ||
@Override | ||
public Schema createProperty31() { | ||
return new JsonSchema(); | ||
return new JsonSchema().typesItem("object").type("object"); | ||
} | ||
}; | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -374,6 +375,9 @@ public Schema createProperty31() { | |
addKeys(names, OBJECT, "object"); | ||
NAMES = Collections.unmodifiableMap(names); | ||
|
||
// Add "LocalTime" class mappings | ||
enablePartialTime(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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() { | ||
} | ||
} | ||
|
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()); | ||
} | ||
} |
There was a problem hiding this comment.
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