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] Paginated Discriminated Annotated Unions response schemas are overridden in OpenAPI docs by make_response_paginated #1308

Open
M3te0r opened this issue Oct 2, 2024 · 0 comments

Comments

@M3te0r
Copy link

M3te0r commented Oct 2, 2024

Describe the bug
Hi,

In my company project we use extensively Annotated Discriminated Unions both for inputs and outputs schemas
We recentlty encountered wrongly assigned schemas in openapi doc, as we added a paginated endpoint

Here is a minimal reproductible api code:

from typing import Annotated
from typing import Literal
from typing import Union

from ninja import Field
from ninja import NinjaAPI
from ninja import Router
from ninja import Schema
from ninja.pagination import paginate


api = NinjaAPI(
    title="Nova API",
    version="1.0.0",
)


router_foo = Router(auth=None, tags=["RouterFoo"])
router_bar = Router(auth=None, tags=["RouterBar"])


class Foo1Schema(Schema):

    id: int
    disc: Literal["foo1"]


class Foo2Schema(Schema):
    id: int
    disc: Literal["foo2"]


class Bar1Schema(Schema):
    id: int
    disc: Literal["bar1"]


class Bar2Schema(Schema):
    id: int
    disc: Literal["bar2"]


DiscriminatedFooUnion = Annotated[
    Union[Foo1Schema, Foo2Schema],
    Field(discriminator="disc"),
]
DiscriminatedBarUnion = Annotated[
    Union[Bar1Schema, Bar2Schema],
    Field(discriminator="disc"),
]


@router_foo.get("/", response={200: list[DiscriminatedFooUnion]})
@paginate
def foos_endpoint(request):
    return []


@router_bar.get("/", response={200: list[DiscriminatedBarUnion]})
@paginate
def bars_endpoint(request):
    return []


api.add_router("/router_foo", router_foo)
api.add_router("/router_bar", router_bar)

This code will result in wrongly assigned Paged schemas in openapi doc

image

I believe this is caused by make_response_paginated and more precisely by the type creation being made with new_name = f"Paged{item_schema.__name__}" resulting in new_name = PagedAnnotated which is always the same name for Annotated schemas
As it's the same name, it probably overwrites previous schemas of same name in schemas registry

image

Versions (please complete the following information):

  • Python version: 3.12.4
  • Django version: 5.0.9
  • Django-Ninja version: 1.3.0
  • Pydantic version: 2.9.2

This is kind of problematic for us as our frontend team generates TS client types validation from the OpenAPI schemas

@M3te0r M3te0r changed the title [BUG] Paginated Discriminated Annotated Unions response schemas are overridden by make_response_paginated [BUG] Paginated Discriminated Annotated Unions response schemas are overridden in OpenAPI docs by make_response_paginated Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant