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.36k stars 6.46k forks source link

[BUG][KOTLIN][SPRING] Add option to skip generation of default implementations #5987

Open kleinph opened 4 years ago

kleinph commented 4 years ago

Bug Report Checklist

Description

There should be an option the deactivate the generation of code which makes use of supporting files.

openapi-generator version

Version 4.3.0

OpenAPI declaration file content or url

api.yml:

openapi: 3.0.0
info:
  title: Example API
  description: Example REST API
  version: 1.0.0
servers:
  - url: http://localhost:8080
    description: Local development server
paths:
  /foo:
    get:
      summary: Get foo
      operationId: getFoo
      responses:
        200:
          description: Successful operation
          content:
            application/json:
              schema:
                type: string

build.gradle:

openApiGenerate {
    generatorName = "kotlin-spring"

    inputSpec = "$rootDir/src/main/resources/api.yaml".toString()
    outputDir = $buildDir/generated-sources".toString()

    configOptions = [
            dateLibrary: "java8",
            delegatePattern: "true",
    ]
    systemProperties = [
            apis: "",
            models: "",
            supportingFiles: "false"
    ]
}
Command line used for generation

The gradle plugin was used (id 'org.openapi.generator' version '4.3.0').

Steps to reproduce

Use the api.yml and the gradle config to generate the API. There will be default implementations in the *Delegate interfaces which use ApiUtil.kt.

Related issues/PRs

Issue #5476

Suggest a fix

Provide an option to disable code generation for the delegate classes. Like the Java Spring generator has with skipDefaultInterface.

Note that interfaceOnly is no solution because it will prevent generation of the REST controller classes.

Also note that supportingFiles: "ApiUtil,kt" fixes the missing class reference, but requires to deactivate stricter null checks (prevenst use of -Xjsr305=strict compiler args). See #5476.

My workaround so far is to provide my own (null-safe) implementation of ApiUtil.

craigberry1 commented 3 years ago

Does serviceInterface config options solve this?

kleinph commented 3 years ago

@cberry-chwy I am not sure, I am not working anymore on this project.

mwatel42 commented 3 years ago

Hello,

I agree that the behavior of having a default controller interface implementation is not really practical. I would expect the existing code to stop compiling when we add new API methods.

Looking at https://github.com/borsch/openapi-generator/blob/d6bbd51b5078dde410e811f9964bc72259dfe96d/modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache and https://github.com/borsch/openapi-generator/blob/d6bbd51b5078dde410e811f9964bc72259dfe96d/modules/openapi-generator/src/main/resources/kotlin-spring/returnValue.mustache, I suggest to only add a body to the method in case there is a service interface.

WDYT?

vladimirkoba commented 3 years ago

Totally agree, I want to get compilation error, where I add new methods in API

pstorch commented 2 months ago

Looks like the skipDefaultInterface option is implemented in the meantime: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/kotlin-spring.md