Open viktorvsk opened 6 months ago
Hi @viktorvsk
interesting trick..
in general django-ninja generates operation id as string like "<module-name>_<function_name>"
- and because you have all in one module that probably gives an issue
you can try to use operation_id parameter:
def get_router(self):
router = Router()
@router.get("/", response=List[self.schema_out], operation_id=f"list_{self.class_name}_records")
def list_records(request, filters: self.list_filter_schema = Query(...)):
return self.record.objects.all()
OR overwrite get_openapi_operation_id globally - see docs and example here https://django-ninja.dev/reference/operations-parameters/#operation_id
Hey @viktorvsk thanks, thats definitely a much cleaner way to handle this. And given its already documented probably this issue could be closed
Describe the bug
In case you want to create routers dynamically like the following (when you have a lot of similar resources for example):
Where your implementation may look like the following:
Currently everything will work correctly except for Swagger. Swagger will correctly display endpoints for
users
andposts
with correct schemas but it will actually send requests only to/users/
(even when you click Try it Out on theposts
section) in this case because in the example we've definedadd_router("/users"/)
first.I assume its because somewhere under the hood Swagger uses function names to resolve endpoints. Next simple hack fixes this by changing the implementation to:
Notice this line
list_records.__name__ = f"list_{self.class_name}_records"
It seems like a dirty hack (it is). And it seems like its not a popular way in python to handle this. I wasn't able to google anything similar. But just in case someone has the same problem, maybe it would be helpful to keep it here.
P.S. If I'm doing something wrong with this approach entirely would be great to get feedback because I'm not yet much experienced in python/django so appreciate any advice :)
Versions: