Skip to content

Commit

Permalink
Fix definition-ref bug with Dict keys (#1014)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle committed Oct 12, 2023
1 parent b51105a commit f67e25c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/definitions.rs
Expand Up @@ -76,7 +76,7 @@ impl TypeSerializer for DefinitionRefSerializer {
}

fn json_key<'py>(&self, key: &'py PyAny, extra: &Extra) -> PyResult<Cow<'py, str>> {
self._invalid_as_json_key(key, extra, Self::EXPECTED_TYPE)
self.definition.get().unwrap().json_key(key, extra)
}

fn serde_serialize<S: serde::ser::Serializer>(
Expand Down
21 changes: 21 additions & 0 deletions tests/serializers/test_definitions.py
Expand Up @@ -113,3 +113,24 @@ def test_use_after():
)
)
assert v.to_python((1, 2)) == ('1', '2')


def test_defs_with_dict():
s = SchemaSerializer(
core_schema.definitions_schema(
schema=core_schema.typed_dict_schema(
{
'foo': core_schema.typed_dict_field(
core_schema.dict_schema(
keys_schema=core_schema.definition_reference_schema('key'),
values_schema=core_schema.definition_reference_schema('val'),
)
)
}
),
definitions=[core_schema.str_schema(ref='key'), core_schema.str_schema(ref='val')],
)
)

assert s.to_json({'foo': {'key': 'val'}}) == b'{"foo":{"key":"val"}}'
assert s.to_python({'foo': {'key': 'val'}}) == {'foo': {'key': 'val'}}

0 comments on commit f67e25c

Please sign in to comment.