Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Java] Mapped types should no be be validated by default #19006

Open
zlatozar opened this issue Jun 24, 2024 · 0 comments
Open

[BUG] [Java] Mapped types should no be be validated by default #19006

zlatozar opened this issue Jun 24, 2024 · 0 comments

Comments

@zlatozar
Copy link

zlatozar commented Jun 24, 2024

Description

We use Maven openAPI generator plugin version 7.6.0 with following settings:

         <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/API.yml</inputSpec>
                            <output>${project.basedir}</output>

                            <generatorName>spring</generatorName>
                            <generateApis>true</generateApis>
                            <generateModels>true</generateModels>

                            <generateSupportingFiles>false</generateSupportingFiles>
                            <configOptions>
                                <useSpringBoot3>true</useSpringBoot3>
                                <useResponseEntity>false</useResponseEntity>
                                <sourceFolder>target/generated-sources/api</sourceFolder>
                                
                               <apiPackage>com.company.product.service.controller</apiPackage>
                                <modelPackage>com.company.product.service.dto</modelPackage>

                                <dateLibrary>java8</dateLibrary>
                                <returnResponse>false</returnResponse>
                                <performBeanValidation>true</performBeanValidation>
                                <useSwaggerAnnotations>true</useSwaggerAnnotations>
                                <useTags>true</useTags>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                
                                <interfaceOnly>true</interfaceOnly>
                                <supportAsync>false</supportAsync>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            </configOptions>

                            <importMappings>
                                <importMapping>ObjectId=org.bson.types.ObjectId</importMapping>
                            </importMappings>
                            <typeMappings>
                                <typeMapping>ObjectId=org.bson.types.ObjectId</typeMapping>
                            </typeMappings>
                            <schemaMappings>
                               <schemaMappings>ObjectId=org.bson.types.ObjectId</schemaMappings>
                            </schemaMappings>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

We would like to use org.bson.types.ObjectId type to reference elements in an array:

  BulkUpdateErpOrderNumberDto:
      type: object
      description: List of replenishment process IDs for which a new ERP order number should be set
      properties:
        erpOrderNumber:
          type: string
          maxLength: 50
          description: SAP (ERP) order number
          example: ERP58207
        rpIds:
          type: array
          uniqueItems: true
          minItems: 1
          items:
            $ref: '#/components/schemas/ObjectId'
          description: Replenishment process IDs
          example: [ 662760d01aad920e6773c164, 662762491aad920e6773c166 ]
      required:
        - rpIds
      
     # Mapped to the Mongo ObjectId object
    ObjectId:
      type: object
      description: ObjectId value
      example: 64a55f3ad5804f0f591b9375

The problem is that generated code tries to validate external mapped types. The generated code can't be compiled because
org.bson.types.ObjectId can't be validated (Set<@Valid org.bson.types.ObjectId>)

@Schema(name = "BulkUpdateErpOrderNumberDto", description = "List of replenishment process IDs for which a new ERP order number should be set")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.6.0")
public class BulkUpdateErpOrderNumberDto {

  private String erpOrderNumber;

  @Valid
  private Set<@Valid org.bson.types.ObjectId> rpIds = new LinkedHashSet<>();

  public BulkUpdateErpOrderNumberDto() {
    super();
  }

  /**
   * Constructor with only required parameters
   */
  public BulkUpdateErpOrderNumberDto(Set<@Valid org.bson.types.ObjectId> rpIds) {
    this.rpIds = rpIds;
  }
}

@Valid annotation shouldn't be added when object is mapped in pom.xml file.

Note that if refer ObjectId with objectId like this:

        rpIds:
          type: array
          uniqueItems: true
          minItems: 1
          items:
            type: objectId
          description: Replenishment process IDs
          example: [ 662760d01aad920e6773c164, 662762491aad920e6773c166 ]

It works with a warning:

[WARNING] Unknown type found in the schema: objectId. To map it, please use the schema mapping option (e.g. --schema-mappings in CLI)

But generated code is correct:

@Schema(name = "BulkUpdateErpOrderNumberDto", description = "List of replenishment process IDs for which a new ERP order number should be set")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.6.0")
public class BulkUpdateErpOrderNumberDto {

  private String erpOrderNumber;

  @Valid
  private Set<org.bson.types.ObjectId> rpIds = new LinkedHashSet<>();

  public BulkUpdateErpOrderNumberDto() {
    super();
  }

  /**
   * Constructor with only required parameters
   */
  public BulkUpdateErpOrderNumberDto(Set<org.bson.types.ObjectId> rpIds) {
    this.rpIds = rpIds;
  }
}

Of course the OpenAPI specification is not valid
Screenshot 2024-06-24 at 14 32 34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant