From 64f5b59441826972799fc29ed4e633815e694d38 Mon Sep 17 00:00:00 2001 From: Remy Date: Mon, 5 Aug 2024 19:22:46 +1000 Subject: [PATCH] Fix no URL validation for ModelSchema URLFields Fixes #1157 --- ninja/orm/fields.py | 8 ++++++-- tests/test_orm_schemas.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ninja/orm/fields.py b/ninja/orm/fields.py index 58ad478e..8ae9ea2b 100644 --- a/ninja/orm/fields.py +++ b/ninja/orm/fields.py @@ -3,9 +3,9 @@ from typing import Any, Callable, Dict, List, Tuple, Type, TypeVar, Union, no_type_check from uuid import UUID -from django.db.models import ManyToManyField +from django.db.models import ManyToManyField, URLField from django.db.models.fields import Field as DjangoField -from pydantic import IPvAnyAddress +from pydantic import AnyUrl, IPvAnyAddress from pydantic.fields import FieldInfo from pydantic_core import PydanticUndefined, core_schema @@ -69,6 +69,7 @@ def validate(cls, value: Any, _: Any) -> Any: "SmallIntegerField": int, "TextField": str, "TimeField": datetime.time, + "URLField": AnyUrl, "UUIDField": UUID, # postgres fields: "ArrayField": List, @@ -148,6 +149,9 @@ def get_schema_field( max_length = field_options.get("max_length") internal_type = field.get_internal_type() + if isinstance(field, URLField): + internal_type = 'URLField' + python_type = TYPES[internal_type] if field.primary_key or blank or null or optional: diff --git a/tests/test_orm_schemas.py b/tests/test_orm_schemas.py index b1f11641..27e2bf27 100644 --- a/tests/test_orm_schemas.py +++ b/tests/test_orm_schemas.py @@ -143,7 +143,7 @@ class Meta: "smallintegerfield": {"type": "integer", "title": "Smallintegerfield"}, "textfield": {"type": "string", "title": "Textfield"}, "timefield": {"type": "string", "format": "time", "title": "Timefield"}, - "urlfield": {"type": "string", "title": "Urlfield"}, + "urlfield": {'format': 'uri', 'minLength': 1, 'title': 'Urlfield', 'type': 'string'}, "uuidfield": {"type": "string", "format": "uuid", "title": "Uuidfield"}, "arrayfield": {"type": "array", "items": {}, "title": "Arrayfield"}, "cicharfield": {"type": "string", "title": "Cicharfield"},