Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

✅ Implementation Complete: Add backward compatibility support for object-typed AdditionalProperties

Issue Resolved

Successfully implemented backward compatibility for models with IDictionary<string, object> AdditionalProperties (from old generator) that are now generated as IDictionary<string, BinaryData> (in new generator).

Final Solution

Core Approach: Matching Backing Field Types

The solution detects when a model previously had object-typed AdditionalProperties and creates the backing field with the appropriate type, ensuring perfect type alignment between field and property.

Files Changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs

  • Added object dictionary type support alongside BinaryData
  • Added early backward compatibility detection via ShouldUseObjectAdditionalProperties()
  • Updated BuildRawDataField() to create field with object or BinaryData type based on backward compat needs
  • Updated property generation logic to use matching backing field type
  • Added comprehensive XML documentation
  • Fixed null-safety issues with IsFrameworkType checks

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ModelProviderTests.cs

  • Added TestBuildProperties_WithObjectAdditionalPropertiesBackwardCompatibility test

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/AdditionalPropertiesTest.cs

  • Added comprehensive deserialization test verifying all code aspects
  • Added comprehensive serialization test verifying all code aspects
  • Both tests validate complete generated code with detailed assertions

packages/http-client-csharp/generator/docs/backward-compatibility.md

  • Added new section "AdditionalProperties Type Preservation"
  • Documents the object-to-BinaryData migration scenario
  • Provides examples of before/after code
  • Explains serialization/deserialization behavior differences
  • Formatted with prettier (removed trailing whitespace)

Test Results ✅

  • 1232/1232 Microsoft.TypeSpec.Generator.Tests PASSED
  • 1014/1014 Microsoft.TypeSpec.Generator.ClientModel.Tests PASSED
  • 10/10 AdditionalPropertiesTest tests PASSED
  • 2/2 Serialization backward compatibility tests with comprehensive assertions PASSED

Comprehensive Test Coverage

Deserialization test verifies:

  • Variable declaration: IDictionary<string, object> additionalProperties
  • Dictionary initialization: ChangeTrackingDictionary<string, object>()
  • Property enumeration: foreach (var prop in element.EnumerateObject())
  • Property name checks: prop.NameEquals("name"u8)
  • Type-specific methods: GetString() for known properties, GetObject() for additional properties
  • Constructor call with proper parameters

Serialization test verifies:

  • Format validation and error handling
  • Property name serialization: writer.WritePropertyName("name"u8)
  • Property value serialization: writer.WriteStringValue(Name)
  • Foreach iteration over AdditionalProperties
  • Key serialization: writer.WritePropertyName(item.Key)
  • Value serialization: writer.WriteObjectValue<object>(item.Value, options)
  • Options.Format handling

Backward Compatibility Guarantees

Scenario Result
Existing library with object type ✅ Maintains object type when regenerated
New library without previous version ✅ Uses BinaryData type (new default)
Binary compatibility ✅ Fully maintained - same types
Source compatibility ✅ Fully maintained - code compiles
Serialization ✅ Works with both types - fully verified
Deserialization ✅ Works with both types - fully verified

Ready for Merge

This PR successfully resolves the issue with comprehensive test coverage and documentation updates.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add backcompat support for public AdditionalProperties property on models defined as object valued dictionary</issue_title>
<issue_description>In the old generator, we would generate AdditionalProperties as IDictionary<string, object>. In the new generator, we use IDictionary<string, BinaryData>. We will need to implement support for object for backwards compatibility.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: JoshLove-msft <54595583+JoshLove-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 Jan 29, 2026
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…bility

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Add backcompat support for AdditionalProperties in models Add backward compatibility for object-typed AdditionalProperties Jan 29, 2026
Copilot AI requested a review from JoshLove-msft January 29, 2026 19:55
…operties backward compatibility

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@JoshLove-msft
Copy link
Contributor

… compatibility test

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…ibility.md

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 29, 2026

@copilot we need to update https://github.com/microsoft/typespec/blob/main/packages/http-client-csharp/generator/docs/backward-compatibility.md

Updated backward-compatibility.md in commit a6fcfe2.

Added new section "AdditionalProperties Type Preservation" that documents:

  • The object-to-BinaryData migration scenario
  • Before/after code examples showing type preservation
  • Key points about serialization/deserialization behavior
  • When and why this backward compatibility is applied

The section is added to the table of contents and placed after "Model Properties" section.

@JoshLove-msft JoshLove-msft marked this pull request as ready for review January 29, 2026 22:56
@JoshLove-msft
Copy link
Contributor

@copilot run format with write for the docs changes

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 29, 2026

@copilot run format with write for the docs changes

Formatted backward-compatibility.md with prettier in commit 87f04ec. Removed trailing whitespace from blank lines in code blocks.

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.

Add backcompat support for public AdditionalProperties property on models defined as object valued dictionary

3 participants