SMILEY4 / ktor-swagger-ui

Kotlin Ktor plugin to generate OpenAPI and provide Swagger UI
Apache License 2.0
182 stars 33 forks source link

"no root-schema for given type-descriptor" when setting body in defaultUnauthorizedResponse #104

Closed rChaoz closed 5 months ago

rChaoz commented 5 months ago

The following works:

get("route", {
    response {
        body<Type> {}
    }
}) {}

But this doesn't work (in setup):

security {
    defaultUnauthorizedResponse {
        body<Type> {}
    }
}

Logs

java.util.NoSuchElementException: no root-schema for given type-descriptor
    at io.github.smiley4.ktorswaggerui.builder.schema.SchemaContextImpl.getSchema(SchemaContextImpl.kt:167)
    at io.github.smiley4.ktorswaggerui.builder.openapi.ContentBuilder.buildSimpleBody(ContentBuilder.kt:34)
    at io.github.smiley4.ktorswaggerui.builder.openapi.ContentBuilder.build(ContentBuilder.kt:28)
    at io.github.smiley4.ktorswaggerui.builder.openapi.ResponseBuilder.build(ResponseBuilder.kt:20)
        ...

It seems like the type's schema is not registered automatically by defaultUnauthorizedResponse.

Workaround

Manually define the unauthorized response on each route. Helper function can be used:

fun OpenApiResponses.unauthorizedResponse() {
    HttpStatusCode.Unauthorized to {
        // define response
    }
}

Then used like so:

get("route", {
    response {
        // ... other responses ...
        unauthorizedResponse()
    }
}) {
    // handle route
}