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
AnyUrl escapes percent sign in URLs without TLD that are already escaped. #4458
Comments
Good catch, thank you for reporting, caused by #3061. @FaresAhmedb what do you think? I'm not on my laptop, so can't confirm until tomorrow. |
It seems that from pydantic import BaseModel, AnyUrl
class DebugAnyUrlBuild(AnyUrl):
def build(**kwargs):
print(f'{kwargs=}')
return AnyUrl.build(**kwargs)
class Foo(BaseModel):
url: DebugAnyUrlBuild
Foo(url='https://localhost/foo%20bar')
# > kwargs={'scheme': 'https', 'user': None, 'password': None, 'host': 'localhost', 'tld': None, 'host_type': 'int_domain', 'port': None, 'path': '/foo%20bar', 'query': None, 'fragment': None}
Foo(url='https://example.com/foo%20bar')
# > While |
I can confirm the bug. Yes, we another possible solution would be to check if the string is quoted and then prevent re-quoting. |
Here url is None when no TLD and than it calls build
AnyUrl rebuilds url, because tld is None anf rebuild flag becomes True
https://github.com/pydantic/pydantic/blob/main/pydantic/networks.py#L366 |
Thanks so much for looking into this. I'm really sorry, it seems the originally implementation was not satisfactory, I made a mistake in merging it when it wasn't ready. I've implemented #4469 which tries to solve the problem but also provides a bunch more test cases to ensure behaviour.
Lastly the initial implementation wrongly quoted Please review and test #4469 and hopefully we can get this right ( |
Initial Checks
Description
When using
AnyUrl
with a localhost URL (or any URL without a top level domain), if the provided URL already is encoded/escaped, pydantic will escape the percent sign again. When the domain has a TLD, it works perfectly fine. I've confirmed that the bug isn't present in 1.9.1. It seems to have been introduced in 1.10.0 and is still present in 1.10.1.Example Code
Python, Pydantic & OS Version
Affected Components
.dict()
and.json()
construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: