fix: enum type def (#563)

This commit is contained in:
Ryan Brink
2024-01-17 16:14:47 -05:00
committed by GitHub
parent 3d1fc566d4
commit 353637bb94
25 changed files with 121 additions and 34 deletions

View File

@ -60,6 +60,7 @@ import io.bkbn.kompendium.core.util.primitives
import io.bkbn.kompendium.core.util.reqRespExamples
import io.bkbn.kompendium.core.util.requiredParams
import io.bkbn.kompendium.core.util.responseHeaders
import io.bkbn.kompendium.core.util.returnsEnumList
import io.bkbn.kompendium.core.util.returnsList
import io.bkbn.kompendium.core.util.rootRoute
import io.bkbn.kompendium.core.util.samePathDifferentMethodsAndAuth
@ -295,6 +296,9 @@ class KompendiumTest : DescribeSpec({
it("Can have a nullable enum as a member field") {
openApiTestAllSerializers("T0037__nullable_enum_field.json") { nullableEnumField() }
}
it("Can have a list of enums as a field") {
openApiTestAllSerializers("T0076__list_of_enums.json") { returnsEnumList() }
}
it("Can have a nullable reference without impacting base type") {
openApiTestAllSerializers("T0041__nullable_reference.json") { nullableReference() }
}

View File

@ -3,6 +3,7 @@ package io.bkbn.kompendium.core.util
import io.bkbn.kompendium.core.fixtures.ComplexRequest
import io.bkbn.kompendium.core.fixtures.SomethingSimilar
import io.bkbn.kompendium.core.fixtures.TestCreatedResponse
import io.bkbn.kompendium.core.fixtures.TestEnum
import io.bkbn.kompendium.core.fixtures.TestRequest
import io.bkbn.kompendium.core.fixtures.TestResponse
import io.bkbn.kompendium.core.fixtures.TestSimpleRequest
@ -284,6 +285,23 @@ fun Routing.returnsList() {
}
}
fun Routing.returnsEnumList() {
route(defaultPath) {
install(NotarizedRoute()) {
parameters = defaultParams
get = GetInfo.builder {
summary(defaultPathSummary)
description(defaultPathDescription)
response {
description("A Successful List-y Endeavor")
responseCode(HttpStatusCode.OK)
responseType<List<TestEnum>>()
}
}
}
}
}
fun Routing.nonRequiredParams() {
route("/optional") {
install(NotarizedRoute()) {

View File

@ -132,7 +132,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"

View File

@ -88,7 +88,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
@ -111,7 +110,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]

View File

@ -84,7 +84,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Bibbity"
]
@ -103,7 +102,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Gibbity"
]

View File

@ -67,7 +67,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
@ -90,7 +89,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]

View File

@ -67,7 +67,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
@ -90,7 +89,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]

View File

@ -67,7 +67,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
@ -90,7 +89,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]

View File

@ -64,7 +64,6 @@
"format": "double"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Bibbity"
]
@ -84,7 +83,6 @@
"format": "double"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Gibbity"
]

View File

@ -70,7 +70,6 @@
]
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Bibbity"
]
@ -96,7 +95,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
@ -119,7 +117,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]
@ -144,7 +141,6 @@
]
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Gibbity"
]

View File

@ -71,7 +71,6 @@
},
"TestEnum":
{
"type": "string",
"enum": [
"YES",
"NO"

View File

@ -119,7 +119,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"

View File

@ -54,7 +54,6 @@
"components": {
"schemas": {
"ColumnMode": {
"type": "string",
"enum": [
"NULLABLE",
"REQUIRED",

View File

@ -154,7 +154,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"

View File

@ -169,7 +169,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"

View File

@ -60,7 +60,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"chillax"
]
@ -79,7 +78,6 @@
"format": "int32"
},
"type": {
"type": "string",
"enum": [
"maximize"
]

View File

@ -0,0 +1,93 @@
{
"openapi": "3.1.0",
"jsonSchemaDialect": "https://json-schema.org/draft/2020-12/schema",
"info": {
"title": "Test API",
"version": "1.33.7",
"description": "An amazing, fully-ish 😉 generated API spec",
"termsOfService": "https://example.com",
"contact": {
"name": "Homer Simpson",
"url": "https://gph.is/1NPUDiM",
"email": "chunkylover53@aol.com"
},
"license": {
"name": "MIT",
"url": "https://github.com/bkbnio/kompendium/blob/main/LICENSE"
}
},
"servers": [
{
"url": "https://myawesomeapi.com",
"description": "Production instance of my API"
},
{
"url": "https://staging.myawesomeapi.com",
"description": "Where the fun stuff happens"
}
],
"paths": {
"/test/{a}": {
"get": {
"tags": [],
"summary": "Great Summary!",
"description": "testing more",
"parameters": [],
"responses": {
"200": {
"description": "A Successful List-y Endeavor",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/List-TestEnum"
}
}
}
}
},
"deprecated": false
},
"parameters": [
{
"name": "a",
"in": "path",
"schema": {
"type": "string"
},
"required": true,
"deprecated": false
},
{
"name": "aa",
"in": "query",
"schema": {
"type": "number",
"format": "int32"
},
"required": true,
"deprecated": false
}
]
}
},
"webhooks": {},
"components": {
"schemas": {
"TestEnum": {
"enum": [
"YES",
"NO"
]
},
"List-TestEnum": {
"items": {
"$ref": "#/components/schemas/TestEnum"
},
"type": "array"
}
},
"securitySchemes": {}
},
"security": [],
"tags": []
}

View File

@ -35,7 +35,7 @@ class KotlinXSchemaConfigurator : SchemaConfigurator {
required = implementationSchema.required?.plus("type"),
properties = implementationSchema.properties?.plus(
mapOf(
"type" to EnumDefinition("string", enum = setOf(determineTypeQualifier(implementationType)))
"type" to EnumDefinition(enum = setOf(determineTypeQualifier(implementationType)))
)
)
)

View File

@ -4,7 +4,6 @@ import kotlinx.serialization.Serializable
@Serializable
data class EnumDefinition(
val type: String,
val enum: Set<String>,
override val deprecated: Boolean? = null,
override val description: String? = null,

View File

@ -4,6 +4,7 @@ import io.bkbn.kompendium.enrichment.TypeEnrichment
import io.bkbn.kompendium.json.schema.SchemaConfigurator
import io.bkbn.kompendium.json.schema.SchemaGenerator
import io.bkbn.kompendium.json.schema.definition.ArrayDefinition
import io.bkbn.kompendium.json.schema.definition.EnumDefinition
import io.bkbn.kompendium.json.schema.definition.JsonSchema
import io.bkbn.kompendium.json.schema.definition.NullableDefinition
import io.bkbn.kompendium.json.schema.definition.OneOfDefinition
@ -23,7 +24,7 @@ object CollectionHandler {
val collectionType = type.arguments.first().type
?: error("This indicates a bug in Kompendium, please open a GitHub issue!")
val typeSchema = SchemaGenerator.fromTypeToSchema(collectionType, cache, schemaConfigurator, enrichment).let {
if (it is TypeDefinition && it.type == "object") {
if ((it is TypeDefinition && it.type == "object") || it is EnumDefinition) {
cache[collectionType.getSlug(enrichment)] = it
ReferenceDefinition(collectionType.getReferenceSlug(enrichment))
} else {

View File

@ -19,6 +19,6 @@ object EnumHandler {
cache[type.getSlug(enrichment)] = ReferenceDefinition(type.getReferenceSlug(enrichment))
val options = clazz.java.enumConstants.map { it.toString() }.toSet()
return EnumDefinition(type = "string", enum = options)
return EnumDefinition(enum = options)
}
}

View File

@ -1,4 +1,3 @@
{
"enum": [ "ONE", "TWO" ],
"type": "string"
"enum": [ "ONE", "TWO" ]
}

View File

@ -1,4 +1,3 @@
{
"enum": [ "ONE", "TWO" ],
"type": "string"
"enum": [ "ONE", "TWO" ]
}

View File

@ -148,7 +148,6 @@ fun fromTypeToSchema(
Descriptors.FieldDescriptor.JavaType.BYTE_STRING -> TypeDefinition.STRING
Descriptors.FieldDescriptor.JavaType.ENUM -> {
cache[javaProtoField.enumType.fullName] = EnumDefinition(
type = "string",
enum = javaProtoField.enumType.values.map { it.name }.toSet()
)
ReferenceDefinition("${Helpers.COMPONENT_SLUG}/${javaProtoField.enumType.name}")

View File

@ -66,7 +66,6 @@
}
},
"Corpus": {
"type": "string",
"enum": [
"CORPUS_UNSPECIFIED",
"CORPUS_UNIVERSAL",