OpenAPITools / OpenAPITools/openapi-generator
[BUG] @Size annotation generated for java.util.UUID field causing issues as @Size does not support UUID.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
We are using UUID in one of the path variables for our api. In the open api specification we are putting a minLength constraint on UUID to be minimum 36 characters.
We are using openapi-generator-maven-plugin to generate java classes with spring. By default bean validations are on so generator is creating validation annotations for respective fields.
The code generator maps UUID to java.util.UUID and adds @Size(min=36) for UUID field.
However, as per the documentation of javax.validation.constraints.Size it does not support UUID validation.
This is creating a problem for us because we don't want to turn off validations on our beans.
openapi-generator version
We are using openapi-generator-maven plugin.
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.1.0</version>
OpenAPI declaration file content or url
UUID:
type: string
format: uuid
minLength: 36
Steps to reproduce
Generate code using plugin configuration below:
<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>${basedir}/contract/contract.yml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>api</apiPackage>
<modelPackage>model</modelPackage>
<modelNameSuffix>Dto</modelNameSuffix>
<configOptions>
<delegatePattern>true</delegatePattern>
<interfaceOnly>true</interfaceOnly>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Suggest a fix
I have fixed it by updating the template "beanValidationCore.mustache" to not generate Size annotation with minimum length when type is UUID. I am not sure if this can be used as a permanent fix. If yes, I can raise a PR.
{{^isUuid}}{{!
minLength && maxLength set
}}{{#minLength}}{{#maxLength}}@Size(min={{minLength}},max={{maxLength}}) {{/maxLength}}{{/minLength}}{{!
minLength set, maxLength not
}}{{#minLength}}{{^maxLength}}@Size(min={{minLength}}) {{/maxLength}}{{/minLength}}{{!
minLength not set, maxLength set
}}{{^minLength}}{{#maxLength}}@Size(max={{maxLength}}) {{/maxLength}}{{/minLength}}{{!
@Size: minItems && maxItems set
}}{{#minItems}}{{#maxItems}}@Size(min={{minItems}},max={{maxItems}}) {{/maxItems}}{{/minItems}}{{!
@Size: minItems set, maxItems not
}}{{#minItems}}{{^maxItems}}@Size(min={{minItems}}) {{/maxItems}}{{/minItems}}{{!
@Size: minItems not set && maxItems set
}}{{^minItems}}{{#maxItems}}@Size(max={{maxItems}}) {{/maxItems}}{{/minItems}}{{/isUuid}}
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 spring generator's beanValidationCore.mustache template and reproduce the issue using the UUID schema with minLength 36 and the provided Maven configuration. Confirm that generated java.util.UUID fields do not receive @Size, while the existing template behavior for other constraints remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100