Skip to content

Commit

Permalink
Do not set type for schemas generated from java.lang.Object (#1347)
Browse files Browse the repository at this point in the history
Fixes #1022

Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar authored Jan 31, 2023
1 parent 32168ea commit f511c93
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,31 @@
*/
public class OpenApiDataObjectScanner {

// Object
public static final Type OBJECT_TYPE = Type.create(DotName.createSimple(java.lang.Object.class.getName()), Type.Kind.CLASS);
// Collection (list-type things)
public static final DotName COLLECTION_INTERFACE_NAME = DotName.createSimple(Collection.class.getName());
public static final Type COLLECTION_TYPE = Type.create(COLLECTION_INTERFACE_NAME, Type.Kind.CLASS);

// Iterable (also list-type things)
public static final DotName ITERABLE_INTERFACE_NAME = DotName.createSimple(Iterable.class.getName());
public static final Type ITERABLE_TYPE = Type.create(ITERABLE_INTERFACE_NAME, Type.Kind.CLASS);

// Map
public static final DotName MAP_INTERFACE_NAME = DotName.createSimple(Map.class.getName());
public static final Type MAP_TYPE = Type.create(MAP_INTERFACE_NAME, Type.Kind.CLASS);

// Set
public static final DotName SET_INTERFACE_NAME = DotName.createSimple(java.util.Set.class.getName());
public static final Type SET_TYPE = Type.create(SET_INTERFACE_NAME, Type.Kind.CLASS);

// Enum
public static final DotName ENUM_INTERFACE_NAME = DotName.createSimple(Enum.class.getName());
public static final Type ENUM_TYPE = Type.create(ENUM_INTERFACE_NAME, Type.Kind.CLASS);

// String type
public static final Type STRING_TYPE = Type.create(DotName.createSimple(String.class.getName()), Type.Kind.CLASS);

// Array type
public static final Type ARRAY_TYPE_OBJECT = Type.create(DotName.createSimple("[Ljava.lang.Object;"), Type.Kind.ARRAY);
public static final Type ARRAY_TYPE_OBJECT = Type.create(DotName.createSimple(Map[].class.getName()), Type.Kind.ARRAY);

private static ClassInfo collectionStandin;
private static ClassInfo iterableStandin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static io.smallrye.openapi.runtime.scanner.OpenApiDataObjectScanner.ENUM_TYPE;
import static io.smallrye.openapi.runtime.scanner.OpenApiDataObjectScanner.ITERABLE_TYPE;
import static io.smallrye.openapi.runtime.scanner.OpenApiDataObjectScanner.MAP_TYPE;
import static io.smallrye.openapi.runtime.scanner.OpenApiDataObjectScanner.OBJECT_TYPE;
import static io.smallrye.openapi.runtime.scanner.OpenApiDataObjectScanner.SET_TYPE;
import static io.smallrye.openapi.runtime.scanner.OpenApiDataObjectScanner.STRING_TYPE;
import static io.smallrye.openapi.runtime.util.TypeUtil.isTerminalType;
Expand Down Expand Up @@ -128,7 +127,7 @@ public Type processType() {

// Raw Map
if (isA(type, MAP_TYPE)) {
return OBJECT_TYPE;
return MAP_TYPE;
}

// Simple case: bare class or primitive type.
Expand Down Expand Up @@ -225,7 +224,7 @@ private Type readParameterizedType(ParameterizedType pType, Schema schema) {
schema.additionalPropertiesSchema(readGenericValueType(valueType, schema));
}

typeRead = OBJECT_TYPE;
typeRead = MAP_TYPE;

if (TypeUtil.allowRegistration(context, pType)) {
// This type will be inspected later, if necessary.
Expand Down
13 changes: 10 additions & 3 deletions core/src/main/java/io/smallrye/openapi/runtime/util/TypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class TypeUtil {
private static final DotName DOTNAME_VOID = DotName.createSimple(Void.class.getName());

private static final String UUID_PATTERN = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}";
private static final TypeWithFormat ANY = TypeWithFormat.anyType().build();
private static final TypeWithFormat STRING_FORMAT = TypeWithFormat.of(SchemaType.STRING).build();
private static final TypeWithFormat BINARY_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.BINARY).build();
private static final TypeWithFormat BYTE_FORMAT = TypeWithFormat.of(SchemaType.STRING).format(DataFormat.BYTE).build();
Expand Down Expand Up @@ -118,6 +119,8 @@ public class TypeUtil {

// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#dataTypeFormat
static {
TYPE_MAP.put(DOTNAME_OBJECT, ANY);

// String
TYPE_MAP.put(DotName.createSimple(String.class.getName()), STRING_FORMAT);
TYPE_MAP.put(DotName.createSimple(StringBuffer.class.getName()), STRING_FORMAT);
Expand Down Expand Up @@ -963,7 +966,6 @@ static class Builder {
private boolean opaque;

Builder(SchemaType schemaType) {
Objects.requireNonNull(schemaType);
properties.put(SchemaConstant.PROP_TYPE, schemaType);
}

Expand Down Expand Up @@ -1005,9 +1007,14 @@ TypeWithFormat build() {
}

static Builder of(SchemaType schemaType) {
Objects.requireNonNull(schemaType);
return new Builder(schemaType);
}

static Builder anyType() {
return new Builder(null);
}

private final Map<String, Object> properties;
private final boolean opaque;

Expand All @@ -1017,8 +1024,8 @@ private TypeWithFormat(Map<String, Object> properties, boolean opaque) {
}

boolean isSchemaType(SchemaType... schemaTypes) {
return Arrays.stream(schemaTypes)
.anyMatch(properties.get(SchemaConstant.PROP_TYPE)::equals);
SchemaType type = (SchemaType) properties.get(SchemaConstant.PROP_TYPE);
return type != null && Arrays.stream(schemaTypes).anyMatch(type::equals);
}

Map<String, Object> getProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"type": "string"
},
"prop2": {
"type": "object"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"1PairStringString": {
"type": "array",
"items": {
"type": "object"
}
},
"TestBean": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@
"example": "F176f717c7a71"
},
"data": {
"description": "The business data object",
"type": "object"
"description": "The business data object"
},
"kind": {
"description": "The class-name of the business data object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"type" : "object",
"properties" : {
"bar" : {
"type" : "object"
},
"foo" : {
"maxLength" : 123456,
Expand Down Expand Up @@ -62,7 +61,6 @@
"type" : "object",
"properties" : {
"bar" : {
"type" : "object"
},
"foo" : {
"maxLength" : 123456,
Expand Down Expand Up @@ -102,7 +100,6 @@
"type" : "object",
"properties" : {
"bar" : {
"type" : "object"
},
"foo" : {
"maxLength" : 123456,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"required" : [ "bar", "foo" ],
"properties" : {
"bar" : {
"type" : "object"
},
"foo" : {
"format" : "int32",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@
"data": {
"type": "array",
"items": {
"type": "object"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"type": "object",
"properties": {
"bar": {
"type": "object"
},
"foo": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"required" : [ "bar", "foo" ],
"properties" : {
"bar" : {
"type" : "object"
},
"foo" : {
"format" : "int32",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
"type": "object",
"properties": {
"theQ": {
"type": "object"
},
"theT": {
"$ref": "#/components/schemas/Baz"
},
"ultimateTShouldBeQ": {
"type": "object"
}
}
},
Expand Down Expand Up @@ -186,14 +184,11 @@
"type": "object",
"properties": {
"qAgain": {
"type": "object"
},
"qAgain3": {
"type": "object"
},
"qValue": {
"description": "Ah, Q, my favourite variable!",
"type": "object"
"description": "Ah, Q, my favourite variable!"
},
"tAgain2": {
"type": "string"
Expand Down Expand Up @@ -306,7 +301,6 @@
"type": "object",
"properties": {
"bar": {
"type": "object"
},
"foo": {
"maxLength": 123456,
Expand Down Expand Up @@ -418,7 +412,6 @@
"barSuper": {
"type": "array",
"items": {
"type": "object"
}
},
"bareCollection": {
Expand All @@ -433,7 +426,6 @@
"blahMap": {
"type": "object",
"additionalProperties": {
"type": "object"
}
},
"booking": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
"type": "object",
"properties": {
"theQ": {
"type": "object"
},
"theT": {
"$ref": "#/components/schemas/Baz"
},
"ultimateTShouldBeQ": {
"type": "object"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,59 @@
{
"openapi" : "3.0.3",
"paths" : {
"/multi-produce-consume" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/plain" : {
"schema" : {
"type" : "object"
"openapi": "3.0.3",
"paths": {
"/multi-produce-consume": {
"get": {
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
}
}
}
}
}
},
"post" : {
"requestBody" : {
"content" : {
"text/plain" : {
"schema" : {
"type" : "object"
"post": {
"requestBody": {
"content": {
"text/plain": {
"schema": {
}
},
"text/html" : {
"schema" : {
"type" : "object"
"text/html": {
"schema": {
}
},
"application/json" : {
"schema" : {
"type" : "object"
"application/json": {
"schema": {
}
},
"application/xml" : {
"schema" : {
"type" : "object"
"application/xml": {
"schema": {
}
}
}
},
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"type" : "object"
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
}
},
"application/xml" : {
"schema" : {
"type" : "object"
"application/xml": {
"schema": {
}
},
"text/plain" : {
"schema" : {
"type" : "object"
"text/plain": {
"schema": {
}
},
"text/html" : {
"schema" : {
"type" : "object"
"text/html": {
"schema": {
}
}
}
Expand All @@ -71,4 +62,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"components" : {
"schemas" : {
"test.io.smallrye.openapi.runtime.scanner.entities.SpecialCaseTestContainer" : {
"type" : "array",
"items" : {
"type" : "object"
"components": {
"schemas": {
"test.io.smallrye.openapi.runtime.scanner.entities.SpecialCaseTestContainer": {
"type": "array",
"items": {
}
}
}
Expand Down
Loading

0 comments on commit f511c93

Please sign in to comment.