Skip to content

Commit 8666d12

Browse files
NaqGuugazmeuk
andauthored
fix: Check for NoneType extension in serialization (#131)
Co-authored-by: Éloi Rivard <eloi@yaal.coop>
1 parent cb74bcf commit 8666d12

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
[0.6.5] - 2026-03-10
5+
--------------------
6+
7+
Fixed
8+
^^^^^
9+
- Fix extension serialization crash when an extension is declared but not populated on a resource serialized outside of SCIM context (e.g. FastAPI ``response_model``). :pr:`131`
10+
411
[0.6.4] - 2026-02-05
512
--------------------
613

scim2_models/resources/resource.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ def _extension_serializer(
117117
For instance, attributes 'meta', 'id' or 'schemas' should not be
118118
dumped when the model is used as an extension for another model.
119119
"""
120+
if value is None:
121+
return None
122+
120123
partial_result = handler(value)
124+
121125
result = {
122126
attr_name: value
123127
for attr_name, value in partial_result.items()

tests/test_resource_extension.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22

33
import pytest
4+
from pydantic import TypeAdapter
45

56
from scim2_models import URN
67
from scim2_models import Context
@@ -370,6 +371,14 @@ def test_class_getitem():
370371
User[int]
371372

372373

374+
def test_dump_resource_with_unset_extension():
375+
"""Serialize a resource whose extension is declared but not populated."""
376+
user = User[EnterpriseUser](user_name="bjensen")
377+
ta = TypeAdapter(User[EnterpriseUser])
378+
payload = ta.dump_python(user)
379+
assert "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User" not in payload
380+
381+
373382
def test_model_attribute_to_scim_attribute_error():
374383
"""Test error case where get_field_root_type returns None."""
375384
from pydantic import Field

0 commit comments

Comments
 (0)