diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs index 004d37334b..80b55075be 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs @@ -842,7 +842,7 @@ private List BuildDeserializePropertiesStatements(ScopedApi if (_additionalBinaryDataProperty != null) { - var binaryDataDeserializationValue = ClientModelPlugin.Instance.TypeFactory.GetValueTypeDeserializationExpression( + var binaryDataDeserializationValue = ClientModelPlugin.Instance.TypeFactory.DeserializeJsonValue( _additionalBinaryDataProperty.Type.ElementType.FrameworkType, jsonProperty.Value(), SerializationFormat.Default); propertyDeserializationStatements.Add( _additionalBinaryDataProperty.AsVariableExpression.AsDictionary(_additionalBinaryDataProperty.Type).Add(jsonProperty.Name(), binaryDataDeserializationValue)); @@ -850,7 +850,7 @@ private List BuildDeserializePropertiesStatements(ScopedApi else if (rawBinaryData != null) { var elementType = rawBinaryData.Type.Arguments[1].FrameworkType; - var rawDataDeserializationValue = ClientModelPlugin.Instance.TypeFactory.GetValueTypeDeserializationExpression(elementType, jsonProperty.Value(), SerializationFormat.Default); + var rawDataDeserializationValue = ClientModelPlugin.Instance.TypeFactory.DeserializeJsonValue(elementType, jsonProperty.Value(), SerializationFormat.Default); propertyDeserializationStatements.Add(new IfStatement(_isNotEqualToWireConditionSnippet) { rawBinaryData.AsVariableExpression.AsDictionary(rawBinaryData.Type).Add(jsonProperty.Name(), rawDataDeserializationValue) @@ -1267,11 +1267,11 @@ private ValueExpression CreateDeserializeValueExpression(CSharpType valueType, S valueType switch { { IsFrameworkType: true } when valueType.FrameworkType == typeof(Nullable<>) => - ClientModelPlugin.Instance.TypeFactory.GetValueTypeDeserializationExpression(valueType.Arguments[0].FrameworkType, jsonElement, serializationFormat), + ClientModelPlugin.Instance.TypeFactory.DeserializeJsonValue(valueType.Arguments[0].FrameworkType, jsonElement, serializationFormat), { IsFrameworkType: true } => - ClientModelPlugin.Instance.TypeFactory.GetValueTypeDeserializationExpression(valueType.FrameworkType, jsonElement, serializationFormat), + ClientModelPlugin.Instance.TypeFactory.DeserializeJsonValue(valueType.FrameworkType, jsonElement, serializationFormat), { IsEnum: true } => - valueType.ToEnum(ClientModelPlugin.Instance.TypeFactory.GetValueTypeDeserializationExpression(valueType.UnderlyingEnumType!, jsonElement, serializationFormat)), + valueType.ToEnum(ClientModelPlugin.Instance.TypeFactory.DeserializeJsonValue(valueType.UnderlyingEnumType!, jsonElement, serializationFormat)), _ => valueType.Deserialize(jsonElement, _mrwOptionsParameterSnippet) }; @@ -1584,48 +1584,43 @@ private MethodBodyStatement CreateValueSerializationStatement( SerializationFormat serializationFormat, ValueExpression value) { + // append the `.Value` if needed (when the type is nullable and a value type) + value = value.NullableStructValue(type); + + // now we just need to focus on how we serialize a value if (type.IsFrameworkType) - return ClientModelPlugin.Instance.TypeFactory.SerializeValueType(type, serializationFormat, value, type.FrameworkType, _utf8JsonWriterSnippet, _mrwOptionsParameterSnippet); + return ClientModelPlugin.Instance.TypeFactory.SerializeJsonValue(type.FrameworkType, value, _utf8JsonWriterSnippet, _mrwOptionsParameterSnippet, serializationFormat); if (!type.IsEnum) return _utf8JsonWriterSnippet.WriteObjectValue(value.As(type), options: _mrwOptionsParameterSnippet); - var enumerableSnippet = value.NullableStructValue(type).As(type); if (type.IsStruct) //is extensible { if (type.UnderlyingEnumType.Equals(typeof(string))) - return _utf8JsonWriterSnippet.WriteStringValue(enumerableSnippet.Invoke(nameof(ToString))); + return _utf8JsonWriterSnippet.WriteStringValue(value.Invoke(nameof(ToString))); - return _utf8JsonWriterSnippet.WriteNumberValue(enumerableSnippet.Invoke($"ToSerial{type.UnderlyingEnumType.Name}")); + return _utf8JsonWriterSnippet.WriteNumberValue(value.Invoke($"ToSerial{type.UnderlyingEnumType.Name}")); } else { if (type.UnderlyingEnumType.Equals(typeof(int))) // when the fixed enum is implemented as int, we cast to the value - return _utf8JsonWriterSnippet.WriteNumberValue(enumerableSnippet.CastTo(type.UnderlyingEnumType)); + return _utf8JsonWriterSnippet.WriteNumberValue(value.CastTo(type.UnderlyingEnumType)); if (type.UnderlyingEnumType.Equals(typeof(string))) - return _utf8JsonWriterSnippet.WriteStringValue(enumerableSnippet.Invoke($"ToSerial{type.UnderlyingEnumType.Name}")); + return _utf8JsonWriterSnippet.WriteStringValue(value.Invoke($"ToSerial{type.UnderlyingEnumType.Name}")); - return _utf8JsonWriterSnippet.WriteNumberValue(enumerableSnippet.Invoke($"ToSerial{type.UnderlyingEnumType.Name}")); + return _utf8JsonWriterSnippet.WriteNumberValue(value.Invoke($"ToSerial{type.UnderlyingEnumType.Name}")); } } - internal static MethodBodyStatement SerializeValueTypeCore( - CSharpType type, - SerializationFormat serializationFormat, - ValueExpression value, + internal static MethodBodyStatement SerializeJsonValueCore( Type valueType, + ValueExpression value, ScopedApi utf8JsonWriter, - ScopedApi mrwOptionsParameter) + ScopedApi mrwOptionsParameter, + SerializationFormat serializationFormat) { - if (valueType == typeof(Nullable<>)) - { - valueType = type.Arguments[0].FrameworkType; - } - - value = value.NullableStructValue(type); - return valueType switch { var t when t == typeof(JsonElement) => @@ -1656,7 +1651,7 @@ var t when ValueTypeIsNumber(t) => }; } - internal static ValueExpression GetValueTypeDeserializationExpressionCore( + internal static ValueExpression DeserializeJsonValueCore( Type valueType, ScopedApi element, SerializationFormat format) diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs index 3a63e2614b..15ceedfc85 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/ScmTypeFactory.cs @@ -118,16 +118,15 @@ public ClientProvider CreateClient(InputClient inputClient) return methods; } - public virtual ValueExpression GetValueTypeDeserializationExpression(Type valueType, ScopedApi element, SerializationFormat format) - => MrwSerializationTypeDefinition.GetValueTypeDeserializationExpressionCore(valueType, element, format); + public virtual ValueExpression DeserializeJsonValue(Type valueType, ScopedApi element, SerializationFormat format) + => MrwSerializationTypeDefinition.DeserializeJsonValueCore(valueType, element, format); - public virtual MethodBodyStatement SerializeValueType( - CSharpType type, - SerializationFormat serializationFormat, - ValueExpression value, + public virtual MethodBodyStatement SerializeJsonValue( Type valueType, + ValueExpression value, ScopedApi utf8JsonWriter, - ScopedApi mrwOptionsParameter) - => MrwSerializationTypeDefinition.SerializeValueTypeCore(type, serializationFormat, value, valueType, utf8JsonWriter, mrwOptionsParameter); + ScopedApi mrwOptionsParameter, + SerializationFormat serializationFormat) + => MrwSerializationTypeDefinition.SerializeJsonValueCore(valueType, value, utf8JsonWriter, mrwOptionsParameter, serializationFormat); } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs index 42689ac6d8..2c62912928 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs @@ -712,7 +712,7 @@ public void TestIntSerializationStatement( [TestCase(typeof(sbyte), SerializationFormat.Default, ExpectedResult = "foo.GetSByte()")] public string TestIntDeserializeExpression(Type type, SerializationFormat format) { - var expr = MrwSerializationTypeDefinition.GetValueTypeDeserializationExpressionCore(type, new ScopedApi(new VariableExpression(typeof(JsonElement), "foo")), format); + var expr = MrwSerializationTypeDefinition.DeserializeJsonValueCore(type, new ScopedApi(new VariableExpression(typeof(JsonElement), "foo")), format); return expr.ToDisplayString(); }