Reputation: 189
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):
skipDefaultInterface
and interfaceOnly
configs too, but still didn't find anything to skip validation.How can I achieve this so that Spring doesn't validate and I can do it by myself with my own validator?
Upvotes: 6
Views: 9754
Reputation: 62653
To remove validation in the generated APIs, you can modify the Mustache templates used by the OpenAPI Generator. For example, for Python, the template look likes:
@validate_call
{{#asyncio}}async {{/asyncio}}def {{operationId}}_with_http_info{{>partial_api_args}} -> ApiResponse[{{{returnType}}}{{^returnType}}None{{/returnType}}]:
{{>partial_api}}
response_data = {{#asyncio}}await {{/asyncio}}self.api_client.call_api(
Just remove the @validate_call
annotation in the template.
Define then a templateDirectory
in your pom.xml
and it will be used at the next build.
Read the documentation for more details.
Upvotes: 0