Fix: generate JsonPatch for @dynamicModel with ...Record<unknown> + backcompat support#9874
Merged
JoshLove-msft merged 4 commits intomainfrom Mar 4, 2026
Merged
Conversation
…ryData additional properties) Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix JsonPatch generation for dynamic models with additional properties
Fix: generate JsonPatch for @dynamicModel with ...Record<unknown>
Mar 2, 2026
...soft.TypeSpec.Generator.ClientModel/test/Providers/ScmModelProvider/ScmModelProviderTests.cs
Show resolved
Hide resolved
…at was previously shipped with AdditionalProperties Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot
AI
changed the title
Fix: generate JsonPatch for @dynamicModel with ...Record<unknown>
Fix: generate JsonPatch for @dynamicModel with ...Record<unknown> + backcompat support
Mar 4, 2026
commit: |
...-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmModelProvider.cs
Show resolved
Hide resolved
...er/TestData/ScmModelProviderTests/TestDynamicModelWithBinaryDataAdditionalPropsBackCompat.cs
Show resolved
Hide resolved
…ion backcompat tests Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
jorgerangel-msft
approved these changes
Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dynamic models with
...Record<unknown>(BinaryData additional properties) were silently skippingJsonPatchgeneration becauseSupportsBinaryDataAdditionalPropertiesshort-circuitedBuildJsonPatchField(). The model would fall back to a plainAdditionalPropertiesproperty instead of the dynamic patch mechanism.Changes
BuildJsonPatchField(): RemoveSupportsBinaryDataAdditionalPropertiesguard — all@dynamicModeltypes getJsonPatchregardless of additional property typeBuildProperties(): WhenJsonPatchPropertyis set andSupportsBinaryDataAdditionalPropertiesis true, filter outAdditionalProperties(JsonPatch subsumes it forunknownAP) — unless backcompat is neededIsAdditionalBinaryDataParameter()helper: Extracted to detect parameters backed by_additionalBinaryDataPropertieseither via field or property backing fieldBuildConstructors(): Extended body-statement filtering to strip_additionalBinaryDataPropertiesassignments from both the full constructor and the init constructor whenJsonPatchis being usedNeedsBackCompatAdditionalProperties: New lazy property that checks if the previous contract (LastContractView) had anAdditionalPropertiesproperty. When true, bothJsonPatchandAdditionalPropertiesare generated together to maintain backward compatibilityUpdateFullConstructorParameters(): Simplified —patchparameter addition is factored out of the inner backcompat condition and always appended after the AdditionalBinaryData parameter check. In backcompat mode the oldadditionalPropertiesparameter is kept; in all casespatchis added after it.Result
A
@dynamicModelwith...Record<unknown>now generatesJsonPatchonly (noAdditionalProperties):When the model was previously shipped with
AdditionalProperties(backcompat), both are generated:The serialization code for the backcompat case correctly:
AdditionalProperties(iterated as BinaryData) andPatch.WriteTo()inJsonModelWriteCoreIDictionary<string, BinaryData> additionalPropertiesandJsonPatch patch, then constructingnew DynamicModel(p1, additionalProperties, patch)Typed additional properties (
...Record<string>, union types) are unaffected.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.