Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function fromSdkClient(
doc: client.doc,
summary: client.summary,
methods: client.methods
.map((m) => fromSdkServiceMethod(sdkContext, m, uri, rootApiVersions, client.namespace))
.map((m) =>
fromSdkServiceMethod(sdkContext, m, uri, rootApiVersions, client.namespace, client),
)
.filter((m) => m !== undefined),
parameters: clientParameters,
initializedBy: client.clientInitialization.initializedBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isHttpMetadata,
SdkBodyParameter,
SdkBuiltInKinds,
SdkClientType,
SdkContext,
SdkHeaderParameter,
SdkHttpOperation,
Expand All @@ -31,6 +32,7 @@ import { getDeprecated, isErrorModel, NoTarget } from "@typespec/compiler";
import { HttpStatusCodeRange } from "@typespec/http";
import { getResourceOperation } from "@typespec/rest";
import { CSharpEmitterContext } from "../sdk-context.js";
import { ClientOptions } from "../type/client-options.js";
import { collectionFormatToDelimMap } from "../type/collection-format.js";
import { HttpResponseHeader } from "../type/http-response-header.js";
import { InputConstant } from "../type/input-constant.js";
Expand Down Expand Up @@ -73,6 +75,7 @@ export function fromSdkServiceMethod(
uri: string,
rootApiVersions: string[],
namespace: string,
client?: SdkClientType<SdkHttpOperation>,
): InputServiceMethod | undefined {
let method = sdkContext.__typeCache.methods.get(sdkMethod);
if (method) {
Expand All @@ -88,6 +91,7 @@ export function fromSdkServiceMethod(
uri,
rootApiVersions,
namespace,
client,
);
break;
case "paging":
Expand All @@ -97,13 +101,15 @@ export function fromSdkServiceMethod(
uri,
rootApiVersions,
namespace,
client,
);
pagingServiceMethod.pagingMetadata = loadPagingServiceMetadata(
sdkContext,
sdkMethod,
rootApiVersions,
uri,
namespace,
client,
);
method = pagingServiceMethod;
break;
Expand All @@ -114,6 +120,7 @@ export function fromSdkServiceMethod(
uri,
rootApiVersions,
namespace,
client,
);
lroServiceMethod.lroMetadata = loadLongRunningMetadata(sdkContext, sdkMethod);
method = lroServiceMethod;
Expand All @@ -125,6 +132,7 @@ export function fromSdkServiceMethod(
uri,
rootApiVersions,
namespace,
client,
);
lroPagingMethod.lroMetadata = loadLongRunningMetadata(sdkContext, sdkMethod);
lroPagingMethod.pagingMetadata = loadPagingServiceMetadata(
Expand All @@ -133,6 +141,7 @@ export function fromSdkServiceMethod(
rootApiVersions,
uri,
namespace,
client,
);
method = lroPagingMethod;
break;
Expand All @@ -158,6 +167,7 @@ export function fromSdkServiceMethodOperation(
method: SdkServiceMethod<SdkHttpOperation>,
uri: string,
rootApiVersions: string[],
client?: SdkClientType<SdkHttpOperation>,
): InputOperation {
let operation = sdkContext.__typeCache.operations.get(method.operation);
if (operation) {
Expand All @@ -176,6 +186,13 @@ export function fromSdkServiceMethodOperation(
generateConvenience = false;
}

const includeRootSlash = resolveIncludeRootSlash(method, client);
const operationPath = method.operation.path;
const path =
!includeRootSlash && operationPath.length > 0 && operationPath[0] === "/"
? operationPath.substring(1)
: operationPath;

operation = {
name: method.name,
resourceName:
Expand All @@ -190,7 +207,7 @@ export function fromSdkServiceMethodOperation(
responses: fromSdkHttpOperationResponses(sdkContext, method.operation.responses),
httpMethod: parseHttpRequestMethod(method.operation.verb),
uri: uri,
path: method.operation.path,
path: path,
externalDocsUrl: getExternalDocs(sdkContext, method.operation.__raw.operation)?.url,
requestMediaTypes: getRequestMediaTypes(method.operation),
bufferResponse: true,
Expand Down Expand Up @@ -241,6 +258,7 @@ function createServiceMethod<T extends InputServiceMethod>(
uri: string,
rootApiVersions: string[],
namespace: string,
client?: SdkClientType<SdkHttpOperation>,
): T {
return {
kind: method.kind,
Expand All @@ -249,7 +267,7 @@ function createServiceMethod<T extends InputServiceMethod>(
apiVersions: method.apiVersions,
doc: method.doc,
summary: method.summary,
operation: fromSdkServiceMethodOperation(sdkContext, method, uri, rootApiVersions),
operation: fromSdkServiceMethodOperation(sdkContext, method, uri, rootApiVersions, client),
parameters: fromSdkServiceMethodParameters(sdkContext, method, rootApiVersions, namespace),
response: fromSdkServiceMethodResponse(sdkContext, method.response),
exception: method.exception
Expand Down Expand Up @@ -702,6 +720,7 @@ function loadPagingServiceMetadata(
rootApiVersions: string[],
uri: string,
namespace: string,
client?: SdkClientType<SdkHttpOperation>,
): InputPagingServiceMetadata {
let nextLink: InputNextLink | undefined;
if (method.pagingMetadata.nextLinkSegments) {
Expand All @@ -723,6 +742,7 @@ function loadPagingServiceMetadata(
uri,
rootApiVersions,
namespace,
client,
);
}

Expand Down Expand Up @@ -985,7 +1005,7 @@ function getCollectionHeaderPrefix(
sdkContext: CSharpEmitterContext,
p: SdkHeaderParameter,
): string | undefined {
const value = getClientOptions(p, "collectionHeaderPrefix");
const value = getClientOptions(p, ClientOptions.collectionHeaderPrefix);
if (value === undefined) {
return undefined;
}
Expand All @@ -998,11 +1018,35 @@ function getCollectionHeaderPrefix(
sdkContext.logger.reportDiagnostic({
code: "general-warning",
format: {
message: `The 'collectionHeaderPrefix' client option must be a string value, but got '${typeof value}'. The option will be ignored.`,
message: `The '${ClientOptions.collectionHeaderPrefix}' client option must be a string value, but got '${typeof value}'. The option will be ignored.`,
},
target: p.__raw ?? NoTarget,
});
return undefined;
}
return value;
}

function resolveIncludeRootSlash(
method: SdkServiceMethod<SdkHttpOperation>,
client?: SdkClientType<SdkHttpOperation>,
): boolean {
// First check the method/operation level
const methodOption = getClientOptions(method, ClientOptions.includeRootSlash);
if (methodOption !== undefined) {
return methodOption !== false;
}

// Walk up the client hierarchy
let current: SdkClientType<SdkHttpOperation> | undefined = client;
while (current) {
const clientOption = getClientOptions(current, ClientOptions.includeRootSlash);
if (clientOption !== undefined) {
return clientOption !== false;
}
current = current.parent;
}

// Default: include root slash
return true;
}
13 changes: 13 additions & 0 deletions packages/http-client-csharp/emitter/src/type/client-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

/**
* Constants for the supported client option names used with the @clientOption decorator.
* @internal
*/
export const ClientOptions = {
/** Controls whether the root slash is included in the operation path. */
includeRootSlash: "includeRootSlash",
/** Sets a prefix for collection header parameters. */
collectionHeaderPrefix: "collectionHeaderPrefix",
} as const;
Loading