OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java][Spring] Delegate interface has to have default methods only

Open
#12,564 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I'm using the open api plugin to generate spring stubs/models with delegate interface without default methods. Since version 6.0.0 I'm forced to generate delegate with default methods only. I'd like to have a delegate without default methods so further adjustments (like adding new methods) should throw compile errors and the developer should be forced to implement the new method.
If this is a feature and not a bug the flag skipDefaultInterface does not make much sense to me.

The generated controller:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-06-09T16:07:19.513564+02:00[Europe/Berlin]")
@Controller
@RequestMapping("${openapi.sample.base-path:/v1}")
public class UsersApiController implements UsersApi {

    private final UsersApiDelegate delegate;

    public UsersApiController(@Autowired(required = false) UsersApiDelegate delegate) {
        this.delegate = Optional.ofNullable(delegate).orElse(new UsersApiDelegate() {});
    }
}

Since UsersApiDelegate is an interface with abstract methods only the generated UsersApiController throws a compile error.
Because of this line

this.delegate = Optional.ofNullable(delegate).orElse(new UsersApiDelegate() {});

The instantiation of interface new UsersApiDelegate() {} will fail since the method implementations are missing.

openapi-generator version

6.0.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Sample API
  description: Optional multiline or single-line description
  version: 0.1.9
servers:
  - url: http://api.example.com/v1
    description: Optional server description, e.g. Main (production) server
  - url: http://staging-api.example.com
    description: Optional server description, e.g. Internal staging server for testing
paths:
  /users:
    get:
      summary: Returns a list of users.
      description: Optional extended description in CommonMark or HTML.
      responses:
        '200':    # status code
          description: A JSON array of user names
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
Generation Details
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.0</version>
                <executions>
                    <execution>
                        <id>generate-service-api</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/api/service/v1.yml</inputSpec>
                            <output>${project.build.directory}/generated-sources</output>
                            <skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
                            <generatorName>spring</generatorName>
                            <apiPackage>sample.api.service.v1</apiPackage>
                            <modelPackage>sample.api.model.v1</modelPackage>
                            <configOptions>
                                <sourceFolder>openapi</sourceFolder>
                                <delegatePattern>true</delegatePattern>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <java8>false</java8> <!-- avoid build interface default implemenations -->
                                <dateLibrary>java8</dateLibrary> <!-- use OffsetDateTime -->
                                <enablePostProcessFile>true</enablePostProcessFile>
                                <interfaceOnly>false</interfaceOnly>
                            </configOptions>
                            <addCompileSourceRoot>true</addCompileSourceRoot>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

Please check prior steps.

Related issues/PRs
Suggest a fix

This is the commit containing controller changes for spring
https://github.com/OpenAPITools/openapi-generator/commit/380aaa55a187b90c800e7d6c0ac44d43e5d1cf14

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Spring generator changes referenced in commit 380aaa55a187b90c800e7d6c0ac44d43e5d1cf14 and reproduce using the supplied OpenAPI YAML and Maven configuration. Check how skipDefaultInterface=true and java8=false affect the generated UsersApiController and UsersApiDelegate. Done means the generated controller compiles when the delegate interface has abstract methods, with regression coverage for this configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.