-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove virtual keyword from struct serial methods & Return Default Fo…
…r Null Elements (#4630) This PR fixes several issues with generated structs: - The `protected virtual` modifiers were removed from the serialization core methods as the modifiers are not valid for structs. - When deserializing the struct, `default` is returned rather than `null` when encountering an JsonElement that is Null. fixes: #4348, #4349
- Loading branch information
1 parent
854024a
commit 7aee12f
Showing
4 changed files
with
187 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
50 changes: 50 additions & 0 deletions
50
...CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/DeserializationTests.cs
This file contains 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,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Linq; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Input; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Providers; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.MrwSerializationTypeDefinitions | ||
{ | ||
public class DeserializationTests | ||
{ | ||
public DeserializationTests() | ||
{ | ||
MockHelpers.LoadMockPlugin(createSerializationsCore: (inputType, typeProvider) | ||
=> inputType is InputModelType modeltype ? [new MockMrwProvider(modeltype, (typeProvider as ModelProvider)!)] : []); | ||
} | ||
|
||
private class MockMrwProvider : MrwSerializationTypeDefinition | ||
{ | ||
public MockMrwProvider(InputModelType inputModel, ModelProvider modelProvider) | ||
: base(inputModel, modelProvider) | ||
{ | ||
} | ||
|
||
protected override MethodProvider[] BuildMethods() | ||
{ | ||
return [.. base.BuildMethods().Where(m => m.Signature.Name.StartsWith("Deserialize"))]; | ||
} | ||
|
||
protected override FieldProvider[] BuildFields() => []; | ||
protected override ConstructorProvider[] BuildConstructors() => []; | ||
} | ||
|
||
[Test] | ||
public void DeserializeStruct() | ||
{ | ||
var inputModel = InputFactory.Model("TestModel", modelAsStruct: true); | ||
|
||
var mrwProvider = new ModelProvider(inputModel).SerializationProviders.FirstOrDefault(); | ||
Assert.IsNotNull(mrwProvider); | ||
var writer = new TypeProviderWriter(mrwProvider!); | ||
var file = writer.Write(); | ||
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content); | ||
} | ||
} | ||
} |
This file contains 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
44 changes: 44 additions & 0 deletions
44
...viders/MrwSerializationTypeDefinitions/TestData/DeserializationTests/DeserializeStruct.cs
This file contains 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,44 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using Sample; | ||
|
||
namespace Sample.Models | ||
{ | ||
/// <summary></summary> | ||
public readonly partial struct TestModel : global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.TestModel>, global::System.ClientModel.Primitives.IJsonModel<object> | ||
{ | ||
internal static global::Sample.Models.TestModel DeserializeTestModel(global::System.Text.Json.JsonElement element, global::System.ClientModel.Primitives.ModelReaderWriterOptions options) | ||
{ | ||
if ((element.ValueKind == global::System.Text.Json.JsonValueKind.Null)) | ||
{ | ||
return default; | ||
} | ||
string stringProperty = default; | ||
global::System.Collections.Generic.IDictionary<string, global::System.BinaryData> additionalBinaryDataProperties = new global::Sample.ChangeTrackingDictionary<string, global::System.BinaryData>(); | ||
foreach (var prop in element.EnumerateObject()) | ||
{ | ||
if (prop.NameEquals("stringProperty"u8)) | ||
{ | ||
if ((prop.Value.ValueKind == global::System.Text.Json.JsonValueKind.Null)) | ||
{ | ||
stringProperty = null; | ||
continue; | ||
} | ||
stringProperty = prop.Value.GetString(); | ||
continue; | ||
} | ||
if ((options.Format != "W")) | ||
{ | ||
additionalBinaryDataProperties.Add(prop.Name, global::System.BinaryData.FromString(prop.Value.GetRawText())); | ||
} | ||
} | ||
return new global::Sample.Models.TestModel(stringProperty, additionalBinaryDataProperties); | ||
} | ||
} | ||
} |