OpenAPITools / OpenAPITools/openapi-generator

[BUG] allOf $ref combination causes issues for unnecessary schema level example field in auto-generated openapi.yaml output file

Open
#7,219 1 comment 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
openapi-generator version

4.3.1

Target: python-flask

Description/OpenAPI declaration file

Minimal declaration spec:

TargetOperation:
  type: object
  required:
    - id
    - uuid
    - name
    - order
    - modules
  properties:
    id:
      allOf:
        - $ref: '#/components/schemas/resource_id'
      readOnly: true
    uuid:
      allOf:
        - $ref: '#/components/schemas/resource_uuid'
      description: The operation UUID that corresponds with the target operation
    name:
      type: string
      description: The operation name that corresponds with the target operation
      example: 'Op 260'
      readOnly: true
    order:
      type: integer
      description: The order of the target operation in the assembly line
      example: 1000
    image_path:
      type: string
      description: The path to the target operation image
      example: 'path/to/image'
    modules:
      type: array
      items:
        type: string
        enum: ['ad', 'rca']
      description: The list of module types the operation has
      example: ['ad', 'rca']
      readOnly: true
resource_id:
  type: integer
  format: int64
  description: The id of the resource
resource_uuid:
  type: string
  pattern: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
  description: The uuid of the resource
  example: 'c2759d3c-c83a-11e9-a32f-2a2ae2dbcce4'

Auto generated openapi.yaml file TargetOperation output

    TargetOperation:
      example:
        image_path: path/to/image
        name: Op 260
        id: ""
        uuid: ""
        modules:
        - ad
        - rca
        order: 1000
      properties:
        id:
          allOf:
          - $ref: '#/components/schemas/resource_id'
          readOnly: true
        uuid:
          allOf:
          - $ref: '#/components/schemas/resource_uuid'
          description: The operation UUID that corresponds with the target operation
        name:
          description: The operation name that corresponds with the target operation
          example: Op 260
          readOnly: true
          type: string
        order:
          description: The order of the target operation in the assembly line
          example: 1000
          type: integer
        image_path:
          description: The path to the target operation image
          example: path/to/image
          type: string
        modules:
          description: The list of module types the operation has
          example:
          - ad
          - rca
          items:
            enum:
            - ad
            - rca
            type: string
          readOnly: true
          type: array
      required:
      - id
      - modules
      - name
      - order
      - uuid
      type: object

Notice how the generator adds this schema level example field with incorrect example values for the id and uuid fields:

      ...

      example:
        image_path: path/to/image
        name: Op 260
        id: ""
        uuid: ""
        modules:
        - ad
        - rca
        order: 1000

      ...

This causes Swagger UI to display example values like so:

image

However, if you remove that schema level example field from the generated openapi.yaml output file and restart Swagger UI, you'll see that the examples for the id, uuid and all other fields actually display correctly in the Swagger UI example value tabs.

Not only that, but the request/response example values end up displaying better without the schema level example field by excluding readOnly fields from request example value.

See below:

image

Steps to reproduce
  • Generate a schema that uses an allOf with a $ref.
  • Run the openapi-generator.
  • See that the generator added an example field at the schema level of the openapi.yaml output file with incorrect examples for the fields inside the example field.
Suggest a fix

Do not add the schema level example field in the auto-generated openapi.yaml output file, Swagger UI works better without it.

Without the schema level example field being there, examples for allOf $ref combinations are actually generated properly in Swagger UI and do not just display as an empty string ("").

On top of that, Swagger UI displays the request/response example values better without the schema level example field by actually excluding readOnly fields from request example values and writeOnly fields from response example values.

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 minimal OpenAPI declaration, the python-flask target, and the generated openapi.yaml from version 4.3.1. Trace how schemas using allOf with $ref receive a schema-level example, then compare the generated output with Swagger UI behavior for readOnly and writeOnly fields. Done means the incorrect schema-level example is no longer produced and the affected examples display correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, java, openapi, python
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.