OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generating java code results in double models model.java, model1.java etc.

Open
#20,454 4 comments 2 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

Description

In our contract, we use $ref to point to shared models. If we use openapi:3.1.0 then it gives duplicate models in the java code we generate. For example, we have a Client.java and a Client1.java.

We have found that if we use $ref along with allOf: that we do not get duplicate models.

Is this a bug and is our use of allOf a correct solution?

openapi-generator version

We are using the npm wrapper "@openapitools/openapi-generator-cli": "^2.15.3".
I think this defaults to the latest version.

OpenAPI declaration file content or url

This results in Office.java and Office1.java
`type: object
title: IdentifiedClientOrRelation
properties:
id:
type: string
number:
type: string
name:
type: string
office:
$ref: '../../../openapi.yml#/components/schemas/Office'
required:

  • id
  • number
  • name
    `

But this results in only Office.java

`type: object
title: IdentifiedClientOrRelation
properties:
id:
type: string
number:
type: string
name:
type: string
office:
allOf:
- $ref: '../../../openapi.yml#/components/schemas/Office'
required:

  • id
  • number
  • name
    `
Generation Details

package.json
{ "name": "e2e", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "generate:server": "openapi-generator-cli generate -c generated-server-code/config.json" }, "author": "", "license": "ISC", "dependencies": { "@openapitools/openapi-generator-cli": "^2.15.3", "mustache": "^4.2.0" }, "devDependencies": { "rimraf": "^6.0.1" } }

Steps to reproduce

npm run generate:server

Related issues/PRs
Suggest a fix

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 package.json and generated-server-code/config.json, then run npm run generate:server against the supplied openapi.yml examples. Compare Java output for a direct $ref and an allOf-wrapped $ref, and determine what behavior should prevent Office.java and Office1.java from being generated.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.