|
1 | 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP |
2 | 2 |
|
| 3 | +exports[`PYTHON_PYDANTIC_PRESET should render default value for discriminator when using polymorphism 1`] = ` |
| 4 | +Array [ |
| 5 | + "", |
| 6 | + "class Car(BaseModel): |
| 7 | + vehicle_type: VehicleType.VehicleType = Field(default=VehicleType.VehicleType.CAR, frozen=True) |
| 8 | + length: Optional[float] = Field(default=None) |
| 9 | + additional_properties: Optional[dict[str, Any]] = Field(default=None, exclude=True) |
| 10 | +
|
| 11 | + @model_serializer(mode='wrap') |
| 12 | + def custom_serializer(self, handler): |
| 13 | + serialized_self = handler(self) |
| 14 | + additional_properties = getattr(self, \\"additional_properties\\") |
| 15 | + if additional_properties is not None: |
| 16 | + for key, value in additional_properties.items(): |
| 17 | + # Never overwrite existing values, to avoid clashes |
| 18 | + if not hasattr(serialized_self, key): |
| 19 | + serialized_self[key] = value |
| 20 | +
|
| 21 | + return serialized_self |
| 22 | +
|
| 23 | + @model_validator(mode='before') |
| 24 | + @classmethod |
| 25 | + def unwrap_additional_properties(cls, data): |
| 26 | + if not isinstance(data, dict): |
| 27 | + data = data.model_dump() |
| 28 | + json_properties = list(data.keys()) |
| 29 | + known_object_properties = ['vehicle_type', 'length', 'additional_properties'] |
| 30 | + unknown_object_properties = [element for element in json_properties if element not in known_object_properties] |
| 31 | + # Ignore attempts that validate regular models, only when unknown input is used we add unwrap extensions |
| 32 | + if len(unknown_object_properties) == 0: |
| 33 | + return data |
| 34 | + |
| 35 | + known_json_properties = ['vehicleType', 'length', 'additionalProperties'] |
| 36 | + additional_properties = data.get('additional_properties', {}) |
| 37 | + for obj_key in list(data.keys()): |
| 38 | + if not known_json_properties.__contains__(obj_key): |
| 39 | + additional_properties[obj_key] = data.pop(obj_key, None) |
| 40 | + data['additional_properties'] = additional_properties |
| 41 | + return data |
| 42 | +
|
| 43 | +", |
| 44 | + "class VehicleType(Enum): |
| 45 | + CAR = \\"Car\\" |
| 46 | + TRUCK = \\"Truck\\"", |
| 47 | + "class Truck(BaseModel): |
| 48 | + vehicle_type: VehicleType.VehicleType = Field(default=VehicleType.VehicleType.TRUCK, frozen=True) |
| 49 | + length: Optional[float] = Field(default=None) |
| 50 | + additional_properties: Optional[dict[str, Any]] = Field(default=None, exclude=True) |
| 51 | +
|
| 52 | + @model_serializer(mode='wrap') |
| 53 | + def custom_serializer(self, handler): |
| 54 | + serialized_self = handler(self) |
| 55 | + additional_properties = getattr(self, \\"additional_properties\\") |
| 56 | + if additional_properties is not None: |
| 57 | + for key, value in additional_properties.items(): |
| 58 | + # Never overwrite existing values, to avoid clashes |
| 59 | + if not hasattr(serialized_self, key): |
| 60 | + serialized_self[key] = value |
| 61 | +
|
| 62 | + return serialized_self |
| 63 | +
|
| 64 | + @model_validator(mode='before') |
| 65 | + @classmethod |
| 66 | + def unwrap_additional_properties(cls, data): |
| 67 | + if not isinstance(data, dict): |
| 68 | + data = data.model_dump() |
| 69 | + json_properties = list(data.keys()) |
| 70 | + known_object_properties = ['vehicle_type', 'length', 'additional_properties'] |
| 71 | + unknown_object_properties = [element for element in json_properties if element not in known_object_properties] |
| 72 | + # Ignore attempts that validate regular models, only when unknown input is used we add unwrap extensions |
| 73 | + if len(unknown_object_properties) == 0: |
| 74 | + return data |
| 75 | + |
| 76 | + known_json_properties = ['vehicleType', 'length', 'additionalProperties'] |
| 77 | + additional_properties = data.get('additional_properties', {}) |
| 78 | + for obj_key in list(data.keys()): |
| 79 | + if not known_json_properties.__contains__(obj_key): |
| 80 | + additional_properties[obj_key] = data.pop(obj_key, None) |
| 81 | + data['additional_properties'] = additional_properties |
| 82 | + return data |
| 83 | +
|
| 84 | +", |
| 85 | +] |
| 86 | +`; |
| 87 | + |
3 | 88 | exports[`PYTHON_PYDANTIC_PRESET should render nullable union 1`] = ` |
4 | 89 | Array [ |
5 | 90 | "class NullableUnionTest(BaseModel): |
|
0 commit comments