OpenAPITools / OpenAPITools/openapi-generator

[JavaSpring] Import conflict with generated model and one of the libraries of same name

Open
#14,867 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

I have a schema named 'ResponseEntity'. The model class gets generated with the same name.
This conflicts with the ResponseEntiy class of Springframework and gives an error

error: a type with the same simple name is already defined by the single-type-import of ResponseEntity
import org.springframework.http.ResponseEntity;

API File:
default ResponseEntity myClass() {
// code
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}

Here, the func return-type ResponseEntity is from org.springframework.http.ResponseEntity
whereas the Generic type(one in the <>) ResponseEntity is my model class.

Discarded approaches:

  1. I do not want to use importmappings to create an external model and map it to the ResponseEntity model. This will create issues on changing the swagger schema.
  2. I cannot add Suffix/Prefix to all the generated models.

Is there a way such that?

  1. I can add Suffix/Prefix to only that particular generated model.
  2. Any workaround to never let the class 'org.springframework.http.ResponseEntity' be imported. I will add this in the ResponseEntity method of api.mustache class as a prefix.

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 with the Java Spring generator's api.mustache template and reproduce the conflict using a schema containing a ResponseEntity model. Trace how the generated return type and imports are assembled, then verify that generation produces compilable code without the name collision while retaining the schema model.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
backend, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.