Skip to content

Fix CanonicalTypeProvider to respect CodeGenMember when categorizing redefined properties#10276

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-xml-serialization-properties
Closed

Fix CanonicalTypeProvider to respect CodeGenMember when categorizing redefined properties#10276
Copilot wants to merge 2 commits intomainfrom
copilot/fix-xml-serialization-properties

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 6, 2026

When a property is renamed via [CodeGenMember] and the original name is redefined as a new custom property, CanonicalTypeProvider.BuildProperties() incorrectly categorized the redefined property as a spec property — causing it to silently disappear from the canonical view. The root cause: the property categorization loop at lines 160-171 checked _specPropertiesMap directly without consulting _renamedProperties, unlike the existing TryGetSpecProperty() method which already had this guard.

Changes

  • CanonicalTypeProvider.cs: Aligned the custom property categorization logic with TryGetSpecProperty() — properties matching a spec name are now checked against _renamedProperties/_renamedFields before being treated as spec properties. Redefined properties without [CodeGenMember] are correctly routed to nonSpecProperties.

  • ModelCustomizationTests.cs: Updated CanChangePropertyNameAndRedefineOriginal to assert the redefined property now appears in the canonical view (without WireInfo, as expected for a non-spec property).

  • XmlSerializationCustomizationTests.cs: Added CanChangePropertyNameAndRedefineOriginal test covering the XML serialization path — validates that the [CodeGenMember] property is used for serialization/deserialization, not the redefined wrapper.

Example scenario

public partial class ShareAccessPolicy
{
    [CodeGenMember("Start")]
    public DateTimeOffset? PolicyStartsOn { get; set; }  // ← should be used for serialization

    public DateTimeOffset StartsOn { get; set; }          // ← wrapper, should NOT be serialized
}

Before: StartsOn could incorrectly steal the spec property mapping from PolicyStartsOn.
After: PolicyStartsOn is always preferred; StartsOn is treated as a non-spec custom property.

…erialization

Fix CanonicalTypeProvider.BuildProperties() to use the same _renamedProperties
check as TryGetSpecProperty when categorizing custom properties. This ensures
that when a property is renamed via CodeGenMember and the original name is
redefined as a new custom property, the CodeGenMember property is always
preferred for serialization, and the redefined property is correctly treated
as a non-spec property in the canonical view.

Add XML serialization test for the CanChangePropertyNameAndRedefineOriginal
scenario to prevent regression.

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/c29130f0-3438-4d5b-9a35-74add018c73e

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Apr 6, 2026
Copilot AI changed the title [WIP] Fix XML serialization to respect CodeGenMember attributes Fix CanonicalTypeProvider to respect CodeGenMember when categorizing redefined properties Apr 6, 2026
Copilot AI requested a review from jorgerangel-msft April 6, 2026 23:05
@jorgerangel-msft jorgerangel-msft deleted the copilot/fix-xml-serialization-properties branch April 7, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Always Respect Properties with CodeGenMember Attribute For XML Serialization

2 participants