Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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.

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.

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.

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.

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.

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.

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Output.Models.Requests;
using AutoRest.CSharp.Output.Models.Types;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
Expand All @@ -16,7 +17,7 @@ namespace AutoRest.CSharp.Generation.Writers
{
internal class LongRunningOperationWriter
{
public void Write(CodeWriter writer, LongRunningOperation operation)
public void Write(CodeWriter writer, LongRunningOperation operation, BuildContext? context = null)
{
var responseVariable = "response";
var pagingResponse = operation.PagingResponse;
Expand All @@ -38,7 +39,7 @@ public void Write(CodeWriter writer, LongRunningOperation operation)

using (writer.Scope())
{
WriteFields(writer, operation, pagingResponse, helperType);
WriteFields(writer, operation, pagingResponse, helperType, context);

writer.Line();
writer.WriteXmlDocumentationSummary($"Initializes a new instance of {cs.Name} for mocking.");
Expand All @@ -48,7 +49,7 @@ public void Write(CodeWriter writer, LongRunningOperation operation)

writer.Line();

WriteConstructor(writer, operation, pagingResponse, cs, helperType);
WriteConstructor(writer, operation, pagingResponse, cs, helperType, context);
writer.Line();

writer.WriteXmlDocumentationInheritDoc();
Expand Down Expand Up @@ -132,7 +133,7 @@ protected virtual CSharpType GetHelperType(LongRunningOperation operation)
return operation.ResultType != null ? new CSharpType(typeof(OperationInternals<>), operation.ResultType) : new CSharpType(typeof(OperationInternals));
}

protected virtual void WriteFields(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType helperType)
protected virtual void WriteFields(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType helperType, BuildContext? context = null)
{
writer.Line($"private readonly {helperType} _operation;");

Expand All @@ -142,7 +143,7 @@ protected virtual void WriteFields(CodeWriter writer, LongRunningOperation opera
}
}

protected virtual void WriteConstructor(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType lroType, CSharpType helperType)
protected virtual void WriteConstructor(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType lroType, CSharpType helperType, BuildContext? context = null)
{
writer.Append($"internal {lroType.Name}({typeof(ClientDiagnostics)} clientDiagnostics, {typeof(HttpPipeline)} pipeline, {typeof(Request)} request, {typeof(Response)} response");

Expand Down
4 changes: 2 additions & 2 deletions src/AutoRest.CSharp/Mgmt/AutoRest/MgmtTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ public static void Execute(GeneratedCodeWorkspace project, CodeModel codeModel,
foreach (var operation in context.Library.LongRunningOperations)
{
var codeWriter = new CodeWriter();
mgmtLongRunningOperationWriter.Write(codeWriter, operation);
mgmtLongRunningOperationWriter.Write(codeWriter, operation, context);

project.AddGeneratedFile($"{operation.Type.Name}.cs", codeWriter.ToString());
}

foreach (var operation in context.Library.NonLongRunningOperations)
{
var codeWriter = new CodeWriter();
NonLongRunningOperationWriter.Write(codeWriter, operation);
NonLongRunningOperationWriter.Write(codeWriter, operation, context);

project.AddGeneratedFile($"{operation.Type.Name}.cs", codeWriter.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using System.Threading.Tasks;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Generation.Writers;
using AutoRest.CSharp.Mgmt.Decorator;
using AutoRest.CSharp.Mgmt.Output;
using AutoRest.CSharp.Output.Models.Requests;
using AutoRest.CSharp.Output.Models.Types;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
Expand Down Expand Up @@ -45,27 +47,33 @@ protected override CSharpType GetValueTaskType(LongRunningOperation operation)
return mgmtOperation.WrapperType != null ? new CSharpType(typeof(Response<>), mgmtOperation.WrapperType) : base.GetValueTaskType(operation);
}

protected override void WriteFields(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType helperType)
protected override void WriteFields(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType helperType, BuildContext? context)
{
Debug.Assert(context != null);
base.WriteFields(writer, operation, pagingResponse, helperType);

MgmtLongRunningOperation mgmtOperation = AsMgmtOperation(operation);

if (mgmtOperation.WrapperType != null)
{
writer.Line();
writer.Line($"private readonly {typeof(ResourceOperationsBase)} {_operationBaseField};");
var isTenantResource = (operation.ResultType?.Implementation as MgmtObjectType)?.OperationGroup?.IsTenantResource(context.Configuration.MgmtConfiguration) == true;
var optionType = isTenantResource ? typeof(OperationsBase) : typeof(ResourceOperationsBase);
writer.Line($"private readonly {optionType} {_operationBaseField};");
}
}

protected override void WriteConstructor(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType cs, CSharpType helperType)
protected override void WriteConstructor(CodeWriter writer, LongRunningOperation operation, PagingResponseInfo? pagingResponse, CSharpType cs, CSharpType helperType, BuildContext? context)
{
Debug.Assert(context != null);
MgmtLongRunningOperation mgmtOperation = AsMgmtOperation(operation);

if (mgmtOperation.WrapperType != null)
{
// pass operationsBase in so that the construction of [Resource] is possible
writer.Append($"internal {cs.Name}({typeof(ResourceOperationsBase)} operationsBase, {typeof(ClientDiagnostics)} clientDiagnostics, {typeof(HttpPipeline)} pipeline, {typeof(Request)} request, {typeof(Response)} response");
var isTenantResource = (operation.ResultType?.Implementation as MgmtObjectType)?.OperationGroup?.IsTenantResource(context.Configuration.MgmtConfiguration) == true;
var optionType = isTenantResource ? typeof(OperationsBase) : typeof(ResourceOperationsBase);
writer.Append($"internal {cs.Name}({optionType} operationsBase, {typeof(ClientDiagnostics)} clientDiagnostics, {typeof(HttpPipeline)} pipeline, {typeof(Request)} request, {typeof(Response)} response");

if (pagingResponse != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Threading.Tasks;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Generation.Writers;
using AutoRest.CSharp.Mgmt.Decorator;
using AutoRest.CSharp.Mgmt.Output;
using AutoRest.CSharp.Output.Models.Types;
using Azure;
using Azure.Core;
using Azure.ResourceManager.Core;
Expand All @@ -20,7 +22,7 @@ internal class NonLongRunningOperationWriter
/// </summary>
/// <param name="writer"></param>
/// <param name="operation"></param>
public static void Write(CodeWriter writer, NonLongRunningOperation operation)
public static void Write(CodeWriter writer, NonLongRunningOperation operation, BuildContext context)
{
var responseVariable = "response";

Expand Down Expand Up @@ -52,7 +54,9 @@ public static void Write(CodeWriter writer, NonLongRunningOperation operation)
if (operation.ResultDataType != null)
{
// todo: programmatically get the type of operationBase from the definition of [Resource]
writer.Append($"{typeof(ResourceOperationsBase)} operationsBase, ");
var isTenantResource = (operation.ResultType?.Implementation as Resource)?.OperationGroup.IsTenantResource(context.Configuration.MgmtConfiguration) == true;
var optionType = isTenantResource ? typeof(OperationsBase) : typeof(ResourceOperationsBase);
writer.Append($"{optionType} operationsBase, ");
writer.Append($"{typeof(Response)}<{operation.ResultDataType}> {responseVariable}");
}
else
Expand Down
15 changes: 14 additions & 1 deletion src/AutoRest.CSharp/Mgmt/Generation/ResourceContainerWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public void WriteContainer()
string baseClass = GetBaseType();
using (_writer.Scope($"{_resourceContainer.Declaration.Accessibility} partial class {cs.Name:D} : {baseClass}"))
{
WriteContainerCtors(_writer, _resourceContainer.Type.Name, "ResourceOperationsBase", "parent");
var isTenantResource = _resourceContainer.OperationGroup.IsTenantResource(_context.Configuration.MgmtConfiguration);
var conextArgumentType = isTenantResource ? "OperationsBase" : "ResourceOperationsBase";
WriteContainerCtors(_writer, _resourceContainer.Type.Name, conextArgumentType, "parent");
if (!isTenantResource)
{
WriteParent(_writer);
}
WriteFields(_writer, _restClient!);
WriteIdProperty();
WriteContainerProperties(_writer, _resourceContainer.GetValidResourceValue());
Expand All @@ -76,6 +82,13 @@ public void WriteContainer()
}
}

private void WriteParent(CodeWriter writer)
{
_writer.Line();
_writer.WriteXmlDocumentationSummary($"Get the parent resource of this container.");
_writer.Line($"protected new ResourceOperationsBase Parent {{ get {{return base.Parent as ResourceOperationsBase;}} }}");
}

private void WriteRemainingMethods()
{
_writer.Line();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ private void WriteClientCtors(CodeWriter writer, ResourceOperation resourceOpera
writer.WriteXmlDocumentationParameter("id", "The identifier of the resource that is the target of operations.");
}
var baseConstructorCall = isSingleton ? "base(options)" : "base(options, id)";
using (writer.Scope($"protected internal {typeOfThis}({typeof(ResourceOperationsBase)} options{constructorIdParam}) : {baseConstructorCall}"))
var isTenantResource = resourceOperation.OperationGroup.IsTenantResource(context.Configuration.MgmtConfiguration);
var optionType = isTenantResource ? typeof(OperationsBase) : typeof(ResourceOperationsBase);
using (writer.Scope($"protected internal {typeOfThis}({optionType} options{constructorIdParam}) : {baseConstructorCall}"))
{
if (!isSingleton)
{
Expand Down
5 changes: 3 additions & 2 deletions src/AutoRest.CSharp/Mgmt/Generation/ResourceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public void WriteResource(CodeWriter writer, Resource resource, BuildContext<Mgm
{
baseConstructor = " : base(options)";
}

using (writer.Scope($"internal {cs.Name}({typeof(ResourceOperationsBase)} options, {resourceDataObject.Type} resource){baseConstructor}"))
var isTenantResource = resource.OperationGroup.IsTenantResource(context.Configuration.MgmtConfiguration);
var optionType = isTenantResource ? typeof(OperationsBase) : typeof(ResourceOperationsBase);
using (writer.Scope($"internal {cs.Name}({optionType} options, {resourceDataObject.Type} resource){baseConstructor}"))
{
writer.LineRaw("Data = resource;");
}
Expand Down
2 changes: 1 addition & 1 deletion src/AutoRest.CSharp/Mgmt/Output/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Resource(OperationGroup operationGroup, BuildContext context)
if (isExtension)
{
var parentOperationGroup = operationGroup.ParentOperationGroup(context);
parentValue = parentOperationGroup!.Key.ToSingular(false);
parentValue = parentOperationGroup?.Key.ToSingular(false) ?? string.Empty;
}
DefaultName = operationGroup.Resource(context.Configuration.MgmtConfiguration) + parentValue;
Description = BuilderHelpers.EscapeXmlDescription(
Expand Down
3 changes: 3 additions & 0 deletions test/TestProjects/TenantOnly/Generated/AgreementContainer.cs

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

Loading