[BUG] Nested Enum Classes With Duplicate Names
See original GitHub issueProblem It’s a pretty common pattern with Django classes to contain enums/choices classes as a nested class within the model class that uses the enum. If there are multiple nested classes with the same name, even though they are nested under different classes, the generated OpenAPI schema only uses one of them.
Using __qualname__
instead of __name__
should solve this issue for nested classes.
It might be worth looking into namespacing schemas by prefixing them with their module or django app or something, as I imagine this issue occurs with any duplicated names.
Example
class SomeSchema(Schema):
class Status(str, Enum):
FOO = "foo"
status: SomeSchema.Status
class OtherSchema(Schema)
class Status(str, Enum):
BAR= "bar"
status: OtherSchema.Status
@api.post("/some")
def get_some_thing(request, data: SomeSchema):
return data.status
@api.post("/other")
def get_other_thing(request, data: OtherSchema):
return data.status
Only one of the status enums will be present in the resulting schema.
Versions
- Python version: 3.10.4
- Django version: 3.2.14
- Django-Ninja version: 0.19.1
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
how to overwrite parent class's enum with same name? #1624
Issue description I'm binding a complex c++ library to python, in this lib, it has many inheritance, and for example class A {...
Read more >c++ - Nested enum and nested class have different behavior
Yes, there are such sections in the C++ Standard. The first one is. 9.9 Nested type names. 1 Type names obey exactly the...
Read more >191739 – "Duplicate nested type" bogus error on static ... - Bugs
You need to have a JAR file that references the inner class static class/enum, but doesn't contain that class. We have an ANTLR...
Read more >Nested "enum"s should not be declared static
According to the Java Language Specification-8.9: Nested enum types are implicitly static . So there's no need to declare them static explicitly.
Read more >Enums - C# language specification - Microsoft Learn
Enums create a set of named constants and are represented by an underlying integral set of ... No two enum members can have...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@jmduke I ended up just renaming my enums, so instead of
Order.Status
andProduct.Status
I haveOrder.OrderStatus
andProduct.ProductStatus
, which solved the problem for me.The
__qualname__
fix is a pretty easy solution, but might be a big of a pain to monkeypatch. I believe the relevant function is here.@OtherBarry you beat me to the explanation 😃 There’s a couple todos here that I think feint towards the issue, but I couldn’t quite wrap my head around the indirection that goes on in this module. I agree that a better approach for django-ninja to take might be delegate as much of the mapping + conflict resolution as possible to
pydantic
, which appears to handle it quite well.To answer your question, though, the monkey-patch in question:
The only line here that changes from the original is:
A couple notes:
replace
so as to prettify the results a bit: I wanted a ref ofOrderStatus
rather thanOrder.Status
.schema.get_long_model_name
instead ofmodel.__qualname__
. Similarly, I avoided this on aesthetic bounds becauselong_model_name
includes the entire module path which I didn’t want to expose within the ref.