diff --git a/typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt b/typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt index 926fc06..e98fb4f 100644 --- a/typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt +++ b/typed/src/commonMain/kotlin/io/github/nomisrev/openapi/OpenAPITransformer.kt @@ -198,12 +198,18 @@ private class OpenAPITransformer(private val openAPI: OpenAPI) { is Type.Basic -> when (type) { Type.Basic.Array -> collection(context) - Type.Basic.Boolean -> Primitive.Boolean(default?.toString()?.toBoolean(), description) + Type.Basic.Boolean -> Primitive.Boolean(default?.toString()?.toBooleanStrictOrNull(), description) Type.Basic.Integer -> Primitive.Int(default?.toString()?.toIntOrNull(), description) Type.Basic.Number -> Primitive.Double(default?.toString()?.toDoubleOrNull(), description) Type.Basic.String -> if (format == "binary") Model.OctetStream(description) - else Primitive.String(default?.toString(), description) + else Primitive.String( + when(val default = default) { + is ExampleValue.Multiple -> + default.values.joinToString() + is ExampleValue.Single -> default.value + null -> null + }, description) Type.Basic.Object -> toObject(context) Type.Basic.Null -> TODO("Schema.Type.Basic.Null") } diff --git a/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/ModelTest.kt b/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/ModelTest.kt index d38473b..5ea8bc8 100644 --- a/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/ModelTest.kt +++ b/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/ModelTest.kt @@ -314,34 +314,6 @@ class ModelTest { assertEquals(setOf(expected), actual) } - @Test - fun int() { - val actual = - testAPI - .copy( - components = - Components(schemas = mapOf("Primitive.Int" to value(Schema(type = Type.Basic.Integer)))) - ) - .models() - val expected = Primitive.Int(default = null, description = null) - assertEquals(setOf(expected), actual) - } - - @Test - fun string() { - val actual = - testAPI - .copy( - components = - Components( - schemas = mapOf("Primitive.String" to value(Schema(type = Type.Basic.String))) - ) - ) - .models() - val expected = Primitive.String(default = null, description = null) - assertEquals(setOf(expected), actual) - } - @Test fun binary() { val actual = @@ -360,35 +332,8 @@ class ModelTest { assertEquals(setOf(expected), actual) } - @Test - fun boolean() { - val actual = - testAPI - .copy( - components = - Components( - schemas = mapOf("Primitive.Boolean" to value(Schema(type = Type.Basic.Boolean))) - ) - ) - .models() - val expected = Primitive.Boolean(default = null, description = null) - assertEquals(setOf(expected), actual) - } - @Test - fun double() { - val actual = - testAPI - .copy( - components = - Components( - schemas = mapOf("Primitive.Number" to value(Schema(type = Type.Basic.Number))) - ) - ) - .models() - val expected = Primitive.Double(default = null, description = null) - assertEquals(setOf(expected), actual) - } + @Test fun defaultArrayIsList() { diff --git a/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/PrimitiveTest.kt b/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/PrimitiveTest.kt new file mode 100644 index 0000000..47c7a55 --- /dev/null +++ b/typed/src/commonTest/kotlin/io/github/nomisrev/openapi/PrimitiveTest.kt @@ -0,0 +1,179 @@ +package io.github.nomisrev.openapi + +import io.github.nomisrev.openapi.Model.Primitive +import io.github.nomisrev.openapi.ReferenceOr.Companion.value +import io.github.nomisrev.openapi.Schema.Type +import org.junit.jupiter.api.assertThrows +import kotlin.test.Test +import kotlin.test.assertEquals + +class PrimitiveTest { + @Test + fun double() { + primitive( + Schema( + type = Type.Basic.Number, + default = ExampleValue("1"), + description = "My Desc" + ), + Primitive.Double(1.0, description = "My Desc") + ) + } + + @Test + fun doubleIncorrectDefault() { + primitive( + Schema( + type = Type.Basic.Number, + default = ExampleValue("Nonsense Value"), + description = "My Desc" + ), + Primitive.Double(null, description = "My Desc") + ) + } + + @Test + fun doubleIncorrectDefaultMultiple() { + primitive( + Schema( + type = Type.Basic.Number, + default = ExampleValue.Multiple(listOf("Nonsense", "Value")), + description = "My Desc" + ), + Primitive.Double(null, description = "My Desc") + ) + } + + @Test + fun boolean() { + primitive( + Schema( + type = Type.Basic.Boolean, + default = ExampleValue("true"), + description = "My Desc" + ), + Primitive.Boolean(true, description = "My Desc") + ) + } + + @Test + fun booleanIncorrectDefault() { + primitive( + Schema( + type = Type.Basic.Boolean, + default = ExampleValue("Nonsense Value"), + description = "My Desc" + ), + Primitive.Boolean(null, description = "My Desc") + ) + } + + @Test + fun booleanIncorrectDefaultMultiple() { + primitive( + Schema( + type = Type.Basic.Boolean, + default = ExampleValue.Multiple(listOf("Nonsense", "Value")), + description = "My Desc" + ), + Primitive.Boolean(null, description = "My Desc") + ) + } + + @Test + fun integer() { + primitive( + Schema( + type = Type.Basic.Integer, + default = ExampleValue("2"), + description = "My Desc" + ), + Primitive.Int(2, description = "My Desc") + ) + } + + @Test + fun integerIncorrectDefault() { + primitive( + Schema( + type = Type.Basic.Integer, + default = ExampleValue("Nonsense Value"), + description = "My Desc" + ), + Primitive.Int(null, description = "My Desc") + ) + } + + @Test + fun integerIncorrectDefaultMultiple() { + primitive( + Schema( + type = Type.Basic.Integer, + default = ExampleValue.Multiple(listOf("Nonsense", "Value")), + description = "My Desc" + ), + Primitive.Int(null, description = "My Desc") + ) + } + + @Test + fun string() { + primitive( + Schema( + type = Type.Basic.String, + default = ExampleValue("Some Text"), + description = "My Desc" + ), + Primitive.String("Some Text", description = "My Desc") + ) + } + + @Test + fun stringIntegerDefaultRemainsString() { + primitive( + Schema( + type = Type.Basic.String, + default = ExampleValue("999"), + description = "My Desc" + ), + Primitive.String("999", description = "My Desc") + ) + } + + @Test + fun stringIncorrectDefaultMultiple() { + primitive( + Schema( + type = Type.Basic.String, + default = ExampleValue.Multiple(listOf("Nonsense", "Value")), + description = "My Desc" + ), + Primitive.String("Nonsense, Value", description = "My Desc") + ) + } + + @Test + fun nullNotSupported() { + assertThrows { + primitive( + Schema( + type = Type.Basic.Null, + default = ExampleValue.Multiple(listOf("Nonsense", "Value")), + description = "My Desc" + ), + Primitive.String("Nonsense, Value", description = "My Desc") + ) + } + } + + private fun primitive( + schema: Schema, + model: Model + ) { + val actual = + testAPI + .copy(components = Components(schemas = mapOf("Primitive" to value(schema)))) + .models() + assertEquals(setOf(model), actual) + } +} \ No newline at end of file