Skip to content

Add custom URL convertor register#1437

Merged
Kludex merged 7 commits intomasterfrom
feat/custom-convertor
Jan 28, 2022
Merged

Add custom URL convertor register#1437
Kludex merged 7 commits intomasterfrom
feat/custom-convertor

Conversation

@Kludex
Copy link
Owner

@Kludex Kludex commented Jan 27, 2022

Custom URL Convertor

Currently, we don't have a way to create URL convertors, meaning that our users can only match the default types. Those are:
https://github.com/encode/starlette/blob/2b54f427613ebd1a6ccb2031aa97364e2d5070a5/starlette/convertors.py#L75-L81

To be fair, there's a way: adding a key, value pair on the CONVERTOR_TYPES dictionary. That's not available on Starlette's public API, or documented.

The idea of this PR is to document and introduce a public API that can be used to create custom URL convertors.

How does Django and Flask handle this?

Both of them have a way to register custom URL convertors. You can find the way Django makes it possible here, and for Flask here.

Other

Recently, there was a PR to introduce time related convertors on #1398, and I think we should follow the path this PR proposes here, and not creating more convertors on Starlette itself.

Missing

  • docs (Do we want a single page with this, or it should be a section on the Routing page?)

@Kludex Kludex changed the title Add register that allows custom URL convertors Add custom URL convertor register Jan 27, 2022
@@ -0,0 +1,99 @@
import datetime as dt
Copy link
Owner Author

Choose a reason for hiding this comment

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

I've created multiple convertors here with the purpose of reproducing #1398. I can also simplify the test suite if wanted. Just let me know.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with simplifying the tests, as this can be extended for other things like UUID and there's not really much difference. This is really good.

@Kludex Kludex requested a review from a team January 27, 2022 09:46
@lovelydinosaur
Copy link
Contributor

Well this is nice and simple isn't it? Seems worthwhile.
Would need some brief docs.

@aminalaee
Copy link
Contributor

@Kludex I guess this PR can close this #1058 with a reference/example, what do you think?

@Kludex
Copy link
Owner Author

Kludex commented Jan 27, 2022

@Kludex I guess this PR can close this #1058 with a reference/example, what do you think?

Yeah, this PR closes that issue as well. I've used another convertor as an example on the docs, but we can answer that issue with "how to register/change the path convertor now".

@Kludex Kludex merged commit 199fc70 into Kludex:master Jan 28, 2022
@Kludex Kludex deleted the feat/custom-convertor branch February 17, 2023 18:41
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.

Add user-defined converters to routing

3 participants