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] Inner class shadowing when generating openapi.json #862

Closed
ostatni5 opened this issue Sep 20, 2023 · 3 comments
Closed

[BUG] Inner class shadowing when generating openapi.json #862

ostatni5 opened this issue Sep 20, 2023 · 3 comments

Comments

@ostatni5
Copy link

Describe the bug
When two inner classes with the same name are used in different classes to define the field type, only the last one is generated in openapi.json and all refs are pointing to it.

Versions:

  • Python version: 3.11.4
  • Django version: 4.2.5
  • Django-Ninja version: 0.22.2
  • Pydantic version: 1.10.12

Context
I was trying to achieve a result schema with a single value that represents a type of response to be able to parse it in a typescript client properly. I know that in open API spec is something like Discriminator but I don't know how to tell Ninja to generate it pointing to the chosen property name.

Code
Sample schema definition:

class ExA(Schema):
    class ExC(str, enum.Enum):
        foo = "foo"

    type: ExC = ExC.foo


class ExB(Schema):
    class ExC(str, enum.Enum):
        bar = "bar"

    type: ExC = ExC.bar

Schemas in openapi.json

    ExC:
      title: ExC
      description: An enumeration.
      enum:
        - bar
      type: string
    ExA:
      title: ExA
      type: object
      properties:
        type:
          default: foo
          allOf:
            - $ref: '#/components/schemas/ExC'
    ExB:
      title: ExB
      type: object
      properties:
        type:
          default: bar
          allOf:
            - $ref: '#/components/schemas/ExC'

Probably prefixes made from the outer classes could fix this issue.

@OtherBarry
Copy link
Contributor

@ostatni5 there's some workarounds listed in #537 - basically your options are to monkeypatch the schema generation code, or rename your models.

@vitalik
Copy link
Owner

vitalik commented Sep 21, 2023

@ostatni5 just a side note - seems pydantic2 will not allow you making nested classes.. so basically this approach is not recommended if you intend to upgarde to future versions of django ninja

@ostatni5
Copy link
Author

Thanks for information

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

3 participants