Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest MGC and set namespace of model and enum for Azure #48197

Merged
merged 13 commits into from
Feb 13, 2025
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
</ItemGroup>

<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250205.1" />
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250209.1" />
</ItemGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static class InputClientTransformer
// There is no need to check sub-clients or custom code since it is specific to handle the above removing
if (operationsToKeep.Count == 0) return null;

return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
return new InputClient(client.Name, client.Namespace, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
}

private static IReadOnlyList<InputParameter> TransformInputOperationParameters(InputOperation operation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ protected override string GetSourceProjectFileContent()
{
var builder = new CSharpProjectWriter()
{
Description = $"This is the {AzureClientPlugin.Instance.Configuration.RootNamespace} client library for developing .NET applications with rich experience.",
AssemblyTitle = $"SDK Code Generation {AzureClientPlugin.Instance.Configuration.RootNamespace}",
Description = $"This is the {AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name} client library for developing .NET applications with rich experience.",
AssemblyTitle = $"SDK Code Generation {AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name}",
Version = "1.0.0-beta.1",
PackageTags = AzureClientPlugin.Instance.Configuration.RootNamespace,
PackageTags = AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name,
GenerateDocumentationFile = true,
};

Expand Down Expand Up @@ -162,7 +162,7 @@ protected override string GetSolutionFileContent()
EndGlobalSection
EndGlobal
";
return string.Format(slnContent, AzureClientPlugin.Instance.Configuration.RootNamespace);
return string.Format(slnContent, AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private class Template<T> { }
public MgmtLongRunningOperationProvider(bool isGeneric)
{
_isGeneric = isGeneric;
_operationField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, isGeneric ? new CSharpType(typeof(OperationInternal), _t) : typeof(OperationInternal), "_operation", this);
_operationField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, isGeneric ? new CSharpType(typeof(OperationInternal<>), _t) : typeof(OperationInternal), "_operation", this);
_rehydrationTokenField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, new CSharpType(typeof(RehydrationToken), true), "_completeRehydrationToken", this);
_nextLinkOperationField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, new CSharpType(typeof(NextLinkOperationImplementation), true), "_nextLinkOperation", this);
_operationIdField = new FieldProvider(FieldModifiers.Private | FieldModifiers.ReadOnly, typeof(string), "_operationId", this);
Expand All @@ -52,7 +52,7 @@ protected override string BuildRelativeFilePath()
string GetFileName() => _isGeneric ? $"{_serviceName}ArmOperationOfT.cs" : $"{Name}.cs";
}

protected override CSharpType[] BuildImplements() => [_isGeneric ? new CSharpType(typeof(ArmOperation), _t) : typeof(ArmOperation)];
protected override CSharpType[] BuildImplements() => [_isGeneric ? new CSharpType(typeof(ArmOperation<>), _t) : typeof(ArmOperation)];

protected override FieldProvider[] BuildFields()
=> [_operationField, _rehydrationTokenField, _nextLinkOperationField, _operationIdField];
Expand All @@ -75,13 +75,13 @@ protected override MethodProvider[] BuildMethods()
this, XmlDocProvider.InheritDocs);

var updateStatusAsyncMethod = new MethodProvider(
new MethodSignature("UpdateStatusAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask), typeof(Response)), null, [KnownParameters.CancellationTokenParameter]),
new MethodSignature("UpdateStatusAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask<>), typeof(Response)), null, [KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("UpdateStatusAsync", [KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs);

var waitForCompletionResponseMethod = _isGeneric
? new MethodProvider(
new MethodSignature("WaitForCompletion", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(Response), _t), null, [KnownParameters.CancellationTokenParameter]),
new MethodSignature("WaitForCompletion", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(Response<>), _t), null, [KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("WaitForCompletion", [KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs)
: new MethodProvider(
Expand All @@ -92,7 +92,7 @@ protected override MethodProvider[] BuildMethods()
var timeSpanParameter = new ParameterProvider("pollingInterval", $"The interval between pollings", typeof(TimeSpan));
var waitForCompletionResponseWithPolingMethod = _isGeneric
? new MethodProvider(
new MethodSignature("WaitForCompletion", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(Response), _t), null, [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
new MethodSignature("WaitForCompletion", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(Response<>), _t), null, [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("WaitForCompletion", [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs)
: new MethodProvider(
Expand All @@ -102,21 +102,21 @@ protected override MethodProvider[] BuildMethods()

var waitForCompletionResponseAsyncMethod = _isGeneric
? new MethodProvider(
new MethodSignature("WaitForCompletionAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask), new CSharpType(typeof(Response), _t)), null, [KnownParameters.CancellationTokenParameter]),
new MethodSignature("WaitForCompletionAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask<>), new CSharpType(typeof(Response<>), _t)), null, [KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("WaitForCompletionAsync", [KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs)
: new MethodProvider(
new MethodSignature("WaitForCompletionResponseAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask), typeof(Response)), null, [KnownParameters.CancellationTokenParameter]),
new MethodSignature("WaitForCompletionResponseAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask<>), typeof(Response)), null, [KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("WaitForCompletionResponseAsync", [KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs);

var waitForCompletionResponseWithPollingAsyncMethod = _isGeneric
? new MethodProvider(
new MethodSignature("WaitForCompletionAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask), new CSharpType(typeof(Response), _t)), null, [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
new MethodSignature("WaitForCompletionAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask<>), new CSharpType(typeof(Response<>), _t)), null, [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("WaitForCompletionAsync", [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs)
: new MethodProvider(
new MethodSignature("WaitForCompletionResponseAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask), typeof(Response)), null, [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
new MethodSignature("WaitForCompletionResponseAsync", null, MethodSignatureModifiers.Public | MethodSignatureModifiers.Override, new CSharpType(typeof(ValueTask<>), typeof(Response)), null, [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
_operationField.Invoke("WaitForCompletionResponseAsync", [timeSpanParameter, KnownParameters.CancellationTokenParameter]),
this, XmlDocProvider.InheritDocs);

Expand Down Expand Up @@ -192,21 +192,21 @@ private ConstructorProvider BuildInitializationConstructor()
_operationIdField.Assign(This.Invoke(GetOperationIdName, _rehydrationTokenField)).Terminate(),
])),
_operationField.Assign(_isGeneric
? New.Instance(new CSharpType(typeof(OperationInternal), _t), [Static<NextLinkOperationImplementation>().Invoke("Create", sourceParameter, nextLinkOperationVariable), clientDiagnosticsParameter, responseParameter, Literal(Name), Null, New.Instance(typeof(SequentialDelayStrategy))])
? New.Instance(new CSharpType(typeof(OperationInternal<>), _t), [Static<NextLinkOperationImplementation>().Invoke("Create", sourceParameter, nextLinkOperationVariable), clientDiagnosticsParameter, responseParameter, Literal(Name), Null, New.Instance(typeof(SequentialDelayStrategy))])
: New.Instance(typeof(OperationInternal), [nextLinkOperationVariable, clientDiagnosticsParameter, responseParameter, Literal(Name), Null, New.Instance(typeof(SequentialDelayStrategy))])).Terminate(),
};
return new ConstructorProvider(signature, body, this);
}

private ConstructorProvider BuildRehydrationConstructor()
{
var responseParameter = new ParameterProvider("response", $"The operation response", _isGeneric ? new CSharpType(typeof(Response), _t) : typeof(Response));
var responseParameter = new ParameterProvider("response", $"The operation response", _isGeneric ? new CSharpType(typeof(Response<>), _t) : typeof(Response));
var rehydrationTokenParameter = new ParameterProvider("rehydrationToken", $"The token to rehydrate the operation", new CSharpType(typeof(RehydrationToken), true), Null);
var signature = new ConstructorSignature(Type, $"", MethodSignatureModifiers.Internal, [responseParameter, rehydrationTokenParameter], null);
var body = new MethodBodyStatement[]
{
_operationField.Assign(_isGeneric
? Static(new CSharpType(typeof(OperationInternal), _t)).Invoke(nameof(OperationInternal.Succeeded), [responseParameter.Invoke("GetRawResponse"), responseParameter.Property("Value")])
? Static(new CSharpType(typeof(OperationInternal<>), _t)).Invoke(nameof(OperationInternal.Succeeded), [responseParameter.Invoke("GetRawResponse"), responseParameter.Property("Value")])
: Static(typeof(OperationInternal)).Invoke(nameof(OperationInternal.Succeeded), [responseParameter])
).Terminate(),
_rehydrationTokenField.Assign(rehydrationTokenParameter).Terminate(),
Expand All @@ -232,4 +232,4 @@ protected override PropertyProvider[] BuildProperties()

protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static bool IsResourceModel(InputModelType inputModelType)
return true;
}

switch (property.SerializedName)
switch (property.Name)
{
case "id":
if (property.Type.GetImplementType() is InputPrimitiveType { Kind: InputPrimitiveTypeKind.String } inputPrimitiveType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ public static InputEnumType Enum(
string access = "public",
InputModelTypeUsage usage = InputModelTypeUsage.Output | InputModelTypeUsage.Input,
IEnumerable<InputEnumTypeValue>? values = null,
bool isExtensible = false)
bool isExtensible = false,
string clientNamespace = "Sample.Models")
{
return new InputEnumType(
name,
clientNamespace,
name,
access,
null,
Expand All @@ -148,17 +150,18 @@ public static InputModelProperty Property(
{
return new InputModelProperty(
name,
wireName ?? name,
summary,
description ?? $"Description for {name}",
type,
isRequired,
isReadOnly,
isDiscriminator);
isDiscriminator,
new(json: new(wireName ?? name)));
}

public static InputModelType Model(
string name,
string clientNamespace = "Sample.Models",
string access = "public",
InputModelTypeUsage usage = InputModelTypeUsage.Output | InputModelTypeUsage.Input | InputModelTypeUsage.Json,
IEnumerable<InputModelProperty>? properties = null,
Expand All @@ -172,6 +175,7 @@ public static InputModelType Model(
IEnumerable<InputModelProperty> propertiesList = properties ?? [Property("StringProperty", InputPrimitiveType.String)];
return new InputModelType(
name,
clientNamespace,
name,
access,
null,
Expand All @@ -185,7 +189,8 @@ public static InputModelType Model(
propertiesList.FirstOrDefault(p => p.IsDiscriminator),
discriminatedModels is null ? new Dictionary<string, InputModelType>() : discriminatedModels.AsReadOnly(),
additionalProperties,
modelAsStruct);
modelAsStruct,
new());
}

public static InputType Array(InputType elementType)
Expand Down Expand Up @@ -244,12 +249,13 @@ public static OperationResponse OperationResponse(IEnumerable<int>? statusCodes
["application/json"]);
}

public static InputClient Client(string name, IEnumerable<InputOperation>? operations = null, IEnumerable<InputParameter>? parameters = null, string? parent = null)
public static InputClient Client(string name, string clientNamespace = "Sample", string? doc = null, IEnumerable<InputOperation>? operations = null, IEnumerable<InputParameter>? parameters = null, string? parent = null)
{
return new InputClient(
name,
null,
$"{name} description",
clientNamespace,
string.Empty,
doc ?? $"{name} description",
operations is null ? [] : [.. operations],
parameters is null ? [] : [.. parameters],
parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void ValidateClientResponseExceptionTypeIsOverridden()

private static ClientProvider CreateMockClientProvider()
{
var client = InputFactory.Client("TestClient", [InputFactory.Operation("foo")]);
var client = InputFactory.Client("TestClient", operations: [InputFactory.Operation("foo")]);
MockHelpers.LoadMockPlugin(clientResponseApi: AzureClientResponseProvider.Instance);
var clientProvider = AzureClientPlugin.Instance.TypeFactory.CreateClient(client)!;
return clientProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ internal class ClientProviderTests
private const string KeyAuthCategory = "WithKeyAuth";
private const string OAuth2Category = "WithOAuth2";
private const string TestClientName = "TestClient";
private static readonly InputClient _animalClient = new("animal", null, "AnimalClient description", [], [], TestClientName);
private static readonly InputClient _dogClient = new("dog", null, "DogClient description", [], [], _animalClient.Name);
private static readonly InputClient _huskyClient = new("husky", null, "HuskyClient description", [], [], _dogClient.Name);
private static readonly InputClient _animalClient = new("animal", "Samples", "AnimalClient description", null, [], [], TestClientName);
private static readonly InputClient _dogClient = new("dog", "Samples", "DogClient description", null, [], [], _animalClient.Name);
private static readonly InputClient _huskyClient = new("husky", "Samples", "HuskyClient description", null, [], [], _dogClient.Name);

private bool _containsSubClients;
private bool _hasKeyAuth;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
global::sample.namespace.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Samples.Argument.AssertNotNull(endpoint, nameof(endpoint));

options ??= new global::sample.namespace.TestClientOptions();
options ??= new global::Samples.TestClientOptions();

_endpoint = endpoint;
Pipeline = global::Azure.Core.Pipeline.HttpPipelineBuilder.Build(options, Array.Empty<global::Azure.Core.Pipeline.HttpPipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global::sample.namespace.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::sample.namespace.Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));
global::Samples.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Samples.Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

options ??= new global::sample.namespace.TestClientOptions();
options ??= new global::Samples.TestClientOptions();

_endpoint = endpoint;
_tokenCredential = tokenCredential;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global::sample.namespace.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::sample.namespace.Argument.AssertNotNull(keyCredential, nameof(keyCredential));
global::Samples.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Samples.Argument.AssertNotNull(keyCredential, nameof(keyCredential));

options ??= new global::sample.namespace.TestClientOptions();
options ??= new global::Samples.TestClientOptions();

_endpoint = endpoint;
_keyCredential = keyCredential;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global::sample.namespace.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::sample.namespace.Argument.AssertNotNull(keyCredential, nameof(keyCredential));
global::Samples.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Samples.Argument.AssertNotNull(keyCredential, nameof(keyCredential));

options ??= new global::sample.namespace.TestClientOptions();
options ??= new global::Samples.TestClientOptions();

_endpoint = endpoint;
_keyCredential = keyCredential;
Expand Down
Loading
Loading