Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/msgspec/_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
schema["maxItems"] = t.max_length
if t.min_length is not None:
schema["minItems"] = t.min_length
if isinstance(t, (mi.SetType, mi.FrozenSetType)):
schema["uniqueItems"] = True
elif isinstance(t, mi.TupleType):
schema["type"] = "array"
schema["minItems"] = schema["maxItems"] = len(t.item_types)
Expand Down
50 changes: 42 additions & 8 deletions tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,33 @@ def test_newtype():
}


@pytest.mark.parametrize(
"typ", [list, tuple, set, frozenset, List, Tuple, Set, FrozenSet]
)
@pytest.mark.parametrize("typ", [list, tuple, List, Tuple])
def test_sequence_any(typ):
assert msgspec.json.schema(typ) == {"type": "array"}


@pytest.mark.parametrize(
"cls", [list, tuple, set, frozenset, List, Tuple, Set, FrozenSet]
)
@pytest.mark.parametrize("cls", [list, tuple, List, Tuple])
def test_sequence_typed(cls):
args = (int, ...) if cls in (tuple, Tuple) else int
typ = cls[args]
assert msgspec.json.schema(typ) == {"type": "array", "items": {"type": "integer"}}


@pytest.mark.parametrize("typ", [set, frozenset, Set, FrozenSet])
def test_set_any(typ):
assert msgspec.json.schema(typ) == {"type": "array", "uniqueItems": True}


@pytest.mark.parametrize("cls", [set, frozenset, Set, FrozenSet])
def test_set_typed(cls):
typ = cls[int]
assert msgspec.json.schema(typ) == {
"type": "array",
"uniqueItems": True,
"items": {"type": "integer"},
}


@pytest.mark.parametrize("cls", [tuple, Tuple])
def test_tuple(cls):
typ = cls[int, float, str]
Expand Down Expand Up @@ -1105,7 +1116,7 @@ def test_binary_metadata(typ, field, n, constraint):
}


@pytest.mark.parametrize("typ", [list, tuple, set, frozenset])
@pytest.mark.parametrize("typ", [list, tuple])
@pytest.mark.parametrize(
"field, constraint",
[("min_length", "minItems"), ("max_length", "maxItems")],
Expand All @@ -1115,6 +1126,21 @@ def test_array_metadata(typ, field, constraint):
assert msgspec.json.schema(typ) == {"type": "array", constraint: 2}


@pytest.mark.parametrize("typ", [set, frozenset])
@pytest.mark.parametrize(
"field, constraint",
[("min_length", "minItems"), ("max_length", "maxItems")],
)
def test_set_metadata(typ, field, constraint):
typ = Annotated[typ[int], Meta(**{field: 2})]
assert msgspec.json.schema(typ) == {
"type": "array",
"uniqueItems": True,
"items": {"type": "integer"},
constraint: 2,
}


@pytest.mark.parametrize(
"field, constraint",
[("min_length", "minProperties"), ("max_length", "maxProperties")],
Expand Down Expand Up @@ -1209,7 +1235,15 @@ class ExDataclass:
"properties": {
"b": {
"anyOf": [
{"items": {"items": r1, "type": "array"}, "type": "array"},
{
"items": {
"items": r1,
"type": "array",
"uniqueItems": True,
},
"type": "array",
"uniqueItems": True,
},
{"type": "integer"},
]
}
Expand Down
Loading