Skip to content

uuidstr url converter instead of shadowing django uuid #1453

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

Merged
merged 5 commits into from
Apr 20, 2025
Merged

Conversation

vitalik
Copy link
Owner

@vitalik vitalik commented Apr 19, 2025

Before django ninja shadowed django's uuid to be able to to get string value from url:

@api.get('/some/{uuid:id}')
def some(request, id):
     return {'id': id, 'type': str(type(id))}  # type=str

Now added custom converted uuidstr

@api.get('/some/{uuidstr:id}')
def some(request, id):
    # id will be str type here

django 6.0 will not allow shadowing

addresses #1452 #1266

ninja/router.py Outdated
@@ -320,6 +320,13 @@ def add_api_operation(
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
) -> None:
if "{uuid:" in path:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think a regex based check would be better?

Copy link
Owner Author

@vitalik vitalik Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baseplate-admin good question...

wondering if django allows doing stuff like /path/<UUID:var>(case insensitive) or /path/< uuid :var> (space)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think django allows that.

By default, Django's URL dispatcher is case-sensitive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But i still think that regex based check is far better. It helps with edge cases.

For example what if someone does: {uuid:: // Note the double ":"

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just in case replaced it with ignore case regex

@vitalik vitalik merged commit 4a51ecf into master Apr 20, 2025
74 checks passed
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

Successfully merging this pull request may close these issues.

2 participants