-
Notifications
You must be signed in to change notification settings - Fork 340
Fix: generate JsonPatch for @dynamicModel with ...Record<unknown> + backcompat support #9874
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
Merged
+375
−15
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d45922
Initial plan
Copilot 45030c7
fix: generate JsonPatch for dynamic models with Record<unknown> (Bina…
Copilot efb1063
fix: add backcompat support when @dynamicModel is added to a model th…
Copilot cee8e20
refactor: simplify UpdateFullConstructorParameters and add serializat…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
38 changes: 38 additions & 0 deletions
38
...DynamicModelSerializationTests/DeserializeModelWithBinaryDataAdditionalPropsBackCompat.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // <auto-generated/> | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System; | ||
| using System.ClientModel.Primitives; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using Sample.Models; | ||
|
|
||
| namespace Sample | ||
| { | ||
| public partial class DynamicModel | ||
| { | ||
| internal static global::Sample.Models.DynamicModel DeserializeDynamicModel(global::System.Text.Json.JsonElement element, global::System.BinaryData data, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
| { | ||
| if ((element.ValueKind == global::System.Text.Json.JsonValueKind.Null)) | ||
| { | ||
| return null; | ||
| } | ||
| string p1 = default; | ||
| global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> additionalProperties = new global::Sample.ChangeTrackingDictionary<string, global::System.BinaryData>(); | ||
| #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
| global::System.ClientModel.Primitives.JsonPatch patch = new global::System.ClientModel.Primitives.JsonPatch((data is null) ? global::System.ReadOnlyMemory<byte>.Empty : data.ToMemory()); | ||
| #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
| foreach (var prop in element.EnumerateObject()) | ||
| { | ||
| if (prop.NameEquals("p1"u8)) | ||
| { | ||
| p1 = prop.Value.GetString(); | ||
| continue; | ||
| } | ||
| additionalProperties.Add(prop.Name, global::System.BinaryData.FromString(prop.Value.GetRawText())); | ||
| } | ||
| return new global::Sample.Models.DynamicModel(p1, additionalProperties, patch); | ||
| } | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...erializationTests/DeserializeModelWithBinaryDataAdditionalPropsBackCompat/DynamicModel.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // This file represents the previous (last contract) state of the model before @dynamicModel was added. | ||
| // It has AdditionalProperties but no JsonPatch, which triggers the backcompat path. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Sample.Models | ||
| { | ||
| public partial class DynamicModel | ||
| { | ||
| public string P1 { get; set; } | ||
|
|
||
| public global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> AdditionalProperties { get; } | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
...tData/DynamicModelSerializationTests/WriteModelWithBinaryDataAdditionalPropsBackCompat.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // <auto-generated/> | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System; | ||
| using System.ClientModel.Primitives; | ||
| using System.Text.Json; | ||
| using Sample.Models; | ||
|
|
||
| namespace Sample | ||
| { | ||
| public partial class DynamicModel | ||
| { | ||
| global::System.BinaryData global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.DynamicModel>.Write(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => this.PersistableModelWriteCore(options); | ||
|
|
||
| void global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.DynamicModel>.Write(global::System.Text.Json.Utf8JsonWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
| { | ||
| #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
| if (Patch.Contains("$"u8)) | ||
| { | ||
| writer.WriteRawValue(Patch.GetJson("$"u8)); | ||
| return; | ||
| } | ||
| #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
|
|
||
| writer.WriteStartObject(); | ||
| this.JsonModelWriteCore(writer, options); | ||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
| { | ||
| string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.DynamicModel>)this).GetFormatFromOptions(options) : options.Format; | ||
| if ((format != "J")) | ||
| { | ||
| throw new global::System.FormatException($"The model {nameof(global::Sample.Models.DynamicModel)} does not support writing '{format}' format."); | ||
| } | ||
| #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
| if (!Patch.Contains("$.p1"u8)) | ||
| { | ||
| writer.WritePropertyName("p1"u8); | ||
| writer.WriteStringValue(P1); | ||
| } | ||
| foreach (var item in AdditionalProperties) | ||
| { | ||
| writer.WritePropertyName(item.Key); | ||
| #if NET6_0_OR_GREATER | ||
| writer.WriteRawValue(item.Value); | ||
| #else | ||
| using (global::System.Text.Json.JsonDocument document = global::System.Text.Json.JsonDocument.Parse(item.Value)) | ||
| { | ||
| global::System.Text.Json.JsonSerializer.Serialize(writer, document.RootElement); | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| Patch.WriteTo(writer); | ||
| #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. | ||
| } | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...ModelSerializationTests/WriteModelWithBinaryDataAdditionalPropsBackCompat/DynamicModel.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // This file represents the previous (last contract) state of the model before @dynamicModel was added. | ||
| // It has AdditionalProperties but no JsonPatch, which triggers the backcompat path. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| namespace Sample.Models | ||
| { | ||
| public partial class DynamicModel | ||
| { | ||
| public string P1 { get; set; } | ||
|
|
||
| public global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> AdditionalProperties { get; } | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.