Skip to content

Commit 4160826

Browse files
Fix url python serialization (#11331)
1 parent f94e842 commit 4160826

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

pydantic/networks.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,14 @@ def build(
290290
)
291291

292292
@classmethod
293-
def serialize_url(cls, url: Any) -> str:
293+
def serialize_url(cls, url: Any, info: core_schema.SerializationInfo) -> str | Self:
294294
if not isinstance(url, cls):
295295
raise PydanticSerializationUnexpectedValue(
296296
f"Expected `{cls}` but got `{type(url)}` with value `'{url}'` - serialized value may not be as expected."
297297
)
298-
return str(url)
298+
if info.mode == 'json':
299+
return str(url)
300+
return url
299301

300302
@classmethod
301303
def __get_pydantic_core_schema__(
@@ -314,7 +316,9 @@ def wrap_val(v, h):
314316
return core_schema.no_info_wrap_validator_function(
315317
wrap_val,
316318
schema=core_schema.url_schema(**cls._constraints.defined_constraints),
317-
serialization=core_schema.plain_serializer_function_ser_schema(cls.serialize_url),
319+
serialization=core_schema.plain_serializer_function_ser_schema(
320+
cls.serialize_url, info_arg=True, when_used='always'
321+
),
318322
)
319323

320324
@classmethod
@@ -465,12 +469,14 @@ def build(
465469
)
466470

467471
@classmethod
468-
def serialize_url(cls, url: Any) -> str:
472+
def serialize_url(cls, url: Any, info: core_schema.SerializationInfo) -> str | Self:
469473
if not isinstance(url, cls):
470474
raise PydanticSerializationUnexpectedValue(
471475
f"Expected `{cls}` but got `{type(url)}` with value `'{url}'` - serialized value may not be as expected."
472476
)
473-
return str(url)
477+
if info.mode == 'json':
478+
return str(url)
479+
return url
474480

475481
@classmethod
476482
def __get_pydantic_core_schema__(
@@ -489,7 +495,9 @@ def wrap_val(v, h):
489495
return core_schema.no_info_wrap_validator_function(
490496
wrap_val,
491497
schema=core_schema.multi_host_url_schema(**cls._constraints.defined_constraints),
492-
serialization=core_schema.plain_serializer_function_ser_schema(cls.serialize_url),
498+
serialization=core_schema.plain_serializer_function_ser_schema(
499+
cls.serialize_url, info_arg=True, when_used='always'
500+
),
493501
)
494502

495503
@classmethod

tests/test_networks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,15 @@ def test_unexpected_ser() -> None:
11891189
match="Expected `<class 'pydantic.networks.HttpUrl'>` but got `<class 'str'>` with value `'http://example.com'`",
11901190
):
11911191
ta.dump_python('http://example.com', warnings='error')
1192+
1193+
1194+
def test_url_ser() -> None:
1195+
ta = TypeAdapter(HttpUrl)
1196+
assert ta.dump_python(HttpUrl('http://example.com')) == HttpUrl('http://example.com')
1197+
assert ta.dump_json(HttpUrl('http://example.com')) == b'"http://example.com/"'
1198+
1199+
1200+
def test_url_ser_as_any() -> None:
1201+
ta = TypeAdapter(Any)
1202+
assert ta.dump_python(HttpUrl('http://example.com')) == HttpUrl('http://example.com')
1203+
assert ta.dump_json(HttpUrl('http://example.com')) == b'"http://example.com/"'

0 commit comments

Comments
 (0)