OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Java] Mapped types should no be be validated by default
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the SpringCodegen path used by the Maven plugin and reproduce the issue using the shown ObjectId schema, type mapping, and import mapping. Compare the generated Set output for a mapped $ref with the output for the unknown objectId type. Done means mapped external element types are not given nested @Valid annotations and the generated Java code compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100