Search code examples
openapiopenapi-generator

Can I suppress the default Optional<NativeWebRequest> getRequest() that OpenAPI generates into interfaces?


I have an OpenAPI 3.0 file that specifies two REST resources with operations, let's say:

openapi: 3.0.0
[...]
paths:
  /a:
    post:
      [...]
  /b
    post:
      [...]

Then I use the openapi-generator-maven-plugin like:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.1.2</version>
  <configuration>
    [...]
    <configOptions>
      <interfaceOnly>true</interfaceOnly>
      [...]
    </configOptions>
 </configuration>
</plugin>

To generate Java interfaces, giving me:

public interface AApi {

  default Optional<NativeWebRequest> getRequest() {
    return Optional.empty();
  }

  default ResponseEntity<String> postA([...]) { [...] }

}

public interface BApi {

  default Optional<NativeWebRequest> getRequest() {
    return Optional.empty();
  }

  default ResponseEntity<String> postB([...]) { [...] }

}    

In the end, I would like to write a single class that implements both interfaces:

class TheController implements AApi, BApi { [...] }

However, the getRequest() method gets in the way, because Java is unable to inherit two default implementations with identical names.

Is there a way to suppress generating this method? (Or some other means to enable implementing both interfaces, that I haven't thought of?)


Solution

  • You can override the getRequest() method in your implementing controller class.

    For further reading, refer to https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.4