-
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.
Construct correct Namespace for Classifier2xxAnd4xxDefinition (#4625)
This PR fixes an issue when a client's namespace is customized via the `[CodeGenClient("ClientName")]` attribute and the private type `Classifier2xxAnd4xx` within a client is still generated using the default root namespace leading to compilation issues. fixes: #4619
- Loading branch information
1 parent
258b327
commit 2ba6c7a
Showing
7 changed files
with
174 additions
and
4 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
48 changes: 48 additions & 0 deletions
48
...ClientProviders/TestData/RestClientProviderCustomizationTests/CanChangeClientNamespace.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,48 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel.Primitives; | ||
|
||
namespace Sample.Custom | ||
{ | ||
/// <summary></summary> | ||
public partial class TestClient | ||
{ | ||
private static global::System.ClientModel.Primitives.PipelineMessageClassifier _pipelineMessageClassifier200; | ||
private static global::System.ClientModel.Primitives.PipelineMessageClassifier _pipelineMessageClassifier204; | ||
private static global::Sample.Custom.TestClient.Classifier2xxAnd4xx _pipelineMessageClassifier2xxAnd4xx; | ||
|
||
private static global::System.ClientModel.Primitives.PipelineMessageClassifier PipelineMessageClassifier200 => _pipelineMessageClassifier200 = global::System.ClientModel.Primitives.PipelineMessageClassifier.Create(stackalloc ushort[] { 200 }); | ||
|
||
private static global::System.ClientModel.Primitives.PipelineMessageClassifier PipelineMessageClassifier204 => _pipelineMessageClassifier204 = global::System.ClientModel.Primitives.PipelineMessageClassifier.Create(stackalloc ushort[] { 204 }); | ||
|
||
private static global::Sample.Custom.TestClient.Classifier2xxAnd4xx PipelineMessageClassifier2xxAnd4xx => _pipelineMessageClassifier2xxAnd4xx ??= new global::Sample.Custom.TestClient.Classifier2xxAnd4xx(); | ||
|
||
private class Classifier2xxAnd4xx : global::System.ClientModel.Primitives.PipelineMessageClassifier | ||
{ | ||
public override bool TryClassify(global::System.ClientModel.Primitives.PipelineMessage message, out bool isError) | ||
{ | ||
isError = false; | ||
if ((message.Response == null)) | ||
{ | ||
return false; | ||
} | ||
isError = message.Response.Status switch | ||
{ | ||
((>= 200) and (< 300)) => false, | ||
((>= 400) and (< 500)) => false, | ||
_ => true | ||
}; | ||
return true; | ||
} | ||
|
||
public override bool TryClassify(global::System.ClientModel.Primitives.PipelineMessage message, global::System.Exception exception, out bool isRetryable) | ||
{ | ||
isRetryable = false; | ||
return false; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...RestClientProviderCustomizationTests/CanChangeClientNamespace/CanChangeClientNamespace.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,11 @@ | ||
#nullable disable | ||
|
||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Threading.Tasks; | ||
using Microsoft.Generator.CSharp.Customization; | ||
|
||
namespace Sample.Custom; | ||
|
||
[CodeGenClient("TestClient")] | ||
public partial class TestClient { } |
61 changes: 61 additions & 0 deletions
61
...rator.CSharp.ClientModel/test/Providers/Definitions/Classifier2xxAnd4xxDefinitionTests.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,61 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
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 Classifier2xxAnd4xxDefinitionTests | ||
{ | ||
[TestCaseSource(nameof(GetTypeNamespaceTestCases))] | ||
public void TestGetTypeNamespace(string mockJson) | ||
{ | ||
MockHelpers.LoadMockPlugin(configuration: mockJson); | ||
var inputClient = InputFactory.Client("TestClient"); | ||
var restClientProvider = new ClientProvider(inputClient).RestClient; | ||
Assert.IsNotNull(restClientProvider); | ||
|
||
var classifier2xxAnd4xxDefinition = new Classifier2xxAnd4xxDefinition(restClientProvider); | ||
var result = classifier2xxAnd4xxDefinition.Type.Namespace; | ||
|
||
Assert.AreEqual(restClientProvider.Type.Namespace, result); | ||
} | ||
|
||
[Test] | ||
public async Task TestGetTypeCustomNamespace() | ||
{ | ||
var inputClient = InputFactory.Client("TestClient"); | ||
var plugin = await MockHelpers.LoadMockPluginAsync( | ||
clients: () => [inputClient], | ||
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
// Find the rest client provider | ||
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider); | ||
Assert.IsNotNull(clientProvider); | ||
var restClientProvider = (clientProvider as ClientProvider)!.RestClient; | ||
Assert.IsNotNull(restClientProvider); | ||
|
||
var classifier2xxAnd4xxDefinition = new Classifier2xxAnd4xxDefinition(restClientProvider!); | ||
var result = classifier2xxAnd4xxDefinition.Type.Namespace; | ||
|
||
Assert.AreEqual(restClientProvider!.Type.Namespace, result); | ||
} | ||
|
||
public static IEnumerable<TestCaseData> GetTypeNamespaceTestCases | ||
{ | ||
get | ||
{ | ||
yield return new TestCaseData(@"{ | ||
""output-folder"": ""outputFolder"", | ||
""library-name"": ""libraryName"", | ||
""namespace"": ""testNamespace"" | ||
}"); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...assifier2xxAnd4xxDefinitionTests/TestGetTypeCustomNamespace/TestGetTypeCustomNamespace.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,11 @@ | ||
#nullable disable | ||
|
||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Threading.Tasks; | ||
using Microsoft.Generator.CSharp.Customization; | ||
|
||
namespace Sample.Custom; | ||
|
||
[CodeGenClient("TestClient")] | ||
public partial class TestClient { } |
34 changes: 34 additions & 0 deletions
34
...osoft.Generator.CSharp.ClientModel/test/Providers/RestClientProviderCustomizationTests.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,34 @@ | ||
// 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.Primitives; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.ClientProviders | ||
{ | ||
public class RestClientProviderCustomizationTests | ||
{ | ||
// Validates the client is generated using the correct namespace | ||
[Test] | ||
public async Task CanChangeClientNamespace() | ||
{ | ||
var inputClient = InputFactory.Client("TestClient"); | ||
var plugin = await MockHelpers.LoadMockPluginAsync( | ||
clients: () => [inputClient], | ||
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider); | ||
Assert.IsNotNull(clientProvider); | ||
var restClientProvider = (clientProvider as ClientProvider)!.RestClient; | ||
Assert.IsNotNull(restClientProvider); | ||
|
||
var writer = new TypeProviderWriter(restClientProvider); | ||
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