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

Honor __serialization_key__ when serializing schema to JSON. #36

Merged
merged 1 commit into from
Oct 9, 2023
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
2 changes: 1 addition & 1 deletion langfun/core/structured/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _node(vs: pg.typing.ValueSpec) -> Any:
return [_node(vs.element.value)]
elif isinstance(vs, pg.typing.Object):
if issubclass(vs.cls, pg.Object):
d = {pg.JSONConvertible.TYPE_NAME_KEY: vs.cls.__type_name__}
d = {pg.JSONConvertible.TYPE_NAME_KEY: vs.cls.__serialization_key__}
d.update(
{
str(k): _node(f.value)
Expand Down
11 changes: 6 additions & 5 deletions langfun/core/structured/schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Itinerary(pg.Object):
]


Itinerary.__serialization_key__ = 'Itinerary'


class Node(pg.Object):
children: list['Node']

Expand Down Expand Up @@ -100,7 +103,7 @@ def test_schema_dict(self):
'result': [
{
'x': {
'_type': Itinerary.__type_name__,
'_type': 'Itinerary',
'day': pg.typing.Int(min_value=1),
'activities': [{
'_type': Activity.__type_name__,
Expand Down Expand Up @@ -139,11 +142,10 @@ def test_schema_repr(self):
self.assertEqual(
schema.schema_str(protocol='json'),
(
'{"result": [{"x": {"_type": "%s", "day":'
'{"result": [{"x": {"_type": "Itinerary", "day":'
' int(min=1), "type": "daytime" | "nighttime", "activities":'
' [{"_type": "%s", "description": str}], "hotel":'
' str(regex=.*Hotel) | None}}]}' % (
Itinerary.__type_name__,
Activity.__type_name__,
)
),
Expand Down Expand Up @@ -495,11 +497,10 @@ def test_repr(self):
self.assertEqual(
schema_lib.SchemaJsonRepr().repr(schema),
(
'{"result": [{"x": {"_type": "%s", "day":'
'{"result": [{"x": {"_type": "Itinerary", "day":'
' int(min=1), "type": "daytime" | "nighttime", "activities":'
' [{"_type": "%s", "description": str}], "hotel":'
' str(regex=.*Hotel) | None}}]}' % (
Itinerary.__type_name__,
Activity.__type_name__,
)
),
Expand Down