Skip to content

Commit f5fbe0a

Browse files
Fix bug in operator writing (#7136)
Contributes to Azure/azure-sdk-for-net#49450
1 parent 5848367 commit f5fbe0a

29 files changed

+127
-36
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,19 @@ private MethodProvider BuildExplicitFromClientResult()
191191
deserialize
192192
};
193193
return new MethodProvider(
194-
new MethodSignature(Type.Name, null, modifiers, null, null, [result]),
194+
new MethodSignature(Type.Name, null, modifiers, Type, null, [result]),
195195
methodBody,
196196
this);
197197
}
198198

199199
private MethodProvider BuildImplicitToBinaryContent()
200200
{
201-
var model = new ParameterProvider(Type.Name.ToVariableName(), $"The {Type:C} to serialize into {ScmCodeModelGenerator.Instance.TypeFactory.RequestContentApi.RequestContentType:C}", Type);
201+
var requestContentType = ScmCodeModelGenerator.Instance.TypeFactory.RequestContentApi.RequestContentType;
202+
var model = new ParameterProvider(Type.Name.ToVariableName(), $"The {Type:C} to serialize into {requestContentType:C}", Type);
202203
var modifiers = MethodSignatureModifiers.Public | MethodSignatureModifiers.Static | MethodSignatureModifiers.Implicit | MethodSignatureModifiers.Operator;
203204
// return BinaryContent.Create(model, ModelSerializationExtensions.WireOptions);
204205
return new MethodProvider(
205-
new MethodSignature(ScmCodeModelGenerator.Instance.TypeFactory.RequestContentApi.RequestContentType.FrameworkType.Name, null, modifiers, null, null, [model]),
206+
new MethodSignature(ScmCodeModelGenerator.Instance.TypeFactory.RequestContentApi.RequestContentType.FrameworkType.Name, null, modifiers, requestContentType, null, [model]),
206207
new MethodBodyStatement[]
207208
{
208209
!_isStruct ? new IfStatement(model.Equal(Null)) { Return(Null) } : MethodBodyStatement.Empty,

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/MrwSerializationTypeDefinitionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ public void TestBuildImplicitToBinaryContent(bool useStruct)
647647

648648
var methodParameters = methodSignature?.Parameters;
649649
Assert.AreEqual(1, methodParameters?.Count);
650-
Assert.IsNull(methodSignature?.ReturnType);
650+
Assert.IsTrue(methodSignature?.ReturnType!.Equals(typeof(BinaryContent)));
651651

652652
var methodBody = method?.BodyStatements;
653653
Assert.IsNotNull(methodBody);
@@ -678,7 +678,7 @@ public void TestBuildExplicitFromClientResult()
678678
Assert.AreEqual(1, methodParameters?.Count);
679679
var clientResultParameter = methodParameters?[0];
680680
Assert.AreEqual(new CSharpType(typeof(ClientResult)), clientResultParameter?.Type);
681-
Assert.IsNull(methodSignature?.ReturnType);
681+
Assert.IsTrue(methodSignature?.ReturnType!.Equals(model.Type));
682682

683683
var methodBody = method?.BodyStatements;
684684
Assert.IsNotNull(methodBody);

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/DiscriminatorTests/TestNestedDiscriminatedModelWithOwnDiscriminator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected override void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrit
107107
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.Tree>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
108108

109109
/// <param name="tree"> The <see cref="global::Sample.Models.Tree"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
110-
public static implicit operator BinaryContent(global::Sample.Models.Tree tree)
110+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.Tree tree)
111111
{
112112
if ((tree == null))
113113
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizeEnumToFieldFrameworkType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
131131
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
132132

133133
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
134-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
134+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
135135
{
136136
if ((mockInputModel == null))
137137
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizeEnumToFrameworkType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
131131
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
132132

133133
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
134-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
134+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
135135
{
136136
if ((mockInputModel == null))
137137
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizeFixedEnumString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
128128
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
129129

130130
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
131-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
131+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
132132
{
133133
if ((mockInputModel == null))
134134
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizeNullableStringToFixedEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
128128
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
129129

130130
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
131-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
131+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
132132
{
133133
if ((mockInputModel == null))
134134
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizePropertyIntoReadOnlyMemory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
140140
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
141141

142142
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
143-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
143+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
144144
{
145145
if ((mockInputModel == null))
146146
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizeStringToFixedEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
128128
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
129129

130130
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
131-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
131+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
132132
{
133133
if ((mockInputModel == null))
134134
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/ModelCustomizationTests/CanCustomizeUriProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
131131
string global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.MockInputModel>.GetFormatFromOptions(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => "J";
132132

133133
/// <param name="mockInputModel"> The <see cref="global::Sample.Models.MockInputModel"/> to serialize into <see cref="global::System.ClientModel.BinaryContent"/>. </param>
134-
public static implicit operator BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
134+
public static implicit operator global::System.ClientModel.BinaryContent(global::Sample.Models.MockInputModel mockInputModel)
135135
{
136136
if ((mockInputModel == null))
137137
{

0 commit comments

Comments
 (0)