I generated just the API server interface from YAML specification (interfaceOnly, skipDefaultInterface). I used the OpenAPI generator maven plugin version 5.1.0. Here is the configuration in my pom.xml:
<!--...-->
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/my-api-1.0-swagger.yaml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>my.service.server</apiPackage>
<modelPackage>my.service.model</modelPackage>
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<useTags>true</useTags>
<interfaceOnly>true</interfaceOnly>
<skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<!--...-->
This way the plugin generates API interface with @Validated
annotation that I don't want, because I want to implement my own validator, and if this annotation is present on the interface then Spring will validate the request.
For example, I have a header parameter that is required in the YAML, so the corresponding method parameter will get @ApiParam(required=true)
and if I don't pass it in the request, Spring will filter out the request saying it doesn't have the required header parameter, and I can't set my custom error response either log a custom message. Example generated API interface:
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Api(value = "MyApi")
public interface MyApi {
@ApiOperation(value = "Clear reservation", nickname = "clear", notes = "Adds an item to the system", tags={ "MyApi", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "..."),
@ApiResponse
//... })
@GetMapping(
value = "/my/api/path",
produces = { "application/json" }
)
ResponseEntity<Void> clear(@ApiParam(required=true) @RequestHeader(value="My-Request-Header", required=true) String myRequiredHeader);
}
I've searched a bit on the web, but the things I found were not much useful (at least I think that):
- automatic request validation with OpenAPI using Kusk Gateway (I have no idea what it is),
- how to turn off the swagger UI validator or something like this, a GitHub issue of swagger UI,
- OpenAPI-generated Spring Boot server doesn't validate required properties, this doesn't solve my problem either,
- https://openapi-generator.tech/docs/generators/spring/ : OpenAPI spring generator documentation - I browsed this site a little, and this is where I found the
skipDefaultInterface
andinterfaceOnly
configs too, but still didn't find anything to skip validation. - Is it possible to disable automatic validation in Spring reviewing my question, stackoverflow suggested me this question, but there are no answers, and the comments suggest to disable Spring validation that doesn't solve my problem; I want the generator not to put the `@Validated`annotation on the method.
Can someone help me please how can I achieve this so that Spring doesn't validate and I can do it by myself with my own validator?