fix: improved error output when an unknown schema is encountered (#327)

This commit is contained in:
Ryan Brink
2022-09-26 08:15:21 -05:00
committed by GitHub
parent b3dcf7dc8b
commit 70345494ab
6 changed files with 36 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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<UnknownSchemaException> { openApiTestAllSerializers("") { dateTimeString() } }
exception.message should startWith("An unknown type was encountered: class java.time.Instant")
}
}
describe("Constraints") {
// TODO Assess strategies here
}

View File

@ -1,5 +1,5 @@
# Kompendium
project.version=3.3.0
project.version=3.3.1
# Kotlin
kotlin.code.style=official
# Gradle

View File

@ -0,0 +1,3 @@
package io.bkbn.kompendium.json.schema.exception
class UnknownSchemaException(message: String) : Exception(message)

View File

@ -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()