Skip to content

Commit e8788a6

Browse files
committed
Add to_dict method to NodeSchema
1 parent 23106cc commit e8788a6

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

backend/infrahub/core/schema/attribute_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import enum
44
from typing import TYPE_CHECKING, Any, Optional, Union
55

6-
from pydantic import field_validator, model_validator
6+
from pydantic import ConfigDict, field_validator, model_validator
77

88
from infrahub import config
99
from infrahub.core.enums import generate_python_enum
@@ -21,6 +21,8 @@
2121

2222

2323
class AttributeSchema(GeneratedAttributeSchema):
24+
model_config = ConfigDict(use_enum_values=True)
25+
2426
_sort_by: list[str] = ["name"]
2527
_enum_class: Optional[type[enum.Enum]] = None
2628

backend/infrahub/core/schema/basenode_schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, Optional, Union, overload
88

99
from infrahub_sdk.utils import compare_lists, intersection
10-
from pydantic import field_validator
10+
from pydantic import ConfigDict, field_validator
1111

1212
from infrahub.core.constants import RelationshipCardinality, RelationshipKind
1313
from infrahub.core.models import HashableModelDiff
@@ -27,6 +27,8 @@
2727

2828

2929
class BaseNodeSchema(GeneratedBaseNodeSchema):
30+
model_config = ConfigDict(use_enum_values=True)
31+
3032
_exclude_from_hash: list[str] = ["attributes", "relationships"]
3133
_sort_by: list[str] = ["namespace", "name"]
3234

@@ -62,6 +64,9 @@ def __hash__(self) -> int:
6264
Be careful hash generated from hash() have a salt by default and they will not be the same across run"""
6365
return hash(self.get_hash())
6466

67+
def to_dict(self) -> dict:
68+
return self.model_dump(exclude_unset=True, exclude_none=True, exclude_defaults=True)
69+
6570
def get_hash(self, display_values: bool = False) -> str:
6671
"""Extend the Hash Calculation to account for attributes and relationships."""
6772

backend/infrahub/core/schema/definitions/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@
10981098
},
10991099
],
11001100
},
1101-
core_repository.model_dump(),
1101+
core_repository.to_dict(),
11021102
core_read_only_repository,
11031103
{
11041104
"name": "TransformJinja2",

backend/infrahub/core/schema/relationship_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import TYPE_CHECKING, Any, Optional, Union
44

5-
from pydantic import BaseModel
5+
from pydantic import BaseModel, ConfigDict
66

77
from infrahub import config
88
from infrahub.core.constants import RelationshipDirection
@@ -19,6 +19,8 @@
1919

2020

2121
class RelationshipSchema(GeneratedRelationshipSchema):
22+
model_config = ConfigDict(use_enum_values=True)
23+
2224
_exclude_from_hash: list[str] = ["filters"]
2325
_sort_by: list[str] = ["name"]
2426

0 commit comments

Comments
 (0)