Description
When a model has properties with @clientDefaultValue, creating an instance without explicitly setting those properties results in their defaults being excluded from JSON serialization.
Root Cause
PR #10476 changed Model.__init__() to no longer pre-populate _data with field defaults. The _RestField.__get__() was updated to lazily return defaults, but the items() method (used by SdkJSONEncoder for serialization) still only returns self._data.items(), missing the lazily-defaulted fields.
Reproduction
from specs.azure.clientgenerator.core.clientdefaultvalue.models import ModelWithDefaultValues
import json
body = ModelWithDefaultValues(name="test")
# body.timeout returns 30 (correct - lazy default)
# but json.dumps(body, cls=SdkJSONEncoder) produces {"name": "test"} (missing timeout, tier, retry)
Expected Behavior
Serialization should include default values: {"name": "test", "timeout": 30, "tier": "standard", "retry": true}
Affected Methods
items() - used by SdkJSONEncoder.default()
- Potentially:
keys(), values(), __iter__(), __len__(), __contains__()
File
packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2
Description
When a model has properties with
@clientDefaultValue, creating an instance without explicitly setting those properties results in their defaults being excluded from JSON serialization.Root Cause
PR #10476 changed
Model.__init__()to no longer pre-populate_datawith field defaults. The_RestField.__get__()was updated to lazily return defaults, but theitems()method (used bySdkJSONEncoderfor serialization) still only returnsself._data.items(), missing the lazily-defaulted fields.Reproduction
Expected Behavior
Serialization should include default values:
{"name": "test", "timeout": 30, "tier": "standard", "retry": true}Affected Methods
items()- used bySdkJSONEncoder.default()keys(),values(),__iter__(),__len__(),__contains__()File
packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2