Skip to content

Commit

Permalink
fix: enum type def (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
brizzbuzz committed Jan 17, 2024
1 parent 3d1fc56 commit 353637b
Show file tree
Hide file tree
Showing 25 changed files with 121 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
1 change: 0 additions & 1 deletion core/src/test/resources/T0008__complex_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
Expand All @@ -111,7 +110,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]
Expand Down
2 changes: 0 additions & 2 deletions core/src/test/resources/T0019__generic_exception.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Bibbity"
]
Expand All @@ -103,7 +102,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Gibbity"
]
Expand Down
2 changes: 0 additions & 2 deletions core/src/test/resources/T0027__polymorphic_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
Expand All @@ -90,7 +89,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]
Expand Down
2 changes: 0 additions & 2 deletions core/src/test/resources/T0028__polymorphic_list_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
Expand All @@ -90,7 +89,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]
Expand Down
2 changes: 0 additions & 2 deletions core/src/test/resources/T0029__polymorphic_map_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
Expand All @@ -90,7 +89,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"format": "double"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Bibbity"
]
Expand All @@ -84,7 +83,6 @@
"format": "double"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Gibbity"
]
Expand Down
4 changes: 0 additions & 4 deletions core/src/test/resources/T0033__crazy_polymorphic_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
]
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Bibbity"
]
Expand All @@ -96,7 +95,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.ComplexGibbit"
]
Expand All @@ -119,7 +117,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.SimpleGibbit"
]
Expand All @@ -144,7 +141,6 @@
]
},
"type": {
"type": "string",
"enum": [
"io.bkbn.kompendium.core.fixtures.Gibbity"
]
Expand Down
1 change: 0 additions & 1 deletion core/src/test/resources/T0037__nullable_enum_field.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
},
"TestEnum":
{
"type": "string",
"enum": [
"YES",
"NO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"
Expand Down
1 change: 0 additions & 1 deletion core/src/test/resources/T0042__simple_recursive.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"components": {
"schemas": {
"ColumnMode": {
"type": "string",
"enum": [
"NULLABLE",
"REQUIRED",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@
]
},
"SimpleEnum": {
"type": "string",
"enum": [
"ONE",
"TWO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"type": "string"
},
"type": {
"type": "string",
"enum": [
"chillax"
]
Expand All @@ -79,7 +78,6 @@
"format": "int32"
},
"type": {
"type": "string",
"enum": [
"maximize"
]
Expand Down
93 changes: 93 additions & 0 deletions core/src/test/resources/T0076__list_of_enums.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
},
"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": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 1 addition & 2 deletions json-schema/src/test/resources/T0007__simple_enum.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"enum": [ "ONE", "TWO" ],
"type": "string"
"enum": [ "ONE", "TWO" ]
}
3 changes: 1 addition & 2 deletions json-schema/src/test/resources/T0008__nullable_enum.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"enum": [ "ONE", "TWO" ],
"type": "string"
"enum": [ "ONE", "TWO" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Loading

0 comments on commit 353637b

Please sign in to comment.