diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeProvider.cs index d4a66d59e8..2751c6a7c0 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/MrwSerializationTypeProvider.cs @@ -173,6 +173,7 @@ protected override MethodProvider[] BuildMethods() { methods.Add(BuildJsonModelWriteMethodObjectDeclaration()); methods.Add(BuildPersistableModelWriteMethodObjectDeclaration()); + methods.Add(BuildPersistableModelGetFormatFromOptionsObjectDeclaration()); } return [.. methods]; @@ -356,15 +357,32 @@ internal MethodProvider BuildPersistableModelCreateMethod() internal MethodProvider BuildPersistableModelGetFormatFromOptionsMethod() { ValueExpression jsonWireFormat = SystemSnippet.JsonFormatSerialization; - // ModelReaderWriterFormat IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) + // string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) return new MethodProvider ( - new MethodSignature(nameof(IPersistableModel.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, new[] { _serializationOptionsParameter }, ExplicitInterface: _persistableModelTInterface), + new MethodSignature(nameof(IPersistableModel.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, [_serializationOptionsParameter], ExplicitInterface: _persistableModelTInterface), jsonWireFormat, this ); } + /// + /// Builds the GetFormatFromOptions method for the model object. + /// + internal MethodProvider BuildPersistableModelGetFormatFromOptionsObjectDeclaration() + { + ValueExpression jsonWireFormat = SystemSnippet.JsonFormatSerialization; + var castToT = This.CastTo(_persistableModelTInterface); + + // string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => ((IPersistableModel)this).GetFormatFromOptions(options); + return new MethodProvider + ( + new MethodSignature(nameof(IPersistableModel.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, [_serializationOptionsParameter], ExplicitInterface: _persistableModelObjectInterface), + castToT.Invoke(nameof(IPersistableModel.GetFormatFromOptions), [_serializationOptionsParameter]), + this + ); + } + /// /// Builds the serialization constructor for the model. /// diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeProviderTests.cs index 9b6957e874..a9e9d204dc 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/MrwSerializationTypeProviderTests.cs @@ -104,7 +104,7 @@ public void TestBuildJsonModelWriteCoreMethod() // This test validates the json model serialization write method object declaration is built correctly [Test] - public void BuildJsonModelWriteMethodObjectDeclaration() + public void TestBuildJsonModelWriteMethodObjectDeclaration() { var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); var mockModelTypeProvider = new ModelProvider(inputModel); @@ -217,7 +217,7 @@ public void TestBuildPersistableModelWriteCoreMethod() // This test validates the PersistableModel serialization write method object declaration is built correctly [Test] - public void BuildPersistableModelWriteMethodObjectDeclaration() + public void TestBuildPersistableModelWriteMethodObjectDeclaration() { var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); var mockModelTypeProvider = new ModelProvider(inputModel); @@ -271,7 +271,7 @@ public void TestBuildPersistableModelDeserializationMethod() Assert.AreEqual(expectedReturnType, methodSignature?.ReturnType); } - // This test validates the I model get format method is built correctly + // This test validates the persistable model get format method is built correctly [Test] public void TestBuildPersistableModelGetFormatMethod() { @@ -295,6 +295,41 @@ public void TestBuildPersistableModelGetFormatMethod() Assert.IsNotNull(methodBody); } + // This test validates the persistable model get format method object declaration is built correctly + [Test] + public void TestBuildPersistableModelGetFormatMethodObjectDeclaration() + { + var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, Array.Empty(), null, new List(), null, null, new Dictionary(), null, true); + var mockModelTypeProvider = new ModelProvider(inputModel); + var jsonMrwSerializationTypeProvider = new MrwSerializationTypeProvider(mockModelTypeProvider, inputModel); + var method = jsonMrwSerializationTypeProvider.BuildPersistableModelGetFormatFromOptionsObjectDeclaration(); + + Assert.IsNotNull(method); + + var expectedInterface = new CSharpType(typeof(IPersistableModel)); + var methodSignature = method?.Signature as MethodSignature; + Assert.IsNotNull(methodSignature); + Assert.AreEqual("GetFormatFromOptions", methodSignature?.Name); + Assert.AreEqual(expectedInterface, methodSignature?.ExplicitInterface); + Assert.AreEqual(1, methodSignature?.Parameters.Count); + var expectedReturnType = new CSharpType(typeof(string)); + Assert.AreEqual(expectedReturnType, methodSignature?.ReturnType); + + // Check method modifiers + var expectedModifiers = MethodSignatureModifiers.None; + Assert.AreEqual(expectedModifiers, methodSignature?.Modifiers, "Method modifiers do not match the expected value."); + + + // Validate body + var methodBody = method?.BodyStatements; + Assert.IsNull(methodBody); + var bodyExpression = method?.BodyExpression as InvokeInstanceMethodExpression; + Assert.IsNotNull(bodyExpression); + Assert.AreEqual("GetFormatFromOptions", bodyExpression?.MethodName); + Assert.IsNotNull(bodyExpression?.InstanceReference); + Assert.AreEqual(1, bodyExpression?.Arguments.Count); + } + [Test] public void TestBuildSerializationConstructor() {