swagger-api / swagger-api/swagger-codegen

[Java] Automatically resolve nested models while remotely referring a model

Open
#9,698 2 comments 0 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

Description

We are using swagger codegen maven plugin to generate models and API clients.

We have a single YAML file for defining all common models and refer them using UR as and when required from all other services. The problem is swagger won't resolve the nested models automatically and it expects all nested models to be defined in the same file (we can reference the remote definition here also but defining the models itself become tedious when there is deep nesting).

Is there any workaround for the same? Can we do something to just reference the root level definition and swagger will automatically resolve all other nested references?

Swagger-codegen version

2.1.6

Swagger declaration file content or url

Example

common.yaml

StatusUpdateRequest:
    description: Status update request
    required:
      - status
    properties:
      status:
        $ref: '#/definitions/Status'

Status:
    description: Status
    type: string
    enum: &status
      - ACTIVE
      - INACTIVE
    default: ACTIVE

Let's say my common.yaml is available at https://example.com/commons.yaml, then my individual service level yaml will be like below

ServiceA.yaml

  /abc/{id}/status:
    parameters:
      - $ref: '#/parameters/IdParam'

    post:
      description: Update status
      operationId: updateStatus
      parameters:
        - name: status
          in: body
          required: true
          schema:
            $ref: '#/definitions/StatusUpdateRequest'
      responses:
        200:
          description: Update done

parameters:

IdParam:
    name:id
    in: path
    required: true
    type: string

definitions:

StatusUpdateRequest:
    $ref: 'https://example.com/commons.yaml#/definitions/StatusUpdateRequest'

But above will fail because swagger won't automatically resolve nested Status definition. We'll have to define Status also in ServiceA.yaml.

Below will work.

StatusUpdateRequest:
    $ref: 'https://example.com/commons.yaml#/definitions/StatusUpdateRequest'

Status:
    $ref: 'https://example.com/commons.yaml#/definitions/Status'
Suggest a fix/enhancement

I think codegen should automatically resolve all nested references. We can use various rules while doing so.
For example, if someone wants to refer a model at different location for a nested model, they can explicitly define that along with root-level model definition.

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 case with the swagger-codegen Maven plugin using the shown common.yaml and ServiceA.yaml definitions. Trace how the root-level remote $ref is resolved and verify whether nested Status references are followed. Done means ServiceA.yaml can reference StatusUpdateRequest remotely without redeclaring Status, while preserving the option for an explicit nested override.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.