Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message for the json schemas of non str-keyed mappings. #412

Merged
merged 3 commits into from
Dec 12, 2022
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
4 changes: 2 additions & 2 deletions apischema/json_schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ def mapping(
with context_setter(self):
self._ignore_first_ref = True
key = self.visit(key_type)
if key["type"] != JsonType.STRING:
raise ValueError("Mapping types must string-convertible key")
if "type" not in key or key["type"] != JsonType.STRING:
raise ValueError("Mapping types must have string-convertible keys")
value = self.visit(value_type)
if "pattern" in key:
return json_schema(
Expand Down
6 changes: 1 addition & 5 deletions tests/integration/test_deserialize_with_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ def test_coerce_json():
key = "test"
value = 2
ret = deserialize(
MyClass,
{
"my_property": f'{{"{key}": {value}}}',
},
coerce=_coerce_json,
MyClass, {"my_property": f'{{"{key}": {value}}}'}, coerce=_coerce_json
)
assert isinstance(ret, MyClass)
assert isinstance(ret.my_property, dict) and ret.my_property[key] == value
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date
from typing import Any, Dict, Mapping

import pytest

Expand All @@ -8,6 +9,20 @@
from apischema.typing import Annotated, TypedDict


class MyDict(dict):
pass


@pytest.mark.parametrize(
"tp", [dict, Dict[int, Any], pytest.param(MyDict, marks=pytest.mark.xfail), Mapping]
)
def test_dict(tp):
with pytest.raises(ValueError, match="string-convertible keys"):
deserialization_schema(tp)
with pytest.raises(ValueError, match="string-convertible keys"):
serialization_schema(tp)


class TD1(TypedDict, total=False):
key1: str

Expand Down