Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate openAPI code using multiple yaml file

I have two yaml file, customer.yaml and employee.yaml. How do I generate java code from these two yaml file in single project. I'm using gradle, I know the task specification for single yaml but how do I specify multiple yaml. Should I specify mutiple inputSpec under single openApiGenerator ? if yes then what is the exact syntax to do so. Below is my openApiGenerator task in build.gradle file.

``
openApiGenerate {
    generatorName = "spring"
    apiPackage = "com.xxx.generated.controller"
    modelPackage = "com.xxx.generated.model"
    inputSpec = "$rootDir//schema/employee.yaml".toString()
    outputDir = "$rootDir/generated".toString()
    configOptions = [
        dateLibrary: "java8"
    ]
    systemProperties = [
        invoker : "false", 
        generateSupportingFiles: "true"
    ]
    additionalProperties = [
        interfaceOnly : "true",
    ]
}
 ``

I heard of openApiGenerators task which Lists generators available via Open API Generators but couldn't find a way to use it.

like image 553
Nicolas Avatar asked Sep 26 '19 14:09

Nicolas


People also ask

Does OpenAPI generate code?

As the name suggests, the OpenAPI Generator generates code from an OpenAPI specification. It can create code for client libraries, server stubs, documentation and configuration. It supports various languages and frameworks.

What is OpenAPI generator CLI?

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (both 2.0 and 3.0 are supported).

What is OpenAPI generator Maven plugin?

The OpenAPI Generator can be used as command line tool or as plugin for build tools like Maven and Gradle. The pom. xml file below shows the integration of the OpenAPI Generator plugin in a Maven project. To generate the server code, you need to add a plugin definition similar to the one below.


Video Answer


2 Answers

I succeeded in creating dynamically the tasks for each of the yaml files you have in your project. I'm using gradle 4.8.1 but I think that applies to next versions too.

import groovy.io.FileType
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

plugins {
    id "org.openapi.generator" version "4.1.3"
}

//here we are going to store swagger files
def swaggerList = []

//iteration by swagger file root folder and save into swaggerList variable
def dir = new File("$rootDir/src/main/resources/api/".toString())
dir.eachFileRecurse(FileType.FILES) { file ->
    if (file.getName().endsWith(".yaml"))
        swaggerList << file
}

// Iterate on all swagger files and generate a task for each one with the nomenclature openApiGenerate + swagger name
swaggerList.each {
    println it.path
    def apiName = it.getName().replace(".yaml", "");

    tasks.create("openApiGenerate" + apiName.capitalize(), GenerateTask.class, {
        generatorName = "jaxrs-spec"
        inputSpec = "$rootDir/src/main/resources/api/".toString() + "${apiName}.yaml"
        outputDir = "$buildDir/generated/openapi".toString()
        apiPackage = "my.package.rest.api.definition.".toString() + "${apiName}"
        modelPackage = "my.package.rest.api.model.".toString() + "${apiName}"
        templateDir = "$rootDir/src/main/resources/api/templates".toString()
        //    https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/jaxrs-spec.md
        configOptions = [
                dateLibrary: "java8",
                java8: true,
                useBeanValidation: true,
                interfaceOnly: true,
                useOptional: true,
                useLombok: true
        ]
    })
}

sourceSets {
    main {
        java {
            srcDir("$buildDir/generated/openapi/src/gen/java")
            srcDir("$buildDir/generated/openapi/src/main/java")
        }
    }
}

After this if I have a swagger file named login.yaml and other named user.yaml user.yaml I call next gradle tasks:

gradle openApiGenerateLogin

Or

gradle openApiGenerateUser
like image 151
atrujillofalcon Avatar answered Sep 22 '22 10:09

atrujillofalcon


Added following code in build.gradle and I was able to generate the java code. Copy the spec1 & spec2 yaml file under schema folder. Additionally, you need to have openApi plugin and dependencies configured in build.gradle.

task buildPaymentClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
generatorName = "spring"
inputSpec = "$rootDir//schema/spec1.yaml".toString()
outputDir = "$rootDir/generated".toString()
apiPackage = "com.abc.code.generated.controller"
modelPackage = "com.abc.code.generated.model"
configOptions = [
        dateLibrary: "java8"
]
systemProperties = [
    invoker : "false", 
    generateSupportingFiles: "true"
]
additionalProperties = [
    interfaceOnly : "true",
]

}

openApiGenerate {
    generatorName = "spring"
    apiPackage = "com.abc.code.generated.controller"
    modelPackage = "com.abc.code.generated.model"
    inputSpec = "$rootDir//schema/spec2.yaml".toString()
    outputDir = "$rootDir/generated".toString()
    configOptions = [
        dateLibrary: "java8"
    ]
    systemProperties = [
        invoker : "false", 
        generateSupportingFiles: "true"
    ]
    additionalProperties = [
        interfaceOnly : "true",
    ]
}

compileJava.dependsOn buildPaymentClient, tasks.openApiGenerate 
like image 23
Nicolas Avatar answered Sep 20 '22 10:09

Nicolas