OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.51k forks source link

[REQ] [Spring] ApiFirst should generate apis and models #9213

Open ShishkinDmitriy opened 3 years ago

ShishkinDmitriy commented 3 years ago

Is your feature request related to a problem? Please describe.

I am trying to generate spring project with apiFirst = true. And I see it doesn't generate api and models, only delegates and home controller to serve openapi json and yaml files. As I understand it is because of these lines: https://github.com/OpenAPITools/openapi-generator/blob/c974c512ff590c797db9f3e93c079017d65d3dcd/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java#L411-L414

Describe the solution you'd like

I think if it is api-first solution apis and models should be generated from yaml. Also plugin openapi-generator-maven-plugin already has controls which can be used for same control: generateApis, generateModels.

bademux commented 1 year ago

@ShishkinDmitriy, unfortunately boolean flags is main plague of openapi-generator

UPD: please find gradle task with minimal leftovers (sadly some support files as still generated, just gitignore them) used with openapi gradle plugin 6.2.1

task generateXServerApi(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    group = 'openapi tools'
    description = 'X API'

    validateSpec = true
    generatorName = 'spring'
    inputSpec = "$rootDir/misc/openapi/x_api.yml"
    outputDir = sourceSets.generatedXApi.java.srcDirs.first() as String
    apiFilesConstrainedTo = ['']
    modelFilesConstrainedTo = ['']
    supportingFilesConstrainedTo = ['ApiUtil.java']
    apiPackage = "${project.group}.${project.name}.api"
    modelPackage = "${project.group}.${project.name}.api.dto"
    modelNameSuffix = 'ApiDto'
    configOptions = [
            library                : 'spring-boot',
            sourceFolder           : '',
            skipOverwrite          : false,
            skipDefaultInterface   : false,
            interfaceOnly          : true,
            useTags                : true,
            hideGenerationTimestamp: true,
            documentationProvider  : 'none',
            useBeanValidation      : true,
            performBeanValidation  : true,
            enumUnknownDefaultCase : true,
    ].collectEntries { [it.key, it.value as String] }
    additionalProperties = [
            useSpringfox   : false,
            openApiNullable: false,
            useSpringBoot3 : true,
    ].collectEntries { [it.key, it.value as String] }

    tasks.compileJava.dependsOn it
}