-
Notifications
You must be signed in to change notification settings - Fork 334
Add backward compatibility for object-typed AdditionalProperties #9516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add backward compatibility for object-typed AdditionalProperties #9516
Conversation
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
...p/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ModelProviderTests.cs
Show resolved
Hide resolved
…bility Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…operties backward compatibility Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
...rator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/AdditionalPropertiesTest.cs
Show resolved
Hide resolved
...rator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/AdditionalPropertiesTest.cs
Outdated
Show resolved
Hide resolved
… 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>
Updated backward-compatibility.md in commit a6fcfe2. Added new section "AdditionalProperties Type Preservation" that documents:
The section is added to the table of contents and placed after "Model Properties" section. |
|
@copilot run format with write for the docs changes |
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
✅ 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 asIDictionary<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
ShouldUseObjectAdditionalProperties()BuildRawDataField()to create field with object or BinaryData type based on backward compat needspackages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ModelProviderTests.cs
TestBuildProperties_WithObjectAdditionalPropertiesBackwardCompatibilitytestpackages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/AdditionalPropertiesTest.cs
packages/http-client-csharp/generator/docs/backward-compatibility.md
Test Results ✅
Comprehensive Test Coverage
Deserialization test verifies:
IDictionary<string, object> additionalPropertiesChangeTrackingDictionary<string, object>()foreach (var prop in element.EnumerateObject())prop.NameEquals("name"u8)GetString()for known properties,GetObject()for additional propertiesSerialization test verifies:
writer.WritePropertyName("name"u8)writer.WriteStringValue(Name)writer.WritePropertyName(item.Key)writer.WriteObjectValue<object>(item.Value, options)Backward Compatibility Guarantees
Ready for Merge
This PR successfully resolves the issue with comprehensive test coverage and documentation updates.
Original prompt
💡 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.