Skip to content

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

Open

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

Activity

  1. github-actions commented on Apr 30, 2026

    @github-actions
    Contributor

    🎯 Issue Triage

    A regression introduced by PR #10476 causes model properties with @clientDefaultValue to be omitted from JSON serialization when not explicitly set, because items() only reads from _data rather than including lazily-defaulted fields.

    Category: Bug

    Area: emitter:client:python

    ⚠️ No reproduction provided as a TypeSpec snippet. The issue includes Python-level repro code (not TypeSpec), which is sufficient context for the fix.

    🔍 Analysis
    • Root cause: PR [python] add support for clientDefaultValue to unblock CI #10476 changed Model.__init__() to stop pre-populating _data with defaults, using lazy evaluation in _RestField.__get__() instead. However, items() (used by SdkJSONEncoder) still reads only self._data.items(), so properties that were never explicitly set are invisible to the serializer.
    • Affected file: packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2
    • Affected methods: items(), and potentially keys(), values(), __iter__(), __len__(), __contains__()
    • Impact: Any model using @clientDefaultValue will silently drop those fields from serialization unless the caller explicitly sets them, which can cause incorrect API requests.
    📝 Suggested next steps
    • Update items() (and related iteration methods) in model_base.py.jinja2 to include fields that have lazy defaults but have not been explicitly set in _data
    • Add or update tests for ModelWithDefaultValues serialization to cover this case
    • Verify no other serialization paths (e.g., __repr__, as_dict) share the same gap

    Generated by Agentic Triage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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