swagger-api / swagger-api/swagger-codegen

How to generate common classes to a separate package and then reuse them

Open
#10,852 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I can define common classes in a separate schema file:

common.json
{
...
"components": {
"schemas": {
"FinancialContext": {
...
}
}
}

and then reuse it in many other schemas:
"financialContext":
"$ref": "common.json#/components/schemas/FinancialContext"
}

but when I generate models using swagger-codegen-maven-plugin this class will be duplicated in each package for each schema (I cannot generate all the classes to the same package because they can have other classes with the same name, but different content).

I want to be able to generate common.json to a separate package (this is actually possible now) and then all other executions of the plugin should import already generated classes from the common package instead of generating copies of the class in the apis packages.

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 tracing the swagger-codegen-maven-plugin flow for generating a separate package from common.json, then inspect how $ref schemas are handled in subsequent executions. The change is complete when generated API packages import the existing common-package classes instead of duplicating them, while same-named schemas with different content remain separate.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, json
Domain
tooling
Issue type
Feature
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.