Skip to content

Commit

Permalink
Fix error message for the json schemas of non str-keyed mappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchanial committed May 15, 2022
1 parent 99e6b23 commit 6868cf1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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
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

0 comments on commit 6868cf1

Please sign in to comment.