Skip to content
Merged
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
Expand Up @@ -59,7 +59,6 @@
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.JsonSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.NumberSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
Expand Down Expand Up @@ -153,6 +152,7 @@ public ObjectMapper objectMapper() {

@Override
public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> next) {
boolean implicitObject = false;
boolean isPrimitive = false;
Schema model = null;
List<String> requiredProps = new ArrayList<>();
Expand Down Expand Up @@ -269,7 +269,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
resolveArraySchema(annotatedType, schema, resolvedArrayAnnotation);
Schema innerSchema = null;

Schema primitive = PrimitiveType.createProperty(cls);
Schema primitive = PrimitiveType.createProperty(cls, openapi31);
if (primitive != null) {
innerSchema = primitive;
} else {
Expand Down Expand Up @@ -310,31 +310,31 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
primitiveType = PrimitiveType.fromName(resolvedSchemaAnnotation.type());
}
if (primitiveType != null) {
Schema primitive = primitiveType.createProperty();
Schema primitive = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
model = primitive;
isPrimitive = true;

}
}

if (model == null && type.isEnumType()) {
model = new StringSchema();
model = openapi31 ? new JsonSchema().typesItem("string") : new StringSchema();
_addEnumProps(type.getRawClass(), model);
isPrimitive = true;
}
if (model == null) {
if (resolvedSchemaAnnotation != null && StringUtils.isEmpty(resolvedSchemaAnnotation.type())) {
PrimitiveType primitiveType = PrimitiveType.fromTypeAndFormat(type, resolvedSchemaAnnotation.format());
if (primitiveType != null) {
model = primitiveType.createProperty();
model = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
isPrimitive = true;
}
}

if (model == null) {
PrimitiveType primitiveType = PrimitiveType.fromType(type);
if (primitiveType != null) {
model = primitiveType.createProperty();
model = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
isPrimitive = true;
}
}
Expand Down Expand Up @@ -561,10 +561,16 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
}
} else if (isComposedSchema) {
model = new ComposedSchema().name(name);
if (openapi31 && resolvedArrayAnnotation == null) {
model.addType("object");
if (
(openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
(!openapi31 && (!Boolean.FALSE.equals(PrimitiveType.explicitObjectType)))) {
if (openapi31 && resolvedArrayAnnotation == null) {
model.addType("object");
} else {
model.type("object");
}
} else {
model.type("object");
implicitObject = true;
}
} else {
AnnotatedType aType = ReferenceTypeUtils.unwrapReference(annotatedType);
Expand All @@ -573,10 +579,16 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
return model;
} else {
model = new Schema().name(name);
if (openapi31 && resolvedArrayAnnotation == null) {
model.addType("object");
if (
(openapi31 && Boolean.TRUE.equals(PrimitiveType.explicitObjectType)) ||
(!openapi31 && (!Boolean.FALSE.equals(PrimitiveType.explicitObjectType)))) {
if (openapi31 && resolvedArrayAnnotation == null) {
model.addType("object");
} else {
model.type("object");
}
} else {
model.type("object");
implicitObject = true;
}
}
}
Expand Down Expand Up @@ -1080,11 +1092,18 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
// define the model here to support self/cyclic referencing of models
context.defineModel(name, model, annotatedType, null);
}

// check if it has "object" related keywords
if (isInferredObjectSchema(model) && model.get$ref() == null) {
if (openapi31 && model.getTypes() == null) {
model.addType("object");
} else if (!openapi31 && model.getType() == null){
model.type("object");
}
}
Schema.SchemaResolution resolvedSchemaResolution = AnnotationsUtils.resolveSchemaResolution(this.schemaResolution, resolvedSchemaAnnotation);

if (model != null && annotatedType.isResolveAsRef() &&
(isComposedSchema || isObjectSchema(model)) &&
(isComposedSchema || isObjectSchema(model) || implicitObject) &&
StringUtils.isNotBlank(model.getName())) {
if (context.getDefinedModels().containsKey(model.getName())) {
if (!Schema.SchemaResolution.INLINE.equals(resolvedSchemaResolution)) {
Expand Down Expand Up @@ -1290,9 +1309,13 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
} else {
n = _intr().findEnumValue(en);
}
if (property instanceof StringSchema) {
StringSchema sp = (StringSchema) property;
sp.addEnumItem(n);
if (isStringSchema(property)) {
if (openapi31) {
property.addEnumItemObject(n);
} else {
StringSchema sp = (StringSchema) property;
sp.addEnumItem(n);
}
}
}
}
Expand Down Expand Up @@ -1431,7 +1454,7 @@ protected Schema processAsId(String propertyName, AnnotatedType type,
final AnnotatedMember propMember = def.getPrimaryMember();
final JavaType propType = propMember.getType();
if (PrimitiveType.fromType(propType) != null) {
return PrimitiveType.createProperty(propType);
return PrimitiveType.createProperty(propType, openapi31);
} else {
List<Annotation> list = new ArrayList<>();
for (Annotation a : propMember.annotations()) {
Expand Down Expand Up @@ -2593,7 +2616,7 @@ protected void resolveDiscriminatorProperty(JavaType type, ModelConverterContext
modelToUpdate = context.getDefinedModels().get(model.get$ref().substring(SCHEMA_COMPONENT_PREFIX));
}
if (modelToUpdate.getProperties() == null || !modelToUpdate.getProperties().keySet().contains(typeInfoProp)) {
Schema discriminatorSchema = new StringSchema().name(typeInfoProp);
Schema discriminatorSchema = openapi31 ? new JsonSchema().typesItem("string").name(typeInfoProp) : new StringSchema().name(typeInfoProp);
modelToUpdate.addProperties(typeInfoProp, discriminatorSchema);
if (modelToUpdate.getRequired() == null || !modelToUpdate.getRequired().contains(typeInfoProp)) {
modelToUpdate.addRequiredItem(typeInfoProp);
Expand Down Expand Up @@ -3429,7 +3452,20 @@ public void setConfiguration(Configuration configuration) {
}

protected boolean isObjectSchema(Schema schema) {
return (schema.getTypes() != null && schema.getTypes().contains("object")) || "object".equals(schema.getType()) || (schema.getType() == null && schema.getProperties() != null && !schema.getProperties().isEmpty());
return (schema.getTypes() != null && schema.getTypes().contains("object")) || "object".equals(schema.getType()) || (schema.getType() == null && ((schema.getProperties() != null && !schema.getProperties().isEmpty()) || (schema.getPatternProperties() != null && !schema.getPatternProperties().isEmpty())));
}

protected boolean isInferredObjectSchema(Schema schema) {
return ((schema.getProperties() != null && !schema.getProperties().isEmpty())
|| (schema.getPatternProperties() != null && !schema.getPatternProperties().isEmpty())
|| (schema.getAdditionalProperties() != null)
|| (schema.getUnevaluatedProperties() != null)
|| (schema.getRequired() != null && !schema.getRequired().isEmpty())
|| (schema.getPropertyNames() != null)
|| (schema.getDependentRequired() != null && !schema.getDependentRequired().isEmpty())
|| (schema.getDependentSchemas() != null && !schema.getDependentSchemas().isEmpty())
|| (schema.getMinProperties() != null && schema.getMinProperties() > 0)
|| (schema.getMaxProperties() != null && schema.getMaxProperties() > 0));
}

protected boolean isArraySchema(Schema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public static Optional<Schema> getArraySchema(io.swagger.v3.oas.annotations.medi
arraySchemaObject = new ArraySchema();
} else {
if (existingSchema == null) {
arraySchemaObject = new ArraySchema();
arraySchemaObject = new JsonSchema().typesItem("array");
} else {
arraySchemaObject = existingSchema;
}
Expand Down Expand Up @@ -889,7 +889,7 @@ public static Schema resolveSchemaFromType(Class<?> schemaImplementation, Compon
Schema schemaObject;
PrimitiveType primitiveType = PrimitiveType.fromType(schemaImplementation);
if (primitiveType != null) {
schemaObject = primitiveType.createProperty();
schemaObject = openapi31 ? primitiveType.createProperty31() : primitiveType.createProperty();
} else {
schemaObject = new Schema();
ResolvedSchema resolvedSchema = null;
Expand Down Expand Up @@ -1583,7 +1583,7 @@ public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.C
}
if (annotationContent.schemaProperties().length > 0) {
if (mediaType.getSchema() == null) {
mediaType.schema(new Schema<Object>().type("object"));
mediaType.schema(openapi31 ? new JsonSchema().typesItem("object") : new Schema<Object>().type("object"));
}
Schema oSchema = mediaType.getSchema();
for (SchemaProperty sp: annotationContent.schemaProperties()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.swagger.v3.oas.models.media.ArbitrarySchema;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.BooleanSchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
Expand All @@ -32,6 +33,15 @@

public class ModelDeserializer extends JsonDeserializer<Schema> {

static Boolean useArbitrarySchema = false;
static {
if (System.getenv(Schema.USE_ARBITRARY_SCHEMA_PROPERTY) != null) {
useArbitrarySchema = Boolean.parseBoolean(System.getenv(Schema.USE_ARBITRARY_SCHEMA_PROPERTY));
} else if (System.getProperty(Schema.USE_ARBITRARY_SCHEMA_PROPERTY) != null) {
useArbitrarySchema = Boolean.parseBoolean(System.getProperty(Schema.USE_ARBITRARY_SCHEMA_PROPERTY));
}
}

protected boolean openapi31 = false;
@Override
public Schema deserialize(JsonParser jp, DeserializationContext ctxt)
Expand Down Expand Up @@ -85,18 +95,18 @@ public Schema deserialize(JsonParser jp, DeserializationContext ctxt)
schema = Json.mapper().convertValue(node, StringSchema.class);
}
} else if (type.textValue().equals("object")) {
schema = deserializeObjectSchema(node);
schema = deserializeArbitraryOrObjectSchema(node, true);
}
} else if (node.get("$ref") != null) {
schema = new Schema().$ref(node.get("$ref").asText());
} else { // assume object
schema = deserializeObjectSchema(node);
} else {
schema = deserializeArbitraryOrObjectSchema(node, false);
}

return schema;
}

private Schema deserializeObjectSchema(JsonNode node) {
private Schema deserializeArbitraryOrObjectSchema(JsonNode node, boolean alwaysObject) {
JsonNode additionalProperties = node.get("additionalProperties");
Schema schema = null;
if (additionalProperties != null) {
Expand All @@ -117,7 +127,11 @@ private Schema deserializeObjectSchema(JsonNode node) {
schema = ms;
}
} else {
schema = Json.mapper().convertValue(node, ObjectSchema.class);
if (!Boolean.TRUE.equals(useArbitrarySchema) || alwaysObject) {
schema = Json.mapper().convertValue(node, ObjectSchema.class);
} else {
schema = Json.mapper().convertValue(node, ArbitrarySchema.class);
}
}
if (schema != null) {
schema.jsonSchema(Json31.jsonSchemaAsMap(node));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ public static Parameter applyAnnotations(
if (parameter.getSchema() == null) {
parameter.setSchema(new ArraySchema());
}
if (parameter.getSchema() instanceof ArraySchema) {
ArraySchema as = (ArraySchema) parameter.getSchema();
if (isArraySchema(parameter.getSchema())) {
Schema as = parameter.getSchema();
Integer min = (Integer) annotation.annotationType().getMethod("min").invoke(annotation);
if (min != null) {
as.setMinItems(min);
Expand All @@ -281,10 +281,9 @@ public static Parameter applyAnnotations(
}
}
if (paramSchema != null) {
if (paramSchema instanceof ArraySchema) {
ArraySchema as = (ArraySchema) paramSchema;
if (isArraySchema(paramSchema)) {
if (defaultValue != null) {
as.getItems().setDefault(defaultValue);
paramSchema.getItems().setDefault(defaultValue);
}
} else {
if (defaultValue != null) {
Expand All @@ -295,6 +294,10 @@ public static Parameter applyAnnotations(
return parameter;
}

public static boolean isArraySchema(Schema schema) {
return "array".equals(schema.getType()) || (schema.getTypes() != null && schema.getTypes().contains("array"));
}

public static void setParameterExplode(Parameter parameter, io.swagger.v3.oas.annotations.Parameter p) {
if (isExplodable(p, parameter)) {
if (Explode.TRUE.equals(p.explode())) {
Expand Down
Loading
Loading