OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java] Schema name collision with template imports
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
If a schema is named "Schema", the import of such class will be import io.swagger.v3.oas.annotations.media.Schema;.
This will also trigger an error because this module is not defined in the pom file by default.
openapi-generator version
7.7.0 and latest (7.8.0-SNAPSHOT)
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Example API
description: Example API
contact: {}
version: 2.0.0
paths: {}
components:
schemas:
Response:
properties:
schema:
$ref: '#/components/schemas/Schema'
Schema:
properties:
test:
type: string
Generation Details
Steps to reproduce
- Create the file
openapi-example-schema.ymlwith the content above in the current directory - run
docker run -vpwd:/temp -it openapitools/openapi-generator-cli:latest generate -g java -i /temp/openapi-example-schema.yml -o /temp/build/ - check the content of the file
build/src/main/java/org/openapitools/client/model/Response.java
Current behavior:
- It contains
import io.swagger.v3.oas.annotations.media.Schema;
Expected behavior:
- it contains
import org.openapitools.client.model.Schema;
Tested the same behavior with other imported classes like HashMap
Related issues/PRs
The error I get, is the same reported in #16932, but root cause could be different
Suggest a fix
Schema is one of the optional imports of the Java code generator: AbstractJavaCodegen.java#L578 and I believe the system automatically use the existing import for it.
In theory this could be solved by adding Schema in the list of reserved words. Doing so will result in a class named SchemaModel. However this will also change the property name called schema to _schema which in reality is not necessary. I'm not sure why all reserved words are compared in a case insensitive way..
I also tried to use reservedWordsMappings, but it seems it's only considered for pre-existing reserved words
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
Reproduce the issue with the provided OpenAPI YAML and Java generator command, then inspect AbstractJavaCodegen.java around the optional imports and reserved-word handling. Compare the generated Response.java import with the expected model import; done means the collision is resolved without unnecessarily renaming the schema property, with regression coverage if the relevant test location is identified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100