Home > Software engineering >  Build swagger-ui from open-api.yml
Build swagger-ui from open-api.yml

Time:12-29

I'm trying to generate interfaces from Open-API specification and I have a Gradle plugin:

implementation(
    "org.springdoc:springdoc-openapi-ui:1.5.12",
    "org.openapitools:openapi-generator-gradle-plugin:5.3.0"
)

def apiFile = "$rootDir/src/main/resources/openapi/api.yml"

task buildOpenApi(type: GenerateTask) {
    generatorName = "spring"
    inputSpec = apiFile
    outputDir = "$buildDir/generated"
    groupId = "$project.group"
    id = "$project.name-java-client"
    version = "$project.version"
    apiPackage = "com.example.my.api"
    modelPackage = "com.example.my.model"
    configOptions = [
            interfaceOnly: "true",
            openApiNullable: "false",
            skipDefaultInterface: "true"
    ]
    globalProperties = [
            apis: "",
            models: ""
    ]
    enablePostProcessFile = true
    skipOverwrite = false
}

compileJava.dependsOn(buildOpenApi)
sourceSets.main.java.srcDirs = ['build/generated/src','src/main/java']

And have an api.yml file that is located in src/main/resources/openapi/api.yml. When I run ./gradlew clean compileJava, it generates me correct interfaces to the build directory, so I can implement them. But when I launch my application and go to http://localhost:8080/swagger-ui.html I see swagger automatically generated from my controller classes, but not from api.yml file - I mean, it doesn't have any descriptions, examples and so on

How to make swagger built from my api.yml file, not from controller source code?

CodePudding user response:

The correct way is not to expect swagger from implemented interfaces, but build it from api.yml file. This config in application.yml helped me:

springdoc:
  api-docs:
    enabled: false
    path: /api-docs
  swagger-ui:
    disable-swagger-default-url: true
    url: /api.yml
    path: /api-docs

Now swagger available at localhost:8080/api-docs

  • Related