swagger-api / swagger-api/swagger-codegen

[Java] format is not respected in $ref

Open
#8,994 0 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

When $ref is used to define a field type, the format in the $ref is not respected.

In the example below, defining a field as type: string, format: uuid directly in the field works (the resulting field has type UUID in Java), but putting that exact same definition in a $ref does not work (the resulting field has type String in Java).

Swagger-codegen version

2.4.0 on docker

Swagger declaration file content or url

This yaml works correctly:

swagger: '2.0'

paths:
  /:
    get:
      produces:
      - application/json
      responses:
        200:
          schema:
            $ref: '#/definitions/Response'

definitions:
  Response:
    type: object
    required:
      - message_id
    properties:
      message_id:
        type: string
        format: uuid
        minLength: 36
        maxLength: 36
        pattern: '^[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-4[0-9a-fA-F]{3}\\-[8-b][0-9a-fA-F]{3}\\-[0-9a-fA-F]{12}$'
        example: '00000000-0000-4444-8888-000000000000'

It produces:

  @SerializedName("message_id")
  private UUID messageId = null;

This yaml does not work:

swagger: '2.0'

paths:
  /:
    get:
      produces:
      - application/json
      responses:
        200:
          schema:
            $ref: '#/definitions/Response'

definitions:
  Response:
    type: object
    required:
      - message_id
    properties:
      message_id:
        $ref: '#/definitions/uuid'

  uuid:
    type: string
    format: uuid
    minLength: 36
    maxLength: 36
    pattern: '^[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-4[0-9a-fA-F]{3}\\-[8-b][0-9a-fA-F]{3}\\-[0-9a-fA-F]{12}$'
    example: '00000000-0000-4444-8888-000000000000'

It produces:

  @SerializedName("message_id")
  private String messageId = null;
Command line used for generation

docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli:2.4.0 generate -i /local/demo.yaml -l java -o /local/out/java

...where demo.yaml is populated with one of the two yaml examples above.

Steps to reproduce
  • Create demo.yaml with the first yaml above
  • Run docker command
  • Observe that Response.java defines messageId as UUID (correct)
  • Replace demo.yaml contents with second yaml above
  • Run docker command
  • Observe that Response.java defines messageId as String (incorrect)
Related issues/PRs

#8498 is similar, but critically does not use uuid as the format in the defined type

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

Reproduce the two schemas from demo.yaml with the documented Docker command and compare the generated Response.java fields. Trace how the Java generator resolves the $ref definition's format, then verify that the referenced uuid definition produces UUID rather than String and add regression coverage if the repository provides a matching generator test location.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.