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 ordering handling of after chaining #228

Merged
merged 1 commit into from
Oct 24, 2021
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
13 changes: 10 additions & 3 deletions apischema/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,16 @@ def sort_by_order(
if not after and not before and len(groups) == 1:
return next(iter(groups.values()))
result = []

def add_to_result(elt: T):
elt_name = name(elt)
for before_elt in before[elt_name]:
add_to_result(before_elt)
result.append(elt)
for after_elt in after[elt_name]:
add_to_result(after_elt)

for value in sorted(groups):
for elt in groups[value]:
result.extend(before[name(elt)])
result.append(elt)
result.extend(after[name(elt)])
add_to_result(elt)
return result
5 changes: 3 additions & 2 deletions examples/class_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from apischema import order, serialize


@order(["baz", "bar"])
@order(["baz", "bar", "biz"])
@dataclass
class Foo:
bar: int
baz: int
biz: str


assert json.dumps(serialize(Foo, Foo(0, 0))) == '{"baz": 0, "bar": 0}'
assert json.dumps(serialize(Foo, Foo(0, 0, ""))) == '{"baz": 0, "bar": 0, "biz": ""}'