SMILEY4 / ktor-swagger-ui

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

Unable to have DEFINITIONS_FOR_ALL_OBJECTS in the open api spec #24

Closed guptachetan1997 closed 1 year ago

guptachetan1997 commented 1 year ago

I have tried multiple config options in the schema builder, but am not able to configure "All Schema in definitions". The spec being generated has the main schema in components, but the internal models are coming inline. This is leading to very long class names, as it is concatenating the main class name with the field name and putting "inner" in the name as well.

I think this should be the correct config for what I want, but if I try this then the schema does not get generated:

install(SwaggerUI) {
   //...
    schemasInComponentSection = true
    examplesInComponentSection = true
    schemaGeneratorConfigBuilder.with(Option.DEFINITIONS_FOR_ALL_OBJECTS)
                                                          .with(Option.DEFINITION_FOR_MAIN_SCHEMA)
                                                          .without(Option.INLINE_ALL_SCHEMAS)

    //...
}

What I want is to generate the spec where for this class:

data class Y(val a: String)
data class X(val y: Y)

The generated schema should be:

"X": {
      "type": "object",
      "properties": {
        "y": {
          "$ref": "#/components/schemas/Y"
        }
      },
      "xml": {
        "name": "X"
      }
    },
    "Y": {
      "type": "object",
      "properties": {
        "a": {
          "type": "string"
        }
      }
    }

What I being generated currently:

"X": {
      "type": "object",
      "properties": {
        "y": {
          "type": "object",
          "properties": {
            "a": {
              "type": "string"
            }
          }
        }
      },
      "xml": {
        "name": "X"
      }
    }
SMILEY4 commented 1 year ago

Hi, thanks for reporting this issue. your config for the "schemaGeneratorConfigBuilder" is correct and would produce the wanted json-schema. However, the swaggerui-plugin currently does not support this scenario. I'm going to look for a possible solution to allow/fix this.

SMILEY4 commented 1 year ago

Should be fixed with version 2.0.0+