OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] Validation options lost when mapped type name equals schema name

Open
#10,453 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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
  1. Generate the models for the given schema
  2. check the resulting java class for the @Size annotation
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.