fix: uuid schema (#296)

This commit is contained in:
Ryan Brink
2022-08-16 09:01:22 -06:00
committed by GitHub
parent 99f5cb5b86
commit 8ae74705ba
4 changed files with 15 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.KType import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.typeOf import kotlin.reflect.typeOf
import java.util.UUID
object SchemaGenerator { object SchemaGenerator {
inline fun <reified T : Any?> fromTypeToSchema(cache: MutableMap<String, JsonSchema> = mutableMapOf()) = inline fun <reified T : Any?> fromTypeToSchema(cache: MutableMap<String, JsonSchema> = mutableMapOf()) =
@ -37,6 +38,7 @@ object SchemaGenerator {
Float::class -> checkForNull(type, TypeDefinition.FLOAT) Float::class -> checkForNull(type, TypeDefinition.FLOAT)
String::class -> checkForNull(type, TypeDefinition.STRING) String::class -> checkForNull(type, TypeDefinition.STRING)
Boolean::class -> checkForNull(type, TypeDefinition.BOOLEAN) Boolean::class -> checkForNull(type, TypeDefinition.BOOLEAN)
UUID::class -> checkForNull(type, TypeDefinition.UUID)
else -> when { else -> when {
clazz.isSubclassOf(Enum::class) -> EnumHandler.handle(type, clazz) clazz.isSubclassOf(Enum::class) -> EnumHandler.handle(type, clazz)
clazz.isSubclassOf(Collection::class) -> CollectionHandler.handle(type, cache) clazz.isSubclassOf(Collection::class) -> CollectionHandler.handle(type, cache)

View File

@ -40,6 +40,11 @@ data class TypeDefinition(
type = "string" type = "string"
) )
val UUID = TypeDefinition(
type = "string",
format = "uuid"
)
val BOOLEAN = TypeDefinition( val BOOLEAN = TypeDefinition(
type = "boolean" type = "boolean"
) )

View File

@ -12,6 +12,7 @@ import io.kotest.assertions.json.shouldEqualJson
import io.kotest.assertions.throwables.shouldThrow import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.DescribeSpec import io.kotest.core.spec.style.DescribeSpec
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.util.UUID
class SchemaGeneratorTest : DescribeSpec({ class SchemaGeneratorTest : DescribeSpec({
describe("Scalars") { describe("Scalars") {
@ -24,6 +25,9 @@ class SchemaGeneratorTest : DescribeSpec({
it("Can generate the schema for a String") { it("Can generate the schema for a String") {
jsonSchemaTest<String>("T0003__scalar_string.json") jsonSchemaTest<String>("T0003__scalar_string.json")
} }
it("Can generate the schema for a UUID") {
jsonSchemaTest<UUID>("T0017__scalar_uuid.json")
}
} }
describe("Objects") { describe("Objects") {
it("Can generate the schema for a simple object") { it("Can generate the schema for a simple object") {

View File

@ -0,0 +1,4 @@
{
"type": "string",
"format": "uuid"
}