Skip to content

Commit

Permalink
♻️ (RUMM-1001): Handle types with additionalProperties in JsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Jan 20, 2021
1 parent 5ecdfeb commit d42f533
Show file tree
Hide file tree
Showing 38 changed files with 975 additions and 703 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ data class JsonDefinition(
@SerializedName("allOf") val allOf: List<JsonDefinition>?,
@SerializedName("properties") val properties: Map<String, JsonDefinition>?,
@SerializedName("definitions") val definitions: Map<String, JsonDefinition>?,
@SerializedName("readOnly") val readOnly: Boolean?
@SerializedName("readOnly") val readOnly: Boolean?,
@SerializedName("additionalProperties") val additionalProperties: JsonDefinition?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.gradle.plugin.jsonschema

enum class JsonPrimitiveType {
STRING, BOOLEAN, INTEGER, DOUBLE
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,18 @@ class JsonSchemaReader(
val type = definition.type
return when (type) {
JsonType.NULL -> TypeDefinition.Null(definition.description.orEmpty())
JsonType.BOOLEAN,
JsonType.NUMBER,
JsonType.INTEGER,
JsonType.STRING -> transformPrimitive(definition, typeName)
JsonType.BOOLEAN -> transformPrimitive(definition, JsonPrimitiveType.BOOLEAN, typeName)
JsonType.NUMBER -> transformPrimitive(definition, JsonPrimitiveType.DOUBLE, typeName)
JsonType.INTEGER -> transformPrimitive(definition, JsonPrimitiveType.INTEGER, typeName)
JsonType.STRING -> transformPrimitive(definition, JsonPrimitiveType.STRING, typeName)
JsonType.ARRAY -> transformArray(definition, typeName)
JsonType.OBJECT, null -> transformType(definition, typeName)
}
}

private fun transformPrimitive(
definition: JsonDefinition,
primitiveType: JsonPrimitiveType,
typeName: String
): TypeDefinition {
return if (!definition.enum.isNullOrEmpty()) {
Expand All @@ -156,7 +157,7 @@ class JsonSchemaReader(
transformConstant(definition.type, definition.constant, definition.description)
} else {
TypeDefinition.Primitive(
type = definition.type ?: JsonType.NULL,
type = primitiveType,
description = definition.description.orEmpty()
)
}
Expand Down Expand Up @@ -209,7 +210,7 @@ class JsonSchemaReader(
transformEnum(typeName, definition.type, definition.enum, definition.description)
} else if (definition.constant != null) {
transformConstant(definition.type, definition.constant, definition.description)
} else if (!definition.properties.isNullOrEmpty()) {
} else if (!definition.properties.isNullOrEmpty() || definition.additionalProperties != null) {
generateDataClass(typeName, definition)
} else if (!definition.allOf.isNullOrEmpty()) {
generateTypeAllOf(typeName, definition.allOf)
Expand Down Expand Up @@ -255,11 +256,13 @@ class JsonSchemaReader(
)
properties.add(TypeProperty(name, propertyType, !required, readOnly))
}
val additional = definition.additionalProperties?.let { transform(it, "?") }

return TypeDefinition.Class(
name = typeName,
description = definition.description.orEmpty(),
properties = properties
properties = properties,
additionalProperties = additional
)
}

Expand Down
Loading

0 comments on commit d42f533

Please sign in to comment.