Skip to content

fix: fix nullable primitive types defined as list of types (#10388) #10390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025

Conversation

ctyar
Copy link
Contributor

@ctyar ctyar commented Mar 30, 2025

At the moment, Swagger UI doesn't produce a correct Example Value with OpenAPI 3.1.1 definition for a nullable property which is defined as an array of types (e.g., ["null", "string"]).

Description

This PR will change the logic that folds an array of types to ignore null so a better example value would be created.

Motivation and Context

Fixes #10388

How Has This Been Tested?

Tested with the following sample definition:

{
  "openapi": "3.1.1",
  "info": {
    "title": "Playground | v1",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "http://localhost:5150"
    }
  ],
  "paths": {
    "/todos": {
      "post": {
        "tags": [
          "TodoEndpoints"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TodoRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Created"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "NullableOfPriority": {
        "enum": [
          "Low",
          "Medium",
          "High",
          "Critical",
          null
        ]
      },
      "TodoRequest": {
        "required": [
          "description",
          "priority"
        ],
        "type": "object",
        "properties": {
          "description": {
            "type": [
              "null",
              "string"
            ]
          },
          "dueDate": {
            "type": [
              "null",
              "string"
            ],
            "format": "date-time"
          },
          "priority": {
            "$ref": "#/components/schemas/NullableOfPriority"
          },
          "tags": {
            "type": [
              "null",
              "array"
            ],
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "TodoEndpoints"
    }
  ]
}

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

Copy link
Contributor

@glowcloud glowcloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ctyar

Thanks for raising this issue and for the PR!

The generated example you provided in the issue is correct, but we think that deprioritising null in favour of other primitive types would be the best.

Your approach here is good, but we cannot ignore null, e.g. in case of type: ["null"]. Please take a look at the code suggestions.

@ctyar ctyar force-pushed the bug/10388-fix-nullable-types branch from 67e3adf to 2b33f83 Compare April 2, 2025 17:34
@ctyar
Copy link
Contributor Author

ctyar commented Apr 2, 2025

@glowcloud thank you for your review and the great project
Applied all the feedbacks

@glowcloud
Copy link
Contributor

@ctyar Thanks for the changes, LGTM!

@glowcloud glowcloud merged commit d375f50 into swagger-api:master Apr 3, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Swagger UI is generating incorrect Example Value for a nullable property in openapi 3.1.1
2 participants