1

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):

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?

tkalvin
  • 71
  • 1
  • 8
  • Did you try to add this in the plugin config "skipValidateSpec" – Tsvetoslav Tsvetkov Nov 10 '22 at 16:18
  • @TsvetoslavTsvetkov I tried it now, but unfortunately it didn't work, the generated interface is the same, and the framework still validates the request. – tkalvin Nov 10 '22 at 16:42
  • @TsvetoslavTsvetkov By the way, I've just found this documentation about `skipValidateSpec`: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md#general-configuration-parameters This says that this option makes the generator skip the validation of the source schema, so this is not what I'm looking for. – tkalvin Nov 11 '22 at 14:24
  • @tkalvin did you find any solve for this? – user1799308 May 04 '23 at 13:33
  • @user1799308 unfortunately not, and I stopped searching for solutions because my senior colleague preferred not to use api generation :( But a workaround could be that you implement a validator that does nothing and config it in spring somehow, sg like this could solve the problem. – tkalvin May 05 '23 at 21:45
  • Setting `false...` removes `@Validated` and `@Valid` annotations from the generated code. Your header will still be annotated as `required = true` though, so this might not completely solve your problem. – gebi May 07 '23 at 12:52

0 Answers0