-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
AnyHttpUrl type appends a slash to the URL #6943
Comments
I see the same issue as well. For now my workaround is an ugly hack to do |
I think this is probably the right solution. Pydantic V2 uses the rust |
That might be worth documenting clearly though because as it stands it does break from past behaviour. |
I can confirm this behavior. My CORS origins did not like the change 😅 I agree that if this is the intended result of HttpUrl it should be documented in the migration guide. It took a lot of trial and error to find the root cause since I looked in the documentation but I could not find any pointer. |
I agree a docs update would be nice. Do either of you want to make a PR to get the contribution? Otherwise, I'll make one in the next couple of days. Interestingly I think #6996 would solve this: |
@adriangb If we cannot do nothing with rust crate, maybe I can add it to the migration guide and find a "right" way to solve this? (also relates to #7186 as duplicate) Already found, that url-related migration changes located here: Lines 750 to 757 in ce51f5e
So I'd like to change it like so: diff --git a/docs/migration.md b/docs/migration.md
index c491b366..d452e417 100644
--- a/docs/migration.md
+++ b/docs/migration.md
@@ -756,6 +756,20 @@ classes using `Annotated`.
Inheriting from `str` had upsides and downsides, and for V2 we decided it would be better to remove this. To use these
types in APIs which expect `str` you'll now need to convert them (with `str(url)`).
+Pydantic V2 uses Rust's [Url](https://crates.io/crates/url) crate for URL validation, and it handles urls a little
+differently. It appends slashes to the end of the url (if it is without path), even if it is not specified in the
+original string. For example:
+
+```py
+from pydantic import AnyUrl
+
+
+assert str(AnyUrl(url="https://google.com")) == "https://google.com/"
+assert str(AnyUrl(url="https://google.com/")) == "https://google.com/"
+assert str(AnyUrl(url="https://google.com/api")) == "https://google.com/api"
+assert str(AnyUrl(url="https://google.com/api/")) == "https://google.com/api/"
+```
+
### Constrained types
The `Constrained*` classes were _removed_, and you should replace them by `Annotated[<type>, Field(...)]`, for example:
Also want to show the solution in the docs, but I'm not 100% sure which one is preferred. Maybe we can add Chain or this to the pydantic sources and use them later? Because very simple Thanks in advance! |
Initial Checks
Description
I rely on the fact that my URLs don't have any trailing slashes, so I had a validator that stripped the trailing slash in pydantic v1.
In pydantic v2, the
AnyHttpUrl
type always appends a slash to the URL even if none is provided.Example Code
Python, Pydantic & OS Version
Selected Assignee: @adriangb
The text was updated successfully, but these errors were encountered: