SMILEY4 / ktor-swagger-ui

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

Cannot specify body with collection of custom schema items #27

Closed lns-ross closed 1 year ago

lns-ross commented 1 year ago

Hi! I may not have found the secret sauce for this if it exists ...

I was unable to specify a custom schema item by id in the schemas section of the plugin config and then use that for a response body that was a collection of that item, i.e. the equivalent of body<List<ABC>>() or something similar.

I was able to do it by defining a second schema that wrapped the first as an array item but that ended up replicating the base schema into the components section of the api.json with a simple array specifier.

Am I missing something? Or is this a limitation of the DSL for now?

TIA.

SMILEY4 commented 1 year ago

Hi,

this is a limitation of the dsl and currently not possible. I'm looking into a possible solution. My current idea would be to allow also specifying whether the body of the given schema is a single object or an array. The options would then look a bit like this:

I would appreciate your opinion, feedback or other suggestions :)

lns-ross commented 1 year ago

Thx for the quick response!

That definitely looks like it would get the job done. The only other way I can think of doing it would be to set an isArray indicator in the request/response "builder" to indicate it directly. Your suggestion would be a lot more concise.

Since we had no way to intercept the body generator to do the wrapping the work-around we used was to register both "myCustomSchema" and "[myCustomSchema]" (which was an ArraySchema().items = customSchemaSpec ) and then reference the appropriate one. Clunky but it achieved a usable result.

With your suggested change the body generator will now be capable of doing the wrapping which will remove a lot of redundant schema data (for us) and reduce the work we need do in the registration section. ... And it will look a lot nicer too.

Nice. :grin:

lns-ross commented 1 year ago

Works like a charm! :partying_face:

Thx.