microsoft / microsoft/typespec
[Python] Model properties with @clientDefaultValue are not included in serialization
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
## 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
```python
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`
Contributor guide
Assessment
This issue has not been assessed yet.