1

I'm using the Maven swagger codegen plugin (v 3.0.17). I was curious how, if possible, would I configure the plugin so that when my DTOs are generated from my defined schemas, the required attributes are marked with "@NotNull" or "@NotEmpty" annotations. I have this defined in my inputSpec .yml

...
components:
    ...
  schemas:
    ...
    MyObjectDTO:
      type: object
      properties:
        id:
          type: integer
          format: int32
          readOnly: true
        groupId:
          type: integer
          format: int64
          required: true 
    ...
      required:
        - groupId
      description: my object 

The generated DTO looks like

@Schema(description = "my object")
@Validated
@javax.annotation.Generated(value = "com.myco.codegen.SpringCodegen", date = "2022-07-22T15:18:11.693263100-05:00[America/Chicago]")
public class MyObjectDTO   {

    ,,,
  @JsonProperty("groupId")
  private Integer groupId = null;

When this DTO is passed to a Spring REST controller which is @Validated, if the "groupId" field isn't populated, I would like validation to fail, and ultimately a 400 (bad request) error to be returned.

Maven plugin configuration is

            <configuration>
                <language>com.myco.myproject.mypackage.swagger.codegen.OAS3SpringCodegen
                </language>
                <apiPackage>com.myco.myproject.mypackage.api
                </apiPackage>
                <modelPackage>com.myco.mypackage.api.model
                </modelPackage>
                <languageSpecificPrimitives>true</languageSpecificPrimitives>                   
                <generateApis>true</generateApis>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
                <generateApiDocumentation>true</generateApiDocumentation>
                <generateModels>true</generateModels>
                <generateSupportingFiles>false</generateSupportingFiles>
                <importMappings>
                    <importMapping>LocalDateTime=OffsetDateTime</importMapping>
                </importMappings>
                <configOptions>
                    <throwsException>true</throwsException>                     
                    <interfaceOnly>true</interfaceOnly>
                    <java8>false</java8>
                    <dateLibrary>java8</dateLibrary>
                    <sourceFolder>.</sourceFolder>
                    <useTags>true</useTags>
                </configOptions>
            </configuration>    
Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

Try adding the org.springframework.boot:spring-boot-starter-validation (or just javax.validation:validation-api / jakarta.validation:jakarta.validation-api) dependency to your project and enable useBeanValidation in the plugin configuration:

<configuration>
    ...
    <configOptions>
        ...
        <useBeanValidation>true</useBeanValidation>
    </configOptions>
</configuration> 

Also, try changing the language parameter to the standard <language>java</language> or <language>spring</language>.

dekkard
  • 6,121
  • 1
  • 16
  • 26
  • Hi, Gave this a try but no validation annotations were added to the generated DTOs (only annotations on each field continue to be the @JsonProperty annotations) – Dave Jul 29 '22 at 00:19
  • BTW, what's in `com.myco.myproject.mypackage.swagger.codegen.OAS3SpringCodegen`? Won't the standard `spring` do for your case? – dekkard Jul 29 '22 at 06:35