Skip to content

Commit 131b844

Browse files
Copilotmsyyc
andauthored
Restore client default value materialization in Model.__init__
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/91e8124d-723d-42e3-a606-d2e9fa8cd443 Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
1 parent 46a6e77 commit 131b844

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-client-python"
5+
---
6+
7+
Fix serialization regression where `@clientDefaultValue` defaults on model properties were no longer included in the request body. Defaults are again materialized in the model's data dictionary at construction time so they are sent on the wire, while the attribute-access fallback for unset fields is preserved.

packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ class Model(_MyMutableMapping):
642642
if v is not None
643643
}
644644
)
645+
# Apply client default values for fields the caller didn't set so that
646+
# defaults are part of `_data` and therefore included during serialization.
647+
for rf in self._attr_to_rest_field.values():
648+
if rf._default is _UNSET:
649+
continue
650+
if rf._rest_name in dict_to_pass:
651+
continue
652+
dict_to_pass[rf._rest_name] = _create_value(rf, rf._default)
645653
super().__init__(dict_to_pass)
646654

647655
def _init_from_xml(self, element: ET.Element) -> dict[str, typing.Any]:

0 commit comments

Comments
 (0)