breaking change: V3 alpha (#256)
This commit is contained in:
@ -0,0 +1,98 @@
|
||||
package io.bkbn.kompendium.json.schema
|
||||
|
||||
import io.bkbn.kompendium.core.fixtures.ComplexRequest
|
||||
import io.bkbn.kompendium.core.fixtures.FlibbityGibbit
|
||||
import io.bkbn.kompendium.core.fixtures.SimpleEnum
|
||||
import io.bkbn.kompendium.core.fixtures.SlammaJamma
|
||||
import io.bkbn.kompendium.core.fixtures.TestHelpers.getFileSnapshot
|
||||
import io.bkbn.kompendium.core.fixtures.TestResponse
|
||||
import io.bkbn.kompendium.core.fixtures.TestSimpleRequest
|
||||
import io.bkbn.kompendium.json.schema.definition.JsonSchema
|
||||
import io.kotest.assertions.json.shouldEqualJson
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.DescribeSpec
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
class SchemaGeneratorTest : DescribeSpec({
|
||||
describe("Scalars") {
|
||||
it("Can generate the schema for an Int") {
|
||||
jsonSchemaTest<Int>("T0001__scalar_int.json")
|
||||
}
|
||||
it("Can generate the schema for a Boolean") {
|
||||
jsonSchemaTest<Boolean>("T0002__scalar_bool.json")
|
||||
}
|
||||
it("Can generate the schema for a String") {
|
||||
jsonSchemaTest<String>("T0003__scalar_string.json")
|
||||
}
|
||||
}
|
||||
describe("Objects") {
|
||||
it("Can generate the schema for a simple object") {
|
||||
jsonSchemaTest<TestSimpleRequest>("T0004__simple_object.json")
|
||||
}
|
||||
it("Can generate the schema for a complex object") {
|
||||
jsonSchemaTest<ComplexRequest>("T0005__complex_object.json")
|
||||
}
|
||||
it("Can generate the schema for a nullable object") {
|
||||
jsonSchemaTest<TestSimpleRequest?>("T0006__nullable_object.json")
|
||||
}
|
||||
it("Can generate the schema for a polymorphic object") {
|
||||
jsonSchemaTest<FlibbityGibbit>("T0015__polymorphic_object.json")
|
||||
}
|
||||
xit("Can generate the schema for a recursive type") {
|
||||
// TODO jsonSchemaTest<SlammaJamma>("T0016__recursive_object.json")
|
||||
}
|
||||
}
|
||||
describe("Enums") {
|
||||
it("Can generate the schema for a simple enum") {
|
||||
jsonSchemaTest<SimpleEnum>("T0007__simple_enum.json")
|
||||
}
|
||||
it("Can generate the schema for a nullable enum") {
|
||||
jsonSchemaTest<SimpleEnum?>("T0008__nullable_enum.json")
|
||||
}
|
||||
}
|
||||
describe("Arrays") {
|
||||
it("Can generate the schema for an array of scalars") {
|
||||
jsonSchemaTest<List<Int>>("T0009__scalar_array.json")
|
||||
}
|
||||
it("Can generate the schema for an array of objects") {
|
||||
jsonSchemaTest<List<TestResponse>>("T0010__object_array.json")
|
||||
}
|
||||
it("Can generate the schema for a nullable array") {
|
||||
jsonSchemaTest<List<Int>?>("T0011__nullable_array.json")
|
||||
}
|
||||
}
|
||||
describe("Maps") {
|
||||
it("Can generate the schema for a map of scalars") {
|
||||
jsonSchemaTest<Map<String, Int>>("T0012__scalar_map.json")
|
||||
}
|
||||
it("Throws an error when map keys are not strings") {
|
||||
shouldThrow<IllegalArgumentException> { SchemaGenerator.fromTypeToSchema<Map<Int, Int>>() }
|
||||
}
|
||||
it("Can generate the schema for a map of objects") {
|
||||
jsonSchemaTest<Map<String, TestResponse>>("T0013__object_map.json")
|
||||
}
|
||||
it("Can generate the schema for a nullable map") {
|
||||
jsonSchemaTest<Map<String, Int>?>("T0014__nullable_map.json")
|
||||
}
|
||||
}
|
||||
}) {
|
||||
companion object {
|
||||
private val json = Json {
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
prettyPrint = true
|
||||
}
|
||||
|
||||
private fun JsonSchema.serialize() = json.encodeToString(JsonSchema.serializer(), this)
|
||||
|
||||
private inline fun <reified T> jsonSchemaTest(snapshotName: String) {
|
||||
// act
|
||||
val schema = SchemaGenerator.fromTypeToSchema<T>()
|
||||
|
||||
// todo add cache assertions!!!
|
||||
|
||||
// assert
|
||||
schema.serialize() shouldEqualJson getFileSnapshot(snapshotName)
|
||||
}
|
||||
}
|
||||
}
|
4
json-schema/src/test/resources/T0001__scalar_int.json
Normal file
4
json-schema/src/test/resources/T0001__scalar_int.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
}
|
3
json-schema/src/test/resources/T0002__scalar_bool.json
Normal file
3
json-schema/src/test/resources/T0002__scalar_bool.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "boolean"
|
||||
}
|
3
json-schema/src/test/resources/T0003__scalar_string.json
Normal file
3
json-schema/src/test/resources/T0003__scalar_string.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "string"
|
||||
}
|
16
json-schema/src/test/resources/T0004__simple_object.json
Normal file
16
json-schema/src/test/resources/T0004__simple_object.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "string"
|
||||
},
|
||||
"b": {
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
}
|
22
json-schema/src/test/resources/T0005__complex_object.json
Normal file
22
json-schema/src/test/resources/T0005__complex_object.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"amazingField": {
|
||||
"type": "string"
|
||||
},
|
||||
"org": {
|
||||
"type": "string"
|
||||
},
|
||||
"tables": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/NestedComplexItem"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"amazingField",
|
||||
"org",
|
||||
"tables"
|
||||
]
|
||||
}
|
23
json-schema/src/test/resources/T0006__nullable_object.json
Normal file
23
json-schema/src/test/resources/T0006__nullable_object.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "null"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "string"
|
||||
},
|
||||
"b": {
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
3
json-schema/src/test/resources/T0007__simple_enum.json
Normal file
3
json-schema/src/test/resources/T0007__simple_enum.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"enum": [ "ONE", "TWO" ]
|
||||
}
|
13
json-schema/src/test/resources/T0008__nullable_enum.json
Normal file
13
json-schema/src/test/resources/T0008__nullable_enum.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "null"
|
||||
},
|
||||
{
|
||||
"enum": [
|
||||
"ONE",
|
||||
"TWO"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
7
json-schema/src/test/resources/T0009__scalar_array.json
Normal file
7
json-schema/src/test/resources/T0009__scalar_array.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"items": {
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
6
json-schema/src/test/resources/T0010__object_array.json
Normal file
6
json-schema/src/test/resources/T0010__object_array.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/TestResponse"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
14
json-schema/src/test/resources/T0011__nullable_array.json
Normal file
14
json-schema/src/test/resources/T0011__nullable_array.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "null"
|
||||
},
|
||||
{
|
||||
"items": {
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
},
|
||||
"type": "array"
|
||||
}
|
||||
]
|
||||
}
|
7
json-schema/src/test/resources/T0012__scalar_map.json
Normal file
7
json-schema/src/test/resources/T0012__scalar_map.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"additionalProperties": {
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
},
|
||||
"type": "object"
|
||||
}
|
6
json-schema/src/test/resources/T0013__object_map.json
Normal file
6
json-schema/src/test/resources/T0013__object_map.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"additionalProperties": {
|
||||
"$ref": "#/components/schemas/TestResponse"
|
||||
},
|
||||
"type": "object"
|
||||
}
|
14
json-schema/src/test/resources/T0014__nullable_map.json
Normal file
14
json-schema/src/test/resources/T0014__nullable_map.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "null"
|
||||
},
|
||||
{
|
||||
"additionalProperties": {
|
||||
"type": "number",
|
||||
"format": "int32"
|
||||
},
|
||||
"type": "object"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
{
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/ComplexGibbit"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/SimpleGibbit"
|
||||
}
|
||||
]
|
||||
}
|
13
json-schema/src/test/resources/T0016__recursive_object.json
Normal file
13
json-schema/src/test/resources/T0016__recursive_object.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/OneJamma"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/AnothaJamma"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/InsaneJamma"
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user