Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

[0.6.3] - 2026-01-29
--------------------

Fixed
^^^^^
- Fix ``model_json_schema()`` generation for models containing :class:`~scim2_models.Reference` or :class:`~scim2_models.Path` fields. :issue:`125`

[0.6.2] - 2026-01-25
--------------------

Expand Down
10 changes: 10 additions & 0 deletions scim2_models/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from typing import TypeVar

from pydantic import GetCoreSchemaHandler
from pydantic import GetJsonSchemaHandler
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import core_schema

from .base import BaseModel
Expand Down Expand Up @@ -129,6 +131,14 @@ def validate_path(value: Any) -> "Path[Any]":
serialization=core_schema.plain_serializer_function_ser_schema(str),
)

@classmethod
def __get_pydantic_json_schema__(
cls,
_core_schema: core_schema.CoreSchema,
_handler: GetJsonSchemaHandler,
) -> JsonSchemaValue:
return {"type": "string"}

def __init__(self, path: "str | Path[Any]"):
if isinstance(path, Path):
path = str(path)
Expand Down
10 changes: 10 additions & 0 deletions scim2_models/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from typing import get_origin

from pydantic import GetCoreSchemaHandler
from pydantic import GetJsonSchemaHandler
from pydantic.json_schema import JsonSchemaValue
from pydantic_core import Url
from pydantic_core import ValidationError
from pydantic_core import core_schema
Expand Down Expand Up @@ -112,6 +114,14 @@ def validate(value: Any) -> "Reference[Any]":
serialization=core_schema.plain_serializer_function_ser_schema(str),
)

@classmethod
def __get_pydantic_json_schema__(
cls,
_core_schema: core_schema.CoreSchema,
_handler: GetJsonSchemaHandler,
) -> JsonSchemaValue:
return {"type": "string", "format": "uri"}

@classmethod
def get_scim_reference_types(cls) -> list[str]:
"""Return referenceTypes for SCIM schema generation."""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,25 @@ def test_everything_is_optional():
]
for model in models:
model()


def test_json_schema_generation():
"""Test that all pre-defined models can generate a JSON Schema."""
models = [
User,
User[EnterpriseUser],
EnterpriseUser,
Group,
Schema,
ResourceType,
ServiceProviderConfig,
ListResponse[User],
PatchOp[User],
BulkRequest,
BulkResponse,
SearchRequest,
Error,
]
for model in models:
schema = model.model_json_schema()
assert schema["type"] == "object"
11 changes: 11 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,14 @@ def test_path_init_with_path_object():
copy = Path(original)
assert str(copy) == "userName"
assert copy.data == original.data


def test_path_json_schema_generation():
"""Test that models with Path fields can generate JSON Schema."""

class ModelWithPath(BaseModel):
path: Path[User] | None = None

schema = ModelWithPath.model_json_schema()
assert schema["type"] == "object"
assert "path" in schema["properties"]
10 changes: 10 additions & 0 deletions tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,13 @@ def test_reference_invalid_type_raises_error():
"""Test that invalid type parameter raises TypeError."""
with pytest.raises(TypeError, match="Invalid reference type"):
Reference[123]


def test_reference_json_schema_generation():
"""Test that models with Reference fields can generate JSON Schema."""
schema = ReferenceTestModel.model_json_schema()
assert schema["type"] == "object"
assert "uriref" in schema["properties"]
assert "extref" in schema["properties"]
assert "resourceref" in schema["properties"]
assert "multiref" in schema["properties"]
Loading