Skip to content
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

Bug: Enum OAS issues #3518

Open
wallseat opened this issue May 23, 2024 · 1 comment · May be fixed by #3525
Open

Bug: Enum OAS issues #3518

wallseat opened this issue May 23, 2024 · 1 comment · May be fixed by #3525
Labels
Bug 🐛 This is something that is not working as expected

Comments

@wallseat
Copy link

wallseat commented May 23, 2024

Description

I have two problems with how Enum declared in OAS.

  1. When I create 3 different models with same enum, i find, that in always paste in schema As Is. There is not schema declared in components->schemas section of OAS.
    It cause a problem with frontend, when they create bindings with code-gen tools. They need to handle this strings directly.
    I think it is better to register Enum as objects in schemas and refer to them.

  2. When you try to make an Enum field optional, instead of getting oneOf{null, schema{enum{...}} you get enum{..., null}, which is incorrect, i think. Because i'm not realy declared null as value of this enum.
    This problem caused by the same logic as above.

I have code to fix it (we use it as monkey-patch in our project), so if we confirm that this is a problem, i'll provide PR.

URL to code causing the issue

No response

MCVE

from enum import StrEnum

from litestar import Litestar, get
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import JsonRenderPlugin
from pydantic import BaseModel, Field


class TestEnum(StrEnum):
    """ "Description!"""

    ONE = "one"
    TWO = "two"


class TestSchemaA(BaseModel):
    enum_field: TestEnum | None = Field(default=None)
    other_filed: int | None = Field(default=None)


class TestSchemaB(BaseModel):
    enum_field: TestEnum
    other_field: int


@get("/a")
async def test_routerA() -> TestSchemaA:
    return TestSchemaA(enum_field=TestEnum.ONE)


@get("/b")
async def test_routerB() -> TestSchemaB:
    return TestSchemaB(enum_field=TestEnum.TWO)


app = Litestar(
    route_handlers=[test_routerA, test_routerB],
    openapi_config=OpenAPIConfig(
        path="/docs",
        title="Test Api",
        version="0.1.0",
        render_plugins=[JsonRenderPlugin()],
    ),
)

Result schema

{
  "info": {
    "title": "Test Api",
    "version": "0.1.0"
  },
  "openapi": "3.1.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/a": {
      "get": {
        "summary": "TestRoutera",
        "operationId": "ATestRoutera",
        "responses": {
          "200": {
            "description": "Request fulfilled, document follows",
            "headers": {

            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TestSchemaA"
                }
              }
            }
          }
        },
        "deprecated": false
      }
    },
    "/b": {
      "get": {
        "summary": "TestRouterb",
        "operationId": "BTestRouterb",
        "responses": {
          "200": {
            "description": "Request fulfilled, document follows",
            "headers": {

            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TestSchemaB"
                }
              }
            }
          }
        },
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {
      "TestSchemaA": {
        "properties": {
          "enum_field": {
            "type": [
              "null",
              "string"
            ],
            "enum": [
              "one",
              "two",
              null]
          },
          "other_filed": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "type": "integer"
              }
            ]
          }
        },
        "type": "object",
        "required": [],
        "title": "TestSchemaA"
      },
      "TestSchemaB": {
        "properties": {
          "enum_field": {
            "type": "string",
            "enum": [
              "one",
              "two"
            ]
          },
          "other_field": {
            "type": "integer"
          }
        },
        "type": "object",
        "required": [
          "enum_field",
          "other_field"
        ],
        "title": "TestSchemaB"
      }
    }
  }
}

Litestar Version

2.8.2


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@wallseat wallseat added the Bug 🐛 This is something that is not working as expected label May 23, 2024
@wallseat wallseat changed the title Bug: Enum get null possible value when optional and not referable Bug: Enum get null as possible value when optional and it is not referable May 23, 2024
@wallseat wallseat changed the title Bug: Enum get null as possible value when optional and it is not referable Bug: Enum get null as possible value when field typed as optional May 23, 2024
@wallseat wallseat changed the title Bug: Enum get null as possible value when field typed as optional Bug: Enum OAS issues May 23, 2024
@provinzkraut
Copy link
Member

@wallseat Your suggestions sound reasonable, feel free to submit a PR with the proposed changes :)

@wallseat wallseat linked a pull request May 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants