SMILEY4 / schema-kenerator

Analyze kotlin types, extract information and generate schemas
Apache License 2.0
4 stars 0 forks source link

Two JsonObject in data class break with kotlinx-serialization schema generator #2

Closed waltkb closed 4 days ago

waltkb commented 1 month ago

When using this setup:

type.processKotlinxSerialization()
    .connectSubTypes()
    .handleNameAnnotation()
    .generateSwaggerSchema()
    .handleCoreAnnotations()
    .withAutoTitle(TitleType.SIMPLE)
    .compileReferencingRoot()

It is able to compile a swagger schema for:

@Serializable
data class A(
    val a: JsonObject,
    // val b: JsonObject,
)

But will break when a second JsonObject is contained in the data class:

@Serializable
data class A(
    val a: JsonObject,
    val b: JsonObject,
)
java.lang.NullPointerException
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep$referenceDefinitionsReferences$json$1.invoke(SwaggerSchemaCompileStep.kt:84)
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep$referenceDefinitionsReferences$json$1.invoke(SwaggerSchemaCompileStep.kt:82)
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep.replaceReferences(SwaggerSchemaCompileStep.kt:158)
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep.replaceReferences(SwaggerSchemaCompileStep.kt:162)
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep.referenceDefinitionsReferences(SwaggerSchemaCompileStep.kt:82)
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep.compileReferencing(SwaggerSchemaCompileStep.kt:47)
    at io.github.smiley4.schemakenerator.swagger.steps.SwaggerSchemaCompileStep.compileReferencingRoot(SwaggerSchemaCompileStep.kt:60)
    at io.github.smiley4.schemakenerator.swagger.StepsKt.compileReferencingRoot(steps.kt:89)
    at io.github.smiley4.schemakenerator.swagger.StepsKt.compileReferencingRoot$default(steps.kt:88)

Though it does work when using the reflection schema generator instead of the kotlinx.serialization schema generator, like this:

type
    .collectSubTypes()
    .processReflection()
    .connectSubTypes()
    .handleNameAnnotation()
    .generateSwaggerSchema()
    .handleCoreAnnotations()
    .withAutoTitle(TitleType.SIMPLE)
    .compileReferencingRoot()
SMILEY4 commented 3 weeks ago

Hi, thanks for reporting this bug. The issue should now be fixed with version 1.0.1 (and for ktor-swagger-ui soon aswell). I you encounter any other issues just let me know :)