Skip to content
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

Fix no URL validation for ModelSchema URLFields #1255

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

lapinvert
Copy link

@lapinvert lapinvert commented Aug 5, 2024

Fixes #1157

Since Django's implementation of of get_internal_type() returns 'CharField' for URLField, we were not able to assign a more appropriate Pydantic type to it, such as AnyUrl. As a result, there was no validation for URLs in post requests that expected a ModelSchema using URLFields.

This PR fixes this by adding a special case for 'URLField' to check it's object type with and assign an internal_type of 'URLField'.

Related tests were also updated.

@lapinvert lapinvert force-pushed the bugfix/fix-no-url-validation-for-modelschema-urlfields branch from 64f5b59 to 5cda539 Compare August 5, 2024 16:21
@lapinvert
Copy link
Author

I added a commit because the previous one would break JSON serialization, since AnyUrl doesn't serialize as a string. It would throw Object of type Url is not JSON serializable.

The problem was discussed over here in Pydantic github and I implemented one of the solutions suggested there by pavradev and updated the tests.

@lapinvert
Copy link
Author

I had to improve on the previous solution which would throw Input should be a valid string as it would appear that validation on individual objects is done (= when response=PostSchema) whereas it's not done when returning lists of objects (response=List[PostSchema]).

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.

[BUG] models.URLField serialized & validated as str
1 participant