Skip to content

fix(): Bug causing single element array serialized into scalar #1164

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

Merged
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 flow360/component/simulation/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _encode_ndarray(x):
"""
encoder for ndarray
"""
if x.size == 1:
if x.shape == ():
return float(x)
return tuple(x.tolist())

Expand Down
14 changes: 14 additions & 0 deletions tests/simulation/framework/test_unit_system_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,20 @@ def test_units_serializer():

assert data_reimport == data

#####
with u.SI_unit_system:
data = ArrayDataWithUnits(
l_arr=[0] * u.rad, # Single element array should come back as an array.
l_arr_nonneg=[1, 1, 1, 1],
)

data_as_json = data.model_dump_json()

with u.CGS_unit_system:
data_reimport = ArrayDataWithUnits(**json.loads(data_as_json))

assert data_reimport == data


def test_units_schema():
schema = Flow360DataWithUnits.model_json_schema()
Expand Down