Skip to content

Commit

Permalink
Fix "type" not being included when defaults are excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
christiansandberg committed Sep 19, 2024
1 parent 7d76fb8 commit fdcc59a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion onedm/sdf/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import datetime
from typing import Annotated, Any, Literal, Union

from pydantic import Field, NonNegativeInt
from pydantic import Field, NonNegativeInt, field_serializer
from pydantic_core import SchemaValidator, core_schema

from .common import CommonQualities
Expand All @@ -26,6 +26,10 @@ class DataQualities(CommonQualities, ABC):
default: Any | None = None
choices: dict[str, Data] | None = Field(None, alias="sdfChoice")

@field_serializer("type")
def always_include_type(self, type: str, _):
return type

def _get_base_schema(self) -> core_schema.CoreSchema:
"""Implemented by sub-classes."""
raise NotImplementedError
Expand Down
5 changes: 5 additions & 0 deletions tests/sdf/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ def test_object_data(test_model: sdf.SDF):

def test_unknown_data(test_model: sdf.SDF):
assert isinstance(test_model.data["Unknown"], sdf.AnyData)


def test_serialization_exluding_defaults():
integer = sdf.IntegerData()
assert "type" in integer.model_dump(exclude_defaults=True)

0 comments on commit fdcc59a

Please sign in to comment.