From 70345494ab9d1a60df0cc436dc69b4acf575a4e4 Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+unredundant@users.noreply.github.com> Date: Mon, 26 Sep 2022 08:15:21 -0500 Subject: [PATCH] fix: improved error output when an unknown schema is encountered (#327) --- .github/workflows/release.yml | 2 +- CHANGELOG.md | 6 ++++++ .../io/bkbn/kompendium/core/KompendiumTest.kt | 10 ++++++++++ gradle.properties | 2 +- .../schema/exception/UnknownSchemaException.kt | 3 +++ .../json/schema/handler/SimpleObjectHandler.kt | 16 +++++++++++++++- 6 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/exception/UnknownSchemaException.kt diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d830cadf7..378638ca5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,7 +16,7 @@ jobs: with: distribution: 'adopt' java-version: '17' - - name: Publlish to Maven Central + - name: Publish to Maven Central uses: burrunan/gradle-cache-action@v1 with: gradle-version: wrapper diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c24a6b0c..623e7d8d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,12 @@ ## Released +## [3.3.1] - September 26th, 2022 + +### Added + +- Better exception thrown when unidentified type is encountered + ## [3.3.0] - September 15th, 2022 ### Added 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 a7f2bd365..b0597c906 100644 --- a/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt +++ b/core/src/test/kotlin/io/bkbn/kompendium/core/KompendiumTest.kt @@ -53,12 +53,16 @@ import io.bkbn.kompendium.core.util.TestModules.trailingSlash import io.bkbn.kompendium.core.util.TestModules.unbackedFieldsResponse import io.bkbn.kompendium.core.util.TestModules.withOperationId import io.bkbn.kompendium.json.schema.definition.TypeDefinition +import io.bkbn.kompendium.json.schema.exception.UnknownSchemaException import io.bkbn.kompendium.oas.component.Components import io.bkbn.kompendium.oas.security.ApiKeyAuth import io.bkbn.kompendium.oas.security.BasicAuth import io.bkbn.kompendium.oas.security.BearerAuth import io.bkbn.kompendium.oas.security.OAuth +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.DescribeSpec +import io.kotest.matchers.should +import io.kotest.matchers.string.startWith import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import io.ktor.http.HttpMethod @@ -240,6 +244,12 @@ class KompendiumTest : DescribeSpec({ openApiTestAllSerializers("T0044__nested_type_name.json") { nestedTypeName() } } } + describe("Error Handling") { + it("Throws a clear exception when an unidentified type is encountered") { + val exception = shouldThrow { openApiTestAllSerializers("") { dateTimeString() } } + exception.message should startWith("An unknown type was encountered: class java.time.Instant") + } + } describe("Constraints") { // TODO Assess strategies here } diff --git a/gradle.properties b/gradle.properties index 0f0661aef..86a6cd817 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Kompendium -project.version=3.3.0 +project.version=3.3.1 # Kotlin kotlin.code.style=official # Gradle diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/exception/UnknownSchemaException.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/exception/UnknownSchemaException.kt new file mode 100644 index 000000000..57ac2327b --- /dev/null +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/exception/UnknownSchemaException.kt @@ -0,0 +1,3 @@ +package io.bkbn.kompendium.json.schema.exception + +class UnknownSchemaException(message: String) : Exception(message) diff --git a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/SimpleObjectHandler.kt b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/SimpleObjectHandler.kt index bb4cd722c..2f5db94d1 100644 --- a/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/SimpleObjectHandler.kt +++ b/json-schema/src/main/kotlin/io/bkbn/kompendium/json/schema/handler/SimpleObjectHandler.kt @@ -7,6 +7,7 @@ import io.bkbn.kompendium.json.schema.definition.NullableDefinition import io.bkbn.kompendium.json.schema.definition.OneOfDefinition import io.bkbn.kompendium.json.schema.definition.ReferenceDefinition import io.bkbn.kompendium.json.schema.definition.TypeDefinition +import io.bkbn.kompendium.json.schema.exception.UnknownSchemaException import io.bkbn.kompendium.json.schema.util.Helpers.getReferenceSlug import io.bkbn.kompendium.json.schema.util.Helpers.getSimpleSlug import kotlin.reflect.KClass @@ -53,7 +54,20 @@ object SimpleObjectHandler { .asSequence() .filterNot { it.javaField == null } .filterNot { prop -> prop.returnType.isMarkedNullable } - .filterNot { prop -> clazz.primaryConstructor!!.parameters.find { it.name == prop.name }!!.isOptional } + .filterNot { prop -> + clazz.primaryConstructor + ?.parameters + ?.find { it.name == prop.name } + ?.isOptional + ?: throw UnknownSchemaException( + """ + |An unknown type was encountered: $clazz. This typically indicates that a complex scalar such as dates, + |timestamps, or custom number representations such as BigInteger were not added as custom types when + |configuring the NotarizedApplication plugin. If you are still seeing this error despite adding all + |required custom types, this indicates a bug in Kompendium, please open an issue on GitHub. + """.trimMargin() + ) + } .map { schemaConfigurator.serializableName(it) } .toSet()