'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 1:[1]
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
Solution 2:[2]
I know this question is already answered, but I also had this issue and found a different solution. You can also add this in the configOptions section:
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
It does have the 'side effect' that you no longer have default implementations.
See also documentation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rohan |
Solution 2 | Tumelo Galenos |