Skip to content

[Python] Model properties with @clientDefaultValue are not included in serialization #10579

@iscai-msft

Description

@iscai-msft

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingemitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions