-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds support for replacing generated methods with custom code. It also fixes several issues with constructing nullable types from custom code. fixes: #4472
- Loading branch information
1 parent
f549f60
commit 7e5e3a6
Showing
20 changed files
with
742 additions
and
60 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
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
22 changes: 22 additions & 0 deletions
22
...viders/TestData/ClientProviderCustomizationTests/CanReplaceOpMethod/CanReplaceOpMethod.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,22 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary></summary> | ||
public partial class TestClient | ||
{ | ||
public virtual ClientResult HelloAgain(BinaryContent content, RequestOptions options) | ||
{ | ||
|
||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...estData/ClientProviderCustomizationTests/CanReplaceStructMethod/CanReplaceStructMethod.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,22 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary></summary> | ||
public partial class TestClient | ||
{ | ||
public virtual ClientResult HelloAgain(MyStruct? p1) | ||
{ | ||
|
||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...p.ClientModel/test/Providers/Definitions/ClientPipelineExtensionsDefCustomizationTests.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,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.Definitions | ||
{ | ||
public class ClientPipelineExtensionsDefCustomizationTests | ||
{ | ||
[Test] | ||
public async Task CanReplaceMethod() | ||
{ | ||
var plugin = await MockHelpers.LoadMockPluginAsync(compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
// Find the extension definition | ||
var definition = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientPipelineExtensionsDefinition); | ||
Assert.IsNotNull(definition); | ||
|
||
// The definitions should not have the custom method | ||
var definitionMethods = definition!.Methods; | ||
Assert.AreEqual(3, definitionMethods.Count); | ||
Assert.IsFalse(definitionMethods.Any(m => m.Signature.Name == "ProcessMessageAsync")); | ||
|
||
// The custom code view should contain the method | ||
var customCodeView = definition.CustomCodeView; | ||
Assert.IsNotNull(customCodeView); | ||
var customMethods = customCodeView!.Methods; | ||
Assert.AreEqual(1, customMethods.Count); | ||
Assert.AreEqual("ProcessMessageAsync", customMethods[0].Signature.Name); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...stData/ClientPipelineExtensionsDefCustomizationTests/CanReplaceMethod/CanReplaceMethod.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,25 @@ | ||
#nullable disable | ||
|
||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
internal static partial class ClientPipelineExtensions | ||
{ | ||
public static async ValueTask<PipelineResponse> ProcessMessageAsync(this ClientPipeline pipeline, PipelineMessage message, RequestOptions options) | ||
{ | ||
await pipeline.SendAsync(message).ConfigureAwait(false); | ||
|
||
if (message.Response.IsError && (options?.ErrorOptions & ClientErrorBehaviors.NoThrow) != ClientErrorBehaviors.NoThrow) | ||
{ | ||
// log instead of throw | ||
Console.WriteLine("Error: " + message.Response); | ||
} | ||
|
||
PipelineResponse response = message.BufferResponse ? message.Response : message.ExtractResponse(); | ||
return response; | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
.../EnumProvider/TestData/SerializationCustomizationTests/CanReplaceMethod/MockInputModel.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,16 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
|
||
namespace Sample.Models | ||
{ | ||
internal static partial class MockInputModelExtensions | ||
{ | ||
public static string ToSerialString(this MockInputModel value) => value switch | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown MockInputModel value.") | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.