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

Mgmt rest client #47665

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
</ItemGroup>

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

<!--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Generator.CSharp.ClientModel;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using System.IO;

namespace Azure.Generator
{
// only apply for MPG
internal class AzureArmVisitor : ScmLibraryVisitor
{
/// <inheritdoc/>
protected override TypeProvider? Visit(TypeProvider type)
{
if (type is ClientProvider)
{
type.Update(modifiers: TransfromPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
}
// TODO: uncomment this once resources are generated
//if (type is RestClientProvider)
Copy link
Member Author

@live1206 live1206 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make both client and restclient internal, all the models referenced will be internalized during the postprocess.
Once we have the resource and collection in place, public reference will be in place and the referenced models will be kept public.
So, keep restclient public for now to make the referenced models public.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we put this comment as a comment in the code? just in case we could remember it and enable this piece in the future when we have the resource/collection types to keep those models public

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TODO

//{
// type.Update(modifiers: TransfromModifiers(type), relativeFilePath: TransformRelativeFilePathForRestClient(type));
//}
return type;
}

private static string TransformRelativeFilePathForClient(TypeProvider type)
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}RestOperations.cs");

private static string TransformRelativeFilePathForRestClient(TypeProvider type)
=> Path.Combine("src", "Generated", "RestOperations", $"{type.Name}.RestClient.cs");

private static TypeSignatureModifiers TransfromPublicModifiersToInternal(TypeProvider type)
{
var modifiers = type.DeclarationModifiers;
if (modifiers.HasFlag(TypeSignatureModifiers.Public))
{
modifiers &= ~TypeSignatureModifiers.Public;
modifiers |= TypeSignatureModifiers.Internal;
}

return modifiers;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public override void Configure()
AddMetadataReference(MetadataReference.CreateFromFile(typeof(Response).Assembly.Location));
var sharedSourceDirectory = Path.Combine(Path.GetDirectoryName(typeof(AzureClientPlugin).Assembly.Location)!, "Shared", "Core");
AddSharedSourceDirectory(sharedSourceDirectory);
if (IsAzureArm.Value)
{
AddVisitor(new AzureArmVisitor());
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Generator.InputTransformation;
using Azure.Generator.Primitives;
using Azure.Generator.Providers;
using Azure.Generator.Providers.Abstraction;
Expand All @@ -13,7 +14,6 @@
using Microsoft.Generator.CSharp.Statements;
using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;

namespace Azure.Generator
Expand Down Expand Up @@ -107,20 +107,7 @@ public override MethodBodyStatement SerializeJsonValue(Type valueType, ValueExpr
}

/// <inheritdoc/>
protected override ClientProvider CreateClientCore(InputClient inputClient) => base.CreateClientCore(TransformInputClient(inputClient));

private InputClient TransformInputClient(InputClient client)
{
var operationsToKeep = new List<InputOperation>();
foreach (var operation in client.Operations)
{
// operations_list has been covered in Azure.ResourceManager already, we don't need to generate it in the client
if (operation.CrossLanguageDefinitionId != "Azure.ResourceManager.Operations.list")
{
operationsToKeep.Add(operation);
}
}
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
}
protected override ClientProvider CreateClientCore(InputClient inputClient)
=> AzureClientPlugin.Instance.IsAzureArm.Value ? base.CreateClientCore(InputClientTransformer.TransformInputClient(inputClient)) : base.CreateClientCore(inputClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Generator.CSharp.Input;
using System;
using System.Collections.Generic;

namespace Azure.Generator.InputTransformation
{
internal static class InputClientTransformer
{
public static InputClient TransformInputClient(InputClient client)
{
var operationsToKeep = new List<InputOperation>();
foreach (var operation in client.Operations)
{
// operations_list has been covered in Azure.ResourceManager already, we don't need to generate it in the client
if (operation.CrossLanguageDefinitionId != "Azure.ResourceManager.Operations.list")
{
var transformedOperation = new InputOperation(operation.Name, operation.ResourceName, operation.Summary, operation.Doc, operation.Deprecated, operation.Accessibility, TransformInputOperationParameters(operation), operation.Responses, operation.HttpMethod, operation.RequestBodyMediaType, operation.Uri, operation.Path, operation.ExternalDocsUrl, operation.RequestMediaTypes, operation.BufferResponse, operation.LongRunning, operation.Paging, operation.GenerateProtocolMethod, operation.GenerateConvenienceMethod, operation.CrossLanguageDefinitionId);
operationsToKeep.Add(transformedOperation);
}
}
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
}

private static IReadOnlyList<InputParameter> TransformInputOperationParameters(InputOperation operation)
{
var parameters = new List<InputParameter>();
foreach (var parameter in operation.Parameters)
{
if (parameter.NameInRequest.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase))
{
// Always set subscriptionId to method parameter and type as string
parameters.Add(new InputParameter(parameter.Name, parameter.NameInRequest, parameter.Summary, parameter.Doc, InputPrimitiveType.String, parameter.Location, parameter.DefaultValue, InputOperationParameterKind.Method, parameter.IsRequired, parameter.IsApiVersion, parameter.IsResourceParameter, parameter.IsContentType, parameter.IsEndpoint, parameter.SkipUrlEncoding, parameter.Explode, parameter.ArraySerializationDelimiter, parameter.HeaderCollectionPrefix));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to change the type of subscriptionId back to string?
We do this in autorest.csharp because we want to minimize the changes to the old generator, since this is a new generator, we could cover the case that subscriptionId is a guid

Copy link
Member Author

@live1206 live1206 Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite following here, change subscriptionId type will break the public contract.

}
else
{
parameters.Add(parameter);
}
}
return parameters;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading