From 6d25e7cbd3326ef787ee6d35a9db466e7109d446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Thu, 29 Jan 2026 19:23:43 +0100 Subject: [PATCH] fix: model_json_schema for Reference and Path --- doc/changelog.rst | 7 +++++++ scim2_models/path.py | 10 ++++++++++ scim2_models/reference.py | 10 ++++++++++ tests/test_models.py | 22 ++++++++++++++++++++++ tests/test_path.py | 11 +++++++++++ tests/test_reference.py | 10 ++++++++++ 6 files changed, 70 insertions(+) diff --git a/doc/changelog.rst b/doc/changelog.rst index c4e4770..1de0aa2 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -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 -------------------- diff --git a/scim2_models/path.py b/scim2_models/path.py index 93f7cdf..5d943cc 100644 --- a/scim2_models/path.py +++ b/scim2_models/path.py @@ -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 @@ -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) diff --git a/scim2_models/reference.py b/scim2_models/reference.py index 221965a..79890eb 100644 --- a/scim2_models/reference.py +++ b/scim2_models/reference.py @@ -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 @@ -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.""" diff --git a/tests/test_models.py b/tests/test_models.py index 7d6465a..36a1dbb 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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" diff --git a/tests/test_path.py b/tests/test_path.py index db2f536..bc7b689 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -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"] diff --git a/tests/test_reference.py b/tests/test_reference.py index 46cfd24..a637bc6 100644 --- a/tests/test_reference.py +++ b/tests/test_reference.py @@ -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"]