Skip to content

describing nullable field by anyOf leads to bad swagger-ui #2094

@ChMThiel

Description

@ChMThiel

Using OpenApi 3.1.0 nullable fields are described using anyOf:

---
openapi: 3.1.0
components:
  schemas:
    DTO:
      type: object
      properties:
        name:
          type: string
          examples:
          - Hello World
        localTime:
          $ref: "#/components/schemas/LocalTime"
          type: string
          examples:
          - 13:45:30.123456789
          - 13:45:30.123456789
        localTimeNullable:
          type:
          - string
          - "null"
          examples:
          - 13:45:30.123456789
          - 13:45:30.123456789
          anyOf:
          - $ref: "#/components/schemas/LocalTime"
          - type: "null"
    LocalTime:
      type: string
      format: local-time
      examples:
      - 13:45:30.123456789
      externalDocs:
        description: As defined by 'partial-time' in RFC3339
        url: https://www.rfc-editor.org/rfc/rfc3339.html#section-5.6
paths:
  /hello:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DTO"
      summary: Hello
      tags:
      - Greeting Resource
info:
  title: code-with-quarkus API
  version: 1.0.0-SNAPSHOT

In swagger-ui this yaml will be rendered as follows:
grafik

As you can see (especially if collapsed) the 'real' type of the nullable field is not easy to see.

IMHO instead of using anyOf to describe that the field is nullable, using $ref and a type-array would be better:

---
openapi: 3.1.0
components:
  schemas:
    DTO:
      type: object
      properties:
        name:
          type: string
          examples:
          - Hello World
        localTime:
          $ref: "#/components/schemas/LocalTime"
          type: string
          examples:
          - 13:45:30.123456789
          - 13:45:30.123456789
        localTimeNullable:
          $ref: "#/components/schemas/LocalTime"
          type:
          - string
          - "null"
          examples:
          - 13:45:30.123456789
          - 13:45:30.123456789
    LocalTime:
      type: string
      format: local-time
      examples:
      - 13:45:30.123456789
      externalDocs:
        description: As defined by 'partial-time' in RFC3339
        url: https://www.rfc-editor.org/rfc/rfc3339.html#section-5.6
paths:
  /hello:
    get:
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DTO"
      summary: Hello
      tags:
      - Greeting Resource
info:
  title: code-with-quarkus API
  version: 1.0.0-SNAPSHOT

This would be rendered as follows
grafik

AFAIK the later kind is valid OpenApi 3.1.0, https://editor-next.swagger.io/ shows no warnings/errors

see https://github.com/ChMThiel/quarkus_demo/tree/OpenApi_generator_3.1.0_nullable for reproducer

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions