Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
f041f24
wip
live1206 Jan 16, 2025
badf1a5
wip
live1206 Jan 20, 2025
69fa28a
wip
live1206 Jan 21, 2025
615fec1
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Jan 21, 2025
d136be2
wip
live1206 Jan 23, 2025
a040627
wip
live1206 Jan 26, 2025
0296a71
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Jan 26, 2025
fda752a
generate operation methods for Resource
live1206 Jan 27, 2025
580508a
add package back
live1206 Jan 27, 2025
ba12225
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 6, 2025
ef83882
update to latest MGC
live1206 Feb 6, 2025
120e164
fix test
live1206 Feb 6, 2025
e4bbde0
Merge branch 'main' into implement-mgmt-resource
live1206 Feb 7, 2025
b2842c4
Add test for ResourceProvider
live1206 Feb 8, 2025
5f5cee9
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 8, 2025
41cf53c
Add RequestPath
live1206 Feb 8, 2025
7bba967
remove unused property
live1206 Feb 10, 2025
5d3a879
address comments
live1206 Feb 11, 2025
2fe508f
Include custom code for convenience methods
live1206 Feb 11, 2025
d9fd368
add TODO
live1206 Feb 11, 2025
17b2a0f
merge from main
live1206 Feb 13, 2025
45a10a7
implement resource LRO
live1206 Feb 13, 2025
b06fb59
implement LRO for resource
live1206 Feb 14, 2025
c98e231
Implement IOperationSouce and generated MgmtTypeSpec can build now
live1206 Feb 14, 2025
848e6d7
update test data
live1206 Feb 14, 2025
0db68cb
Add test for OperationSourceProvider
live1206 Feb 14, 2025
2b15650
update tests
live1206 Feb 14, 2025
51bd7f3
cleanup
live1206 Feb 14, 2025
a8b2b8f
update
live1206 Feb 14, 2025
dc2b83d
cleanup
live1206 Feb 14, 2025
adbcc1c
Address comments
live1206 Feb 15, 2025
bb92673
Add Mgmt-TypeSpec generation to Generate.ps1
live1206 Feb 15, 2025
af9bd9c
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 18, 2025
c5ca437
Update eng/packages/http-client-csharp/generator/Azure.Generator/src/…
live1206 Feb 19, 2025
3ed339d
update
live1206 Feb 19, 2025
d800d35
Merge branch 'main' into implement-mgmt-resource
live1206 Feb 24, 2025
8d78f7f
fix build
live1206 Feb 24, 2025
8fbc8aa
update to use CanonicalView
live1206 Feb 24, 2025
081696d
update test
live1206 Feb 24, 2025
f9e8afd
Use ModelSerializationExtensions.WireOptions instead of new instance
live1206 Feb 24, 2025
8d7b831
update test
live1206 Feb 24, 2025
4dcccc7
Address comments
live1206 Feb 25, 2025
d00ee3a
typo
live1206 Feb 25, 2025
0086b11
Merge remote-tracking branch 'origin/main' into implement-mgmt-resource
live1206 Feb 25, 2025
e34db7b
resolve comments
live1206 Feb 26, 2025
417af63
fix build
live1206 Feb 26, 2025
965e951
update test
live1206 Feb 27, 2025
579dd9b
separate tests for ResourceClientProvider
live1206 Feb 27, 2025
682e704
Add TODO
live1206 Feb 27, 2025
22e08c6
fix tests
live1206 Feb 28, 2025
541193c
Move resource related logic to ResourceBuilder
live1206 Feb 28, 2025
20cdaaf
Remove clientProvider and implement client methods in Resource directly
live1206 Mar 1, 2025
78b5cb5
update
live1206 Mar 1, 2025
ff44b6b
simplify resource detection
live1206 Mar 3, 2025
33c30fa
Merge branch 'main' into implement-mgmt-resource
live1206 Mar 4, 2025
ca43fe6
replace resource detection with decorators
live1206 Mar 6, 2025
7ff96ba
cleanup
live1206 Mar 6, 2025
34478aa
Apply resourceMetadata decorator for generator to consume
live1206 Mar 10, 2025
f6d7776
Merge remote-tracking branch 'upstream/main' into implement-mgmt-reso…
live1206 Mar 10, 2025
41721a8
cleanup
live1206 Mar 10, 2025
949d5e1
refine
live1206 Mar 10, 2025
10ca791
rename
live1206 Mar 10, 2025
eb48437
Merge remote-tracking branch 'upstream/main' into implement-mgmt-reso…
live1206 Mar 11, 2025
70c212d
merge
live1206 Mar 11, 2025
ace3f42
address comments
live1206 Mar 11, 2025
4037c12
fix lint and format
live1206 Mar 11, 2025
79f2a3f
address comments
live1206 Mar 12, 2025
b2ab27a
rename
live1206 Mar 12, 2025
98f91a6
set update callback with options
live1206 Mar 13, 2025
f691c45
update MTG emitter version
live1206 Mar 14, 2025
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
1 change: 1 addition & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
<PackageReference Update="Azure.Messaging.EventGrid" Version="4.17.0" />
<PackageReference Update="Azure.Messaging.EventHubs.Processor" Version="5.11.6" />
<PackageReference Update="Azure.Messaging.ServiceBus" Version="7.18.2" />
<PackageReference Update="Azure.ResourceManager" Version="1.13.0" />
<PackageReference Update="Azure.ResourceManager.Compute" Version="1.7.0-beta.1" />
<PackageReference Update="Azure.ResourceManager.CognitiveServices" Version="1.3.0" />
<PackageReference Update="Azure.ResourceManager.KeyVault" Version="1.1.0" />
Expand Down
78 changes: 76 additions & 2 deletions eng/packages/http-client-csharp/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,91 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

import { EmitContext } from "@typespec/compiler";
import { DecoratorInfo } from "@azure-tools/typespec-client-generator-core";

import {
$onEmit as $OnMGCEmit,
$onEmit as $onMTGEmit,
CodeModel,
CSharpEmitterOptions,
InputModelType,
setSDKContextOptions
} from "@typespec/http-client-csharp";
import { azureSDKContextOptions } from "./sdk-context-options.js";
import { calculateResourceTypeFromPath } from "./resource-type.js";

const armResourceOperations = "Azure.ResourceManager.@armResourceOperations";
const armResourceRead = "Azure.ResourceManager.@armResourceRead";
const armResourceCreateOrUpdate =
"Azure.ResourceManager.@armResourceCreateOrUpdate";
const singleton = "Azure.ResourceManager.@singleton";
const resourceMetadata = "Azure.ClientGenerator.Core.@resourceSchema";

export async function $onEmit(context: EmitContext<CSharpEmitterOptions>) {
context.options["plugin-name"] ??= "AzureClientPlugin";
context.options["emitter-extension-path"] = import.meta.url;
context.options["update-code-model"] = updateCodeModel;
setSDKContextOptions(azureSDKContextOptions);
await $OnMGCEmit(context);
await $onMTGEmit(context);
}

function updateCodeModel(codeModel: CodeModel): CodeModel {
for (const client of codeModel.Clients) {
// TODO: we can implement this decorator in TCGC until we meet the corner case
// if the client has resourceMetadata decorator, it is a resource client and we don't need to add it again
if (client.Decorators?.some((d) => d.name == resourceMetadata)) {
continue;
}

// TODO: Once we have the ability to get resource hierarchy from TCGC directly, we can remove this implementation
// A resource client should have decorator armResourceOperations and contains either a get operation(containing armResourceRead deocrator) or a put operation(containing armResourceCreateOrUpdate decorator)
if (
client.Decorators?.some((d) => d.name == armResourceOperations) &&
client.Operations.some(
(op) =>
op.Decorators?.some(
(d) => d.name == armResourceRead || armResourceCreateOrUpdate
)
)
) {
let resourceModel: InputModelType | undefined = undefined;
let isSingleton: boolean = false;
let resourceType: string | undefined = undefined;
// We will try to get resource metadata from put operation firstly, if not found, we will try to get it from get operation
const putOperation = client.Operations.find(
(op) => op.Decorators?.some((d) => d.name == armResourceCreateOrUpdate)
);
if (putOperation) {
const path = putOperation.Path;
resourceType = calculateResourceTypeFromPath(path);
resourceModel = putOperation.Responses.filter((r) => r.BodyType)[0]
.BodyType as InputModelType;
isSingleton =
resourceModel.decorators?.some((d) => d.name == singleton) ?? false;
} else {
const getOperation = client.Operations.find(
(op) => op.Decorators?.some((d) => d.name == armResourceRead)
);
if (getOperation) {
const path = getOperation.Path;
resourceType = calculateResourceTypeFromPath(path);
resourceModel = getOperation.Responses.filter((r) => r.BodyType)[0]
.BodyType as InputModelType;
isSingleton =
resourceModel.decorators?.some((d) => d.name == singleton) ?? false;
}
}

const resourceMetadataDecorator: DecoratorInfo = {
name: resourceMetadata,
arguments: {}
};
resourceMetadataDecorator.arguments["resourceModel"] =
resourceModel?.crossLanguageDefinitionId;
resourceMetadataDecorator.arguments["isSingleton"] =
isSingleton.toString();
resourceMetadataDecorator.arguments["resourceType"] = resourceType;
client.Decorators.push(resourceMetadataDecorator);
}
}
return codeModel;
}
28 changes: 28 additions & 0 deletions eng/packages/http-client-csharp/emitter/src/resource-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const ResourceGroupScopePrefix =
"/subscriptions/{subscriptionId}/resourceGroups";
const SubscriptionScopePrefix = "/subscriptions";
const TenantScopePrefix = "/tenants";
const Providers = "/providers";

export function calculateResourceTypeFromPath(path: string): string {
const providerIndex = path.indexOf(Providers);
if (providerIndex === -1) {
if (path.startsWith(ResourceGroupScopePrefix)) {
return "Microsoft.Resources/resourceGroups";
} else if (path.startsWith(SubscriptionScopePrefix)) {
return "Microsoft.Resources/subscriptions";
} else if (path.startsWith(TenantScopePrefix)) {
return "Microsoft.Resources/tenants";
}
throw `Path ${path} doesn't have resource type`;
}

return path
.substring(providerIndex + Providers.length)
.split("/")
.reduce((result, current, index) => {
if (index === 1 || index % 2 === 0)
return result === "" ? current : `${result}/${current}`;
else return result;
}, "");
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
import { CreateSdkContextOptions } from "@azure-tools/typespec-client-generator-core";

export const azureSDKContextOptions: CreateSdkContextOptions = {
versioning: {},
additionalDecorators: [
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-client-generator-core/README.md#usesystemtextjsonconverter
"Azure\\.ClientGenerator\\.Core\\.@useSystemTextJsonConverter",
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-resource-manager/README.md#armprovidernamespace
"Azure\\.ResourceManager\\.@armProviderNamespace"
]
versioning: {},
additionalDecorators: [
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-client-generator-core/README.md#usesystemtextjsonconverter
"Azure\\.ClientGenerator\\.Core\\.@useSystemTextJsonConverter",
// TODO: add this decorator to TCGC
"Azure\\.ClientGenerator\\.Core\\.@resourceSchema",
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-resource-manager/README.md#armprovidernamespace
"Azure\\.ResourceManager\\.@armProviderNamespace",
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-resource-manager/README.md#armresourceoperations
"Azure\\.ResourceManager\\.@armResourceOperations",
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-resource-manager/README.md#armResourceRead
"Azure\\.ResourceManager\\.@armResourceRead",
// https://github.com/microsoft/typespec/blob/main/packages/rest/README.md#parentresource
"TypeSpec\\.Rest\\.parentResource",
// https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-resource-manager/README.md#singleton
"Azure\\.ResourceManager\\.@singleton",
"Azure\\.ResourceManager\\.Private\\.@armResourceInternal"
]
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { UsageFlags } from "@azure-tools/typespec-client-generator-core";
import { strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { createModel } from "@typespec/http-client-csharp";
import { createCSharpSdkContext, createEmitterContext, createEmitterTestHost, typeSpecCompile, } from "./test-util.js";
import {
createCSharpSdkContext,
createEmitterContext,
createEmitterTestHost,
typeSpecCompile
} from "./test-util.js";

describe("Test GetInputType for enum", () => {
let runner: TestHost;
beforeEach(async () => {
runner = await createEmitterTestHost();
});
it("Fixed string enum", async () => {
const program = await typeSpecCompile(`
let runner: TestHost;
beforeEach(async () => {
runner = await createEmitterTestHost();
});
it("Fixed string enum", async () => {
const program = await typeSpecCompile(
`
#suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "Enums should be defined without the @fixed decorator."
@doc("fixed string enum")
@fixed
Expand All @@ -26,31 +32,40 @@ describe("Test GetInputType for enum", () => {
#suppress "@azure-tools/typespec-azure-core/use-standard-operations" "Operation 'test' should be defined using a signature from the Azure.Core namespace."
@doc("test fixed enum.")
op test(@doc("fixed enum as input.")@body input: SimpleEnum): string[];
`, runner, { IsNamespaceNeeded: true});
const context = createEmitterContext(program);
const sdkContext = await createCSharpSdkContext(context);
const root = createModel(sdkContext);
const inputParamArray = root.Clients[0].Operations[0].Parameters.filter((p) => p.Name === "input");
strictEqual(1, inputParamArray.length);
const type = inputParamArray[0].Type;
strictEqual(type.kind, "enum");
strictEqual(type.name, "SimpleEnum");
strictEqual(type.isFixed, true);
strictEqual(type.doc, "fixed string enum");
strictEqual(type.crossLanguageDefinitionId, "Azure.Csharp.Testing.SimpleEnum");
strictEqual(type.access, undefined);
strictEqual(type.valueType.kind, "string");
strictEqual(type.values.length, 3);
strictEqual(type.values[0].name, "One");
strictEqual(type.values[0].value, "1");
strictEqual(type.values[1].name, "Two");
strictEqual(type.values[1].value, "2");
strictEqual(type.values[2].name, "Four");
strictEqual(type.values[2].value, "4");
strictEqual(type.usage, UsageFlags.Input | UsageFlags.Json);
});
it("Fixed int enum", async () => {
const program = await typeSpecCompile(`
`,
runner,
{ IsNamespaceNeeded: true }
);
const context = createEmitterContext(program);
const sdkContext = await createCSharpSdkContext(context);
const root = createModel(sdkContext);
const inputParamArray = root.Clients[0].Operations[0].Parameters.filter(
(p) => p.Name === "input"
);
strictEqual(1, inputParamArray.length);
const type = inputParamArray[0].Type;
strictEqual(type.kind, "enum");
strictEqual(type.name, "SimpleEnum");
strictEqual(type.isFixed, true);
strictEqual(type.doc, "fixed string enum");
strictEqual(
type.crossLanguageDefinitionId,
"Azure.Csharp.Testing.SimpleEnum"
);
strictEqual(type.access, undefined);
strictEqual(type.valueType.kind, "string");
strictEqual(type.values.length, 3);
strictEqual(type.values[0].name, "One");
strictEqual(type.values[0].value, "1");
strictEqual(type.values[1].name, "Two");
strictEqual(type.values[1].value, "2");
strictEqual(type.values[2].name, "Four");
strictEqual(type.values[2].value, "4");
strictEqual(type.usage, UsageFlags.Input | UsageFlags.Json);
});
it("Fixed int enum", async () => {
const program = await typeSpecCompile(
`
#suppress "@azure-tools/typespec-azure-core/use-extensible-enum" "Enums should be defined without the @fixed decorator."
@doc("Fixed int enum")
@fixed
Expand All @@ -65,29 +80,37 @@ describe("Test GetInputType for enum", () => {
#suppress "@azure-tools/typespec-azure-core/use-standard-operations" "Operation 'test' should be defined using a signature from the Azure.Core namespace."
@doc("test fixed enum.")
op test(@doc("fixed enum as input.")@body input: FixedIntEnum): string[];
`, runner, { IsNamespaceNeeded: true });
const context = createEmitterContext(program);
const sdkContext = await createCSharpSdkContext(context);
const root = createModel(sdkContext);
const inputParamArray = root.Clients[0].Operations[0].Parameters.filter((p) => p.Name === "input");
strictEqual(1, inputParamArray.length);
const type = inputParamArray[0].Type;
strictEqual(type.kind, "enum");
strictEqual(type.name, "FixedIntEnum");
strictEqual(type.crossLanguageDefinitionId, "Azure.Csharp.Testing.FixedIntEnum");
strictEqual(type.access, undefined);
strictEqual(type.doc, "Fixed int enum");
strictEqual(type.valueType.crossLanguageDefinitionId, "TypeSpec.int32");
strictEqual(type.valueType.kind, "int32");
strictEqual(type.values.length, 3);
strictEqual(type.values[0].name, "One");
strictEqual(type.values[0].value, 1);
strictEqual(type.values[1].name, "Two");
strictEqual(type.values[1].value, 2);
strictEqual(type.values[2].name, "Four");
strictEqual(type.values[2].value, 4);
strictEqual(type.isFixed, true);
strictEqual(type.usage, UsageFlags.Input | UsageFlags.Json);
});
`,
runner,
{ IsNamespaceNeeded: true }
);
const context = createEmitterContext(program);
const sdkContext = await createCSharpSdkContext(context);
const root = createModel(sdkContext);
const inputParamArray = root.Clients[0].Operations[0].Parameters.filter(
(p) => p.Name === "input"
);
strictEqual(1, inputParamArray.length);
const type = inputParamArray[0].Type;
strictEqual(type.kind, "enum");
strictEqual(type.name, "FixedIntEnum");
strictEqual(
type.crossLanguageDefinitionId,
"Azure.Csharp.Testing.FixedIntEnum"
);
strictEqual(type.access, undefined);
strictEqual(type.doc, "Fixed int enum");
strictEqual(type.valueType.crossLanguageDefinitionId, "TypeSpec.int32");
strictEqual(type.valueType.kind, "int32");
strictEqual(type.values.length, 3);
strictEqual(type.values[0].name, "One");
strictEqual(type.values[0].value, 1);
strictEqual(type.values[1].name, "Two");
strictEqual(type.values[1].value, 2);
strictEqual(type.values[2].name, "Four");
strictEqual(type.values[2].value, 4);
strictEqual(type.isFixed, true);
strictEqual(type.usage, UsageFlags.Input | UsageFlags.Json);
});
});
//# sourceMappingURL=property-type.test.js.map
//# sourceMappingURL=property-type.test.js.map
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createCSharpSdkContext,
createEmitterContext,
createEmitterTestHost,
typeSpecCompile,
typeSpecCompile
} from "./test-util.js";

describe("Test GetInputType for scalar", () => {
Expand All @@ -21,13 +21,13 @@ describe("Test GetInputType for scalar", () => {
`
op test(@query location: azureLocation): void;
`,
runner,
runner
);
const context = await createCSharpSdkContext(createEmitterContext(program));
const model = createModel(context);

const inputParamArray = model.Clients[0].Operations[0].Parameters.filter(
(p) => p.Name === "location",
(p) => p.Name === "location"
);
strictEqual(1, inputParamArray.length);
const type = inputParamArray[0].Type;
Expand Down
Loading