Skip to content

Commit c8e2c1c

Browse files
committed
Add to dict method to manage Enum serialization
1 parent e8788a6 commit c8e2c1c

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

backend/infrahub/core/schema/attribute_schema.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

33
import enum
4+
from enum import Enum
45
from typing import TYPE_CHECKING, Any, Optional, Union
56

6-
from pydantic import ConfigDict, field_validator, model_validator
7+
from pydantic import field_validator, model_validator
78

89
from infrahub import config
910
from infrahub.core.enums import generate_python_enum
@@ -21,8 +22,6 @@
2122

2223

2324
class AttributeSchema(GeneratedAttributeSchema):
24-
model_config = ConfigDict(use_enum_values=True)
25-
2625
_sort_by: list[str] = ["name"]
2726
_enum_class: Optional[type[enum.Enum]] = None
2827

@@ -38,6 +37,13 @@ def is_relationship(self) -> bool:
3837
def is_deprecated(self) -> bool:
3938
return bool(self.deprecation)
4039

40+
def to_dict(self) -> dict:
41+
data = self.model_dump(exclude_unset=True, exclude_none=True, exclude_defaults=True)
42+
for field_name, value in data.items():
43+
if isinstance(value, Enum):
44+
data[field_name] = value.value
45+
return data
46+
4147
@field_validator("kind")
4248
@classmethod
4349
def kind_options(cls, v: str) -> str:

backend/infrahub/core/schema/basenode_schema.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import keyword
55
import os
66
from dataclasses import asdict, dataclass
7+
from enum import Enum
78
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, Optional, Union, overload
89

910
from infrahub_sdk.utils import compare_lists, intersection
10-
from pydantic import ConfigDict, field_validator
11+
from pydantic import field_validator
1112

1213
from infrahub.core.constants import RelationshipCardinality, RelationshipKind
1314
from infrahub.core.models import HashableModelDiff
@@ -27,8 +28,6 @@
2728

2829

2930
class BaseNodeSchema(GeneratedBaseNodeSchema):
30-
model_config = ConfigDict(use_enum_values=True)
31-
3231
_exclude_from_hash: list[str] = ["attributes", "relationships"]
3332
_sort_by: list[str] = ["namespace", "name"]
3433

@@ -65,7 +64,15 @@ def __hash__(self) -> int:
6564
return hash(self.get_hash())
6665

6766
def to_dict(self) -> dict:
68-
return self.model_dump(exclude_unset=True, exclude_none=True, exclude_defaults=True)
67+
data = self.model_dump(
68+
exclude_unset=True, exclude_none=True, exclude_defaults=True, exclude={"attributes", "relationships"}
69+
)
70+
for field_name, value in data.items():
71+
if isinstance(value, Enum):
72+
data[field_name] = value.value
73+
data["attributes"] = [attr.to_dict() for attr in self.attributes]
74+
data["relationships"] = [rel.to_dict() for rel in self.relationships]
75+
return data
6976

7077
def get_hash(self, display_values: bool = False) -> str:
7178
"""Extend the Hash Calculation to account for attributes and relationships."""

backend/infrahub/core/schema/relationship_schema.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

3+
from enum import Enum
34
from typing import TYPE_CHECKING, Any, Optional, Union
45

5-
from pydantic import BaseModel, ConfigDict
6+
from pydantic import BaseModel
67

78
from infrahub import config
89
from infrahub.core.constants import RelationshipDirection
@@ -19,8 +20,6 @@
1920

2021

2122
class RelationshipSchema(GeneratedRelationshipSchema):
22-
model_config = ConfigDict(use_enum_values=True)
23-
2423
_exclude_from_hash: list[str] = ["filters"]
2524
_sort_by: list[str] = ["name"]
2625

@@ -36,6 +35,13 @@ def is_relationship(self) -> bool:
3635
def is_deprecated(self) -> bool:
3736
return bool(self.deprecation)
3837

38+
def to_dict(self) -> dict:
39+
data = self.model_dump(exclude_unset=True, exclude_none=True, exclude_defaults=True)
40+
for field_name, value in data.items():
41+
if isinstance(value, Enum):
42+
data[field_name] = value.value
43+
return data
44+
3945
def get_class(self) -> type[Relationship]:
4046
return Relationship
4147

0 commit comments

Comments
 (0)