From 353637bb94762a68eec7c90f24b54eca61b350de Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+unredundant@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:14:47 -0500 Subject: [PATCH] fix: enum type def (#563) --- .../io/bkbn/kompendium/core/KompendiumTest.kt | 4 + .../kompendium/core/util/NotarizedOpenApi.kt | 18 ++++ .../test/resources/T0008__complex_type.json | 1 - ...T0018__polymorphic_error_status_codes.json | 2 - .../resources/T0019__generic_exception.json | 2 - .../T0027__polymorphic_response.json | 2 - .../T0028__polymorphic_list_response.json | 2 - .../T0029__polymorphic_map_response.json | 2 - ...2__polymorphic_response_with_generics.json | 2 - .../T0033__crazy_polymorphic_example.json | 4 - .../resources/T0037__nullable_enum_field.json | 1 - ...__nested_generic_multiple_type_params.json | 1 - .../resources/T0042__simple_recursive.json | 1 - .../T0056__enriched_nested_collection.json | 1 - .../T0057__enriched_complex_generic_type.json | 1 - ...__sealed_interface_type_name_override.json | 2 - .../test/resources/T0076__list_of_enums.json | 93 +++++++++++++++++++ .../json/schema/KotlinXSchemaConfigurator.kt | 2 +- .../json/schema/definition/EnumDefinition.kt | 1 - .../json/schema/handler/CollectionHandler.kt | 3 +- .../json/schema/handler/EnumHandler.kt | 2 +- .../test/resources/T0007__simple_enum.json | 3 +- .../test/resources/T0008__nullable_enum.json | 3 +- .../converters/FieldDescriptiorConverters.kt | 1 - .../resources/T0002__enummessage_post.json | 1 - 25 files changed, 121 insertions(+), 34 deletions(-) create mode 100644 core/src/test/resources/T0076__list_of_enums.json diff --git a/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt b/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt index 9f8d48cad..299e953b4 100644 --- a/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt +++ b/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt @@ -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() } } diff --git a/core/src/test/kotlin/io/bkbn/kompendium/core/util/NotarizedOpenApi.kt b/core/src/test/kotlin/io/bkbn/kompendium/core/util/NotarizedOpenApi.kt index 9c6947c86..4c2c9073d 100644 --- a/core/src/test/kotlin/io/bkbn/kompendium/core/util/NotarizedOpenApi.kt +++ b/core/src/test/kotlin/io/bkbn/kompendium/core/util/NotarizedOpenApi.kt @@ -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>() + } + } + } + } +} + fun Routing.nonRequiredParams() { route("/optional") { install(NotarizedRoute()) { diff --git a/core/src/test/resources/T0008__complex_type.json b/core/src/test/resources/T0008__complex_type.json index 4b2b62438..2cf0dda40 100644 --- a/core/src/test/resources/T0008__complex_type.json +++ b/core/src/test/resources/T0008__complex_type.json @@ -132,7 +132,6 @@ ] }, "SimpleEnum": { - "type": "string", "enum": [ "ONE", "TWO" diff --git a/core/src/test/resources/T0018__polymorphic_error_status_codes.json b/core/src/test/resources/T0018__polymorphic_error_status_codes.json index 3c83c524a..c934ac824 100644 --- a/core/src/test/resources/T0018__polymorphic_error_status_codes.json +++ b/core/src/test/resources/T0018__polymorphic_error_status_codes.json @@ -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" ] diff --git a/core/src/test/resources/T0019__generic_exception.json b/core/src/test/resources/T0019__generic_exception.json index f59ce0385..afe0d12e9 100644 --- a/core/src/test/resources/T0019__generic_exception.json +++ b/core/src/test/resources/T0019__generic_exception.json @@ -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" ] diff --git a/core/src/test/resources/T0027__polymorphic_response.json b/core/src/test/resources/T0027__polymorphic_response.json index 2caf27336..3ffa5f629 100644 --- a/core/src/test/resources/T0027__polymorphic_response.json +++ b/core/src/test/resources/T0027__polymorphic_response.json @@ -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" ] diff --git a/core/src/test/resources/T0028__polymorphic_list_response.json b/core/src/test/resources/T0028__polymorphic_list_response.json index 2684a2abc..2f055b7e9 100644 --- a/core/src/test/resources/T0028__polymorphic_list_response.json +++ b/core/src/test/resources/T0028__polymorphic_list_response.json @@ -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" ] diff --git a/core/src/test/resources/T0029__polymorphic_map_response.json b/core/src/test/resources/T0029__polymorphic_map_response.json index e36815640..273b7add0 100644 --- a/core/src/test/resources/T0029__polymorphic_map_response.json +++ b/core/src/test/resources/T0029__polymorphic_map_response.json @@ -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" ] diff --git a/core/src/test/resources/T0032__polymorphic_response_with_generics.json b/core/src/test/resources/T0032__polymorphic_response_with_generics.json index 7f898585e..bdcc03e60 100644 --- a/core/src/test/resources/T0032__polymorphic_response_with_generics.json +++ b/core/src/test/resources/T0032__polymorphic_response_with_generics.json @@ -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" ] diff --git a/core/src/test/resources/T0033__crazy_polymorphic_example.json b/core/src/test/resources/T0033__crazy_polymorphic_example.json index 0f3e39a8b..5ec42069e 100644 --- a/core/src/test/resources/T0033__crazy_polymorphic_example.json +++ b/core/src/test/resources/T0033__crazy_polymorphic_example.json @@ -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" ] diff --git a/core/src/test/resources/T0037__nullable_enum_field.json b/core/src/test/resources/T0037__nullable_enum_field.json index 4fc208e5e..c87a68837 100644 --- a/core/src/test/resources/T0037__nullable_enum_field.json +++ b/core/src/test/resources/T0037__nullable_enum_field.json @@ -71,7 +71,6 @@ }, "TestEnum": { - "type": "string", "enum": [ "YES", "NO" diff --git a/core/src/test/resources/T0040__nested_generic_multiple_type_params.json b/core/src/test/resources/T0040__nested_generic_multiple_type_params.json index 946a3da1d..2fc9845a9 100644 --- a/core/src/test/resources/T0040__nested_generic_multiple_type_params.json +++ b/core/src/test/resources/T0040__nested_generic_multiple_type_params.json @@ -119,7 +119,6 @@ ] }, "SimpleEnum": { - "type": "string", "enum": [ "ONE", "TWO" diff --git a/core/src/test/resources/T0042__simple_recursive.json b/core/src/test/resources/T0042__simple_recursive.json index df4557942..98702c9f7 100644 --- a/core/src/test/resources/T0042__simple_recursive.json +++ b/core/src/test/resources/T0042__simple_recursive.json @@ -54,7 +54,6 @@ "components": { "schemas": { "ColumnMode": { - "type": "string", "enum": [ "NULLABLE", "REQUIRED", diff --git a/core/src/test/resources/T0056__enriched_nested_collection.json b/core/src/test/resources/T0056__enriched_nested_collection.json index 1de1b1c91..afe86a8da 100644 --- a/core/src/test/resources/T0056__enriched_nested_collection.json +++ b/core/src/test/resources/T0056__enriched_nested_collection.json @@ -154,7 +154,6 @@ ] }, "SimpleEnum": { - "type": "string", "enum": [ "ONE", "TWO" diff --git a/core/src/test/resources/T0057__enriched_complex_generic_type.json b/core/src/test/resources/T0057__enriched_complex_generic_type.json index 44954b3ef..04369c030 100644 --- a/core/src/test/resources/T0057__enriched_complex_generic_type.json +++ b/core/src/test/resources/T0057__enriched_complex_generic_type.json @@ -169,7 +169,6 @@ ] }, "SimpleEnum": { - "type": "string", "enum": [ "ONE", "TWO" diff --git a/core/src/test/resources/T0070__sealed_interface_type_name_override.json b/core/src/test/resources/T0070__sealed_interface_type_name_override.json index 2883a5a7f..dce527f1c 100644 --- a/core/src/test/resources/T0070__sealed_interface_type_name_override.json +++ b/core/src/test/resources/T0070__sealed_interface_type_name_override.json @@ -60,7 +60,6 @@ "type": "string" }, "type": { - "type": "string", "enum": [ "chillax" ] @@ -79,7 +78,6 @@ "format": "int32" }, "type": { - "type": "string", "enum": [ "maximize" ] diff --git a/core/src/test/resources/T0076__list_of_enums.json b/core/src/test/resources/T0076__list_of_enums.json new file mode 100644 index 000000000..475711606 --- /dev/null +++ b/core/src/test/resources/T0076__list_of_enums.json @@ -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": [] +} diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/KotlinXSchemaConfigurator.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/KotlinXSchemaConfigurator.kt index 57f4a02f2..e03510848 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/KotlinXSchemaConfigurator.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/KotlinXSchemaConfigurator.kt @@ -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))) ) ) ) diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/EnumDefinition.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/EnumDefinition.kt index f00b51b62..331902b37 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/EnumDefinition.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/definition/EnumDefinition.kt @@ -4,7 +4,6 @@ import kotlinx.serialization.Serializable @Serializable data class EnumDefinition( - val type: String, val enum: Set, override val deprecated: Boolean? = null, override val description: String? = null, diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/CollectionHandler.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/CollectionHandler.kt index 0880ca303..3599483d8 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/CollectionHandler.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/CollectionHandler.kt @@ -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 { diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/EnumHandler.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/EnumHandler.kt index 2f5b16cdc..dac1238b5 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/EnumHandler.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/EnumHandler.kt @@ -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) } } diff --git a/json-schema/src/test/resources/T0007__simple_enum.json b/json-schema/src/test/resources/T0007__simple_enum.json index 35a4f6dfc..19f2d782e 100644 --- a/json-schema/src/test/resources/T0007__simple_enum.json +++ b/json-schema/src/test/resources/T0007__simple_enum.json @@ -1,4 +1,3 @@ { - "enum": [ "ONE", "TWO" ], - "type": "string" + "enum": [ "ONE", "TWO" ] } diff --git a/json-schema/src/test/resources/T0008__nullable_enum.json b/json-schema/src/test/resources/T0008__nullable_enum.json index 35a4f6dfc..19f2d782e 100644 --- a/json-schema/src/test/resources/T0008__nullable_enum.json +++ b/json-schema/src/test/resources/T0008__nullable_enum.json @@ -1,4 +1,3 @@ { - "enum": [ "ONE", "TWO" ], - "type": "string" + "enum": [ "ONE", "TWO" ] } diff --git a/protobuf-java-converter/src/main/kotlin/io/bkbn/kompendium/protobufjavaconverter/converters/FieldDescriptiorConverters.kt b/protobuf-java-converter/src/main/kotlin/io/bkbn/kompendium/protobufjavaconverter/converters/FieldDescriptiorConverters.kt index 970081801..a4cc44f5c 100644 --- a/protobuf-java-converter/src/main/kotlin/io/bkbn/kompendium/protobufjavaconverter/converters/FieldDescriptiorConverters.kt +++ b/protobuf-java-converter/src/main/kotlin/io/bkbn/kompendium/protobufjavaconverter/converters/FieldDescriptiorConverters.kt @@ -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}") diff --git a/protobuf-java-converter/src/test/resources/T0002__enummessage_post.json b/protobuf-java-converter/src/test/resources/T0002__enummessage_post.json index fd64b0f5a..142f5e71d 100644 --- a/protobuf-java-converter/src/test/resources/T0002__enummessage_post.json +++ b/protobuf-java-converter/src/test/resources/T0002__enummessage_post.json @@ -66,7 +66,6 @@ } }, "Corpus": { - "type": "string", "enum": [ "CORPUS_UNSPECIFIED", "CORPUS_UNIVERSAL",