OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.51k stars 6.27k forks source link

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

Open zlatozar opened 4 days ago

zlatozar commented 4 days ago

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