OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java] Validation options lost when mapped type name equals schema name
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
In a project we have a lot of wrapper types around primitives that add description, examples and validation to the bare primitive. Behind the scenes we map these types to simple wrapper classes for increased type safety in our code base.
Our usual pattern is to have a schema with the same name as the wrapper type in our code base with the format of the schema set to the type name in kebab-case. So if we have a wrapper type like CustomerName, then we'd create a schema with name CustomerName, type string and format customer-name. The we'd add a type mapping like string+customer-name=CustomerName plus an appropriate import mapping.
We noticed that if we add validations (e.g. minLength: 1 for the customer name) to these types, then the generator does not generate the @Size(min=1) annotation for bean validation. If the schema name does not match the name of the mapped type, then everything works fine. It seems lose pretty much all information about the type (description, validations, example, ...).
openapi-generator version
version 5.2.1
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: 'test'
version: 'version'
paths: {}
components:
schemas:
SomeCustomType:
type: string
format: some-custom-format
minLength: 1
description: "some description"
Generation Details
- generatorName: java
- library: native
- typeMapping:
string+some-custom-format=SomeCustomType - importMapping:
SomeCustomType=org.example.SomeCustomType
Steps to reproduce
- Generate the models for the given schema
- check the resulting java class for the
@Sizeannotation
Related issues/PRs
#7342 seemed related initially, but I'm not convinced anymore.
Suggest a fix
I assume this triggers some kind of recursive resolution issue, since the schema name is practically mapped to itself.
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 by reproducing the supplied OpenAPI schema with the Java generator, using the native library, type mapping, and import mapping shown in the report. Inspect the generated model and related Java generator entry points to trace why validation metadata is lost when the schema and mapped type share a name; done means the class includes @Size(min=1) and retains the schema metadata.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100