SMILEY4 / ktor-swagger-ui

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

Lowe case path parameter options #94

Closed tobyworks closed 3 months ago

tobyworks commented 4 months ago

Imagine i have a call where the path parameters can only be of a certain value for instance:

["dog", "cat", "mouse"]

Any other value is deemed invalid, so i would like to show those as available pathParams The only way i figured out to do it is to have a enum and pass that in as type, however it is not according to kotlin coding standards to create an enum like this:

enum class {
dog,cat,mouse;
}

you would rather have it like this

enum class {
Dog, Cat, Mouse;
}

But then the path param actually gets uppercased which also isn't what i want.

SMILEY4 commented 4 months ago

Hi, you can overwrite the enums toString-method and use that as the values for the path parameters

enum class Animal(private val type: String) {
    Dog("dog"), Cat("cat"), Mouse("mouse");
    overwrite fun toString() = type
}

// add somewhere at startup (i.e. before/after installing swaggerui-plugin)
EncodingData.DEFAULT_SCHEMA_GENERATOR = SchemaGenerator(
    schemaGeneratorConfigBuilder()
        .with(Option.FLATTENED_ENUMS_FROM_TOSTRING) // use toString for enums
        .build()
)
tobyworks commented 2 weeks ago

How does this work with 3.2.0?