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

fix operation hierarchies info in sample gen #3023

Merged
merged 11 commits into from
Feb 18, 2025
55 changes: 24 additions & 31 deletions packages/typespec-ts/src/modular/emitSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
SdkHttpOperationExample,
SdkHttpParameterExampleValue,
SdkInitializationType,
SdkServiceMethod,
SdkServiceOperation,
SdkExampleValue
} from "@azure-tools/typespec-client-generator-core";
Expand All @@ -33,6 +32,10 @@ import {
hasKeyCredential,
hasTokenCredential
} from "../utils/credentialUtils.js";
import {
getMethodHierarchiesMap,
ServiceOperation
} from "../utils/operationUtil.js";

/**
* Interfaces for samples generations
Expand Down Expand Up @@ -74,37 +77,26 @@ function emitClientSamples(
client: SdkClientType<SdkServiceOperation>,
options: EmitSampleOptions
) {
for (const operationOrGroup of client.methods) {
// handle client-level methods
if (operationOrGroup.kind !== "clientaccessor") {
emitMethodSamples(dpgContext, operationOrGroup, options);
continue;
}
// handle operation group
let prefix = normalizeName(
operationOrGroup.response.name,
NameType.Property
);
// append hierarchy prefix if hierarchyClient is enabled
if (dpgContext.rlcOptions?.hierarchyClient === true) {
prefix =
(options.classicalMethodPrefix
? `${options.classicalMethodPrefix}.`
: "") + prefix;
} else if (dpgContext.rlcOptions?.enableOperationGroup === false) {
prefix = "";
const methodMap = getMethodHierarchiesMap(dpgContext, client);
for (const [prefixKey, operations] of methodMap) {
const prefix = prefixKey
.split("/")
.map((name) => {
return normalizeName(name, NameType.Property);
})
.join(".");
for (const op of operations) {
emitMethodSamples(dpgContext, op, {
...options,
classicalMethodPrefix: prefix
});
}

emitClientSamples(dpgContext, operationOrGroup.response, {
...options,
classicalMethodPrefix: prefix
});
}
}

function emitMethodSamples(
dpgContext: SdkContext,
method: SdkServiceMethod<SdkServiceOperation>,
method: ServiceOperation,
options: EmitSampleOptions
): SourceFile | undefined {
const examples = method.operation.examples ?? [];
Expand All @@ -113,7 +105,7 @@ function emitMethodSamples(
}
const project = useContext("outputProject");
const operationPrefix = `${options.classicalMethodPrefix ?? ""} ${
method.name
method.oriName ?? method.name
}`;
const sampleFolder = join(
dpgContext.generationPathDetail?.rootDir ?? "",
Expand Down Expand Up @@ -194,7 +186,7 @@ function emitMethodSamples(
? `${options.classicalMethodPrefix}.`
: "";
const isPaging = method.kind === "paging";
const methodCall = `client.${prefix}${method.name}(${methodParams.join(
const methodCall = `client.${prefix}${normalizeName(method.oriName ?? method.name, NameType.Property)}(${methodParams.join(
", "
)})`;
if (isPaging) {
Expand All @@ -212,7 +204,8 @@ function emitMethodSamples(
}

// Create a function declaration structure
const description = method.doc ?? `execute ${method.name}`;
const description =
method.doc ?? `execute ${method.oriName ?? method.name}`;
const normalizedDescription =
description.charAt(0).toLowerCase() + description.slice(1);
const functionDeclaration: FunctionDeclarationStructure = {
Expand Down Expand Up @@ -252,7 +245,7 @@ function buildParameterValueMap(example: SdkHttpOperationExample) {

function prepareExampleParameters(
dpgContext: SdkContext,
method: SdkServiceMethod<SdkServiceOperation>,
method: ServiceOperation,
parameterMap: Record<string, SdkHttpParameterExampleValue>,
topLevelClient: SdkClientType<SdkServiceOperation>
): ExampleValue[] {
Expand Down Expand Up @@ -284,7 +277,7 @@ function prepareExampleParameters(
reportDiagnostic(dpgContext.program, {
code: "required-sample-parameter",
format: {
exampleName: method.name,
exampleName: method.oriName ?? method.name,
paramName: param.name
},
target: NoTarget
Expand Down
7 changes: 6 additions & 1 deletion packages/typespec-ts/src/utils/operationUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,12 @@ export function getMethodHierarchiesMap(
if (!method) {
continue;
}
const prefixes = method[0];
const prefixes =
context.rlcOptions?.hierarchyClient === false &&
context.rlcOptions?.enableOperationGroup &&
method[0].length > 0
? [method[0][method[0].length - 1] as string]
: method[0];
const operationOrGroup = method[1];

if (operationOrGroup.kind === "clientaccessor") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ interface Employees {
}
```

This is the tspconfig.yaml.

```yaml
hierarchyClient: false
enableOperationGroup: true
```
## Example
Raw json files.
Expand Down
Loading
Loading