-
Notifications
You must be signed in to change notification settings - Fork 334
Fix inconsistent variable naming in deserialization methods for models with additional properties #9478
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
Conversation
Co-authored-by: JonathanCrd <[email protected]>
...tor/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: JonathanCrd <[email protected]>
|
Testes this fix on my local by running In internal static IsUnknownAdditionalPropertiesDiscriminatedDerived DeserializeIsUnknownAdditionalPropertiesDiscriminatedDerived(JsonElement element, ModelReaderWriterOptions options)
{
// ...
IDictionary<string, BinaryData> additionalProperties = new ChangeTrackingDictionary<string, BinaryData>();
// ...
foreach (var prop in element.EnumerateObject())
{
// ...
additionalProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
}
return new IsUnknownAdditionalPropertiesDiscriminatedDerived(name, kind, additionalProperties, index, age);
} |
| : _additionalBinaryDataProperty.Value.BackingField!.Type; | ||
| propertyDeserializationStatements.Add( | ||
| _additionalBinaryDataProperty.Value.AsVariableExpression.AsDictionary(_additionalBinaryDataProperty.Value.Type).Add(jsonProperty.Name(), binaryDataDeserializationValue)); | ||
| variableExpression.AsDictionary(dictionaryType).Add(jsonProperty.Name(), binaryDataDeserializationValue)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure there is an actual bug here? When I tried to repro in the Search library the variable name was consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me double check
| : _additionalBinaryDataProperty.Value.BackingField!.Type; | ||
| propertyDeserializationStatements.Add( | ||
| _additionalBinaryDataProperty.Value.AsVariableExpression.AsDictionary(_additionalBinaryDataProperty.Value.Type).Add(jsonProperty.Name(), binaryDataDeserializationValue)); | ||
| variableExpression.AsDictionary(dictionaryType).Add(jsonProperty.Name(), binaryDataDeserializationValue)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot can you include some tests for your fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test in commit a85d580. The new test TestAdditionalPropertiesVariableNameConsistency validates that the variable name used in the declaration matches the name used in the .Add() calls, ensuring consistency regardless of whether the property or backing field is in the constructor parameters.
…erialization Co-authored-by: JonathanCrd <[email protected]>
|
Solved on Microsoft teams. This is not a bug, it was happening because of custom code on the user's end. |
Fix Inconsistent Variable Naming in Deserialization Methods
Summary
Fixed variable naming inconsistency in deserialization methods when models have additional properties.
Root Cause
The original fix incorrectly assumed that when
_additionalBinaryDataProperty.Valueexists (a property with backing field_additionalBinaryDataProperties), the backing field would always be used in the constructor. However, the actual logic is:This meant:
additionalProperties(using property's variable name)additionalBinaryDataProperties(backing field's variable name)Solution
Check which one is actually in the constructor parameters:
additionalProperties)additionalBinaryDataProperties)Changes
Testing
✅ All ClientModel unit tests pass (1010 tests, including new test)
✅ All Spector additional-properties tests pass (98 tests)
✅ New test
TestAdditionalPropertiesVariableNameConsistencyvalidates the fixOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.