From 8ae74705bab0a5541e71c1a0f1985296a108baf7 Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+unredundant@users.noreply.github.com> Date: Tue, 16 Aug 2022 09:01:22 -0600 Subject: [PATCH] fix: uuid schema (#296) --- .../kotlin/io/bkbn/kompendium/json/schema/SchemaGenerator.kt | 2 ++ .../bkbn/kompendium/json/schema/definition/TypeDefinition.kt | 5 +++++ .../io/bkbn/kompendium/json/schema/SchemaGeneratorTest.kt | 4 ++++ json-schema/src/test/resources/T0017__scalar_uuid.json | 4 ++++ 4 files changed, 15 insertions(+) create mode 100644 json-schema/src/test/resources/T0017__scalar_uuid.json diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/SchemaGenerator.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/SchemaGenerator.kt index a5e8ac423..ef4c91f49 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/SchemaGenerator.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/SchemaGenerator.kt @@ -14,6 +14,7 @@ import kotlin.reflect.KClass import kotlin.reflect.KType import kotlin.reflect.full.isSubclassOf import kotlin.reflect.typeOf +import java.util.UUID object SchemaGenerator { inline fun fromTypeToSchema(cache: MutableMap = mutableMapOf()) = @@ -37,6 +38,7 @@ object SchemaGenerator { Float::class -> checkForNull(type, TypeDefinition.FLOAT) String::class -> checkForNull(type, TypeDefinition.STRING) Boolean::class -> checkForNull(type, TypeDefinition.BOOLEAN) + UUID::class -> checkForNull(type, TypeDefinition.UUID) else -> when { clazz.isSubclassOf(Enum::class) -> EnumHandler.handle(type, clazz) clazz.isSubclassOf(Collection::class) -> CollectionHandler.handle(type, cache) diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/TypeDefinition.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/TypeDefinition.kt index 40b331c78..b7f34157f 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/TypeDefinition.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/TypeDefinition.kt @@ -40,6 +40,11 @@ data class TypeDefinition( type = "string" ) + val UUID = TypeDefinition( + type = "string", + format = "uuid" + ) + val BOOLEAN = TypeDefinition( type = "boolean" ) diff --git a/json-schema/src/test/kotlin/io/bkbn/kompendium/json/schema/SchemaGeneratorTest.kt b/json-schema/src/test/kotlin/io/bkbn/kompendium/json/schema/SchemaGeneratorTest.kt index 181de9c62..765212e6c 100644 --- a/json-schema/src/test/kotlin/io/bkbn/kompendium/json/schema/SchemaGeneratorTest.kt +++ b/json-schema/src/test/kotlin/io/bkbn/kompendium/json/schema/SchemaGeneratorTest.kt @@ -12,6 +12,7 @@ import io.kotest.assertions.json.shouldEqualJson import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.DescribeSpec import kotlinx.serialization.json.Json +import java.util.UUID class SchemaGeneratorTest : DescribeSpec({ describe("Scalars") { @@ -24,6 +25,9 @@ class SchemaGeneratorTest : DescribeSpec({ it("Can generate the schema for a String") { jsonSchemaTest("T0003__scalar_string.json") } + it("Can generate the schema for a UUID") { + jsonSchemaTest("T0017__scalar_uuid.json") + } } describe("Objects") { it("Can generate the schema for a simple object") { diff --git a/json-schema/src/test/resources/T0017__scalar_uuid.json b/json-schema/src/test/resources/T0017__scalar_uuid.json new file mode 100644 index 000000000..a72687cc2 --- /dev/null +++ b/json-schema/src/test/resources/T0017__scalar_uuid.json @@ -0,0 +1,4 @@ +{ + "type": "string", + "format": "uuid" +}