Skip to content

Commit

Permalink
fix: restore trait value coercion to string
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed Jul 4, 2023
1 parent 298c32b commit 7ac59a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion flag_engine/identities/traits/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from pydantic import BaseModel
from typing import Any, get_args

from pydantic import BaseModel, validator

from flag_engine.identities.traits.types import TraitValue

TRAIT_VALUE_TYPES = get_args(TraitValue)


class TraitModel(BaseModel):
trait_key: str
trait_value: TraitValue = ...

@validator("trait_value", pre=True)
def convert_trait_value(cls, value: Any) -> TraitValue:
if isinstance(value, TRAIT_VALUE_TYPES):
return value
return str(value)

class Config:
smart_union: bool = True
4 changes: 4 additions & 0 deletions tests/unit/identities/test_identities_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any

import pytest

from flag_engine.features.models import FeatureModel, FeatureStateModel
Expand Down Expand Up @@ -186,6 +187,9 @@ def test_get_hash_key_with_use_identity_composite_key_for_hashing_disabled(ident
(False, False),
(0.0, 0.0),
(0, 0),
(None, None),
([], "[]"),
(["SUPERADMIN"], "['SUPERADMIN']"),
],
)
def test_trait_model__deserialize__expected_trait_value(
Expand Down

0 comments on commit 7ac59a2

Please sign in to comment.