From 23a18d13bb206b7fbc14f7a337804cf99d54123b Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Fri, 9 Aug 2024 13:33:52 +0800 Subject: [PATCH] tsp, path allowReserved (#2904) * tsp, path allowReserved * update for tsp-client 0.19.0 --- eng/sdk/sync_sdk.py | 2 +- typespec-extension/src/code-model-builder.ts | 48 ++++++++++---------- typespec-tests/tsp/server.tsp | 4 +- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/eng/sdk/sync_sdk.py b/eng/sdk/sync_sdk.py index 40555ee155..4f3a369bd6 100644 --- a/eng/sdk/sync_sdk.py +++ b/eng/sdk/sync_sdk.py @@ -77,7 +77,7 @@ def update_emitter(package_json_path: str, use_dev_package: bool): logging.error('Failed to locate the dev package.') logging.info('Update emitter-package-lock.json') - subprocess.check_call(['tsp-client', '--generate-lock-file'], cwd=sdk_root) + subprocess.check_call(['tsp-client', 'generate-lock-file'], cwd=sdk_root) def get_generated_folder_from_artifact(module_path: str, artifact: str, type: str) -> str: diff --git a/typespec-extension/src/code-model-builder.ts b/typespec-extension/src/code-model-builder.ts index 7242480668..47d96a3a15 100644 --- a/typespec-extension/src/code-model-builder.ts +++ b/typespec-extension/src/code-model-builder.ts @@ -67,7 +67,6 @@ import { getHttpOperationWithCache, getWireName, isApiVersion, - isInternal, isSdkBuiltInKind, isSdkIntKind, listClients, @@ -116,10 +115,8 @@ import { Visibility, getAuthentication, getHeaderFieldName, - getHeaderFieldOptions, getPathParamName, getQueryParamName, - getQueryParamOptions, getServers, getStatusCodeDescription, isHeader, @@ -306,6 +303,7 @@ export class CodeModelBuilder { }, }, extensions: { + // TODO: deprecate this logic of string/url for x-ms-skip-url-encoding "x-ms-skip-url-encoding": schema instanceof UriSchema, }, // // make the logic same as TCGC, which takes the server-side default of host as client-side default @@ -401,14 +399,12 @@ export class CodeModelBuilder { return !this.options["flavor"] || this.options["flavor"].toLocaleLowerCase() === "azure"; } - private isInternal(context: SdkContext, operation: Operation): boolean { + private isInternal(operation: Operation): boolean { const access = getAccess(operation); if (access) { return access === "internal"; } else { - // TODO: deprecate "internal" - // eslint-disable-next-line deprecation/deprecation - return isInternal(context, operation); + return false; } } @@ -759,7 +755,7 @@ export class CodeModelBuilder { this.sdkContext, operation, ); - codeModelOperation.internalApi = this.isInternal(this.sdkContext, operation); + codeModelOperation.internalApi = this.isInternal(operation); const convenienceApiName = this.getConvenienceApiName(operation); let generateConvenienceApi: boolean = Boolean(convenienceApiName); @@ -1072,14 +1068,22 @@ export class CodeModelBuilder { schema = this.processSchemaFromSdkType(sdkType, param.param.name); } - // skip-url-encoding let extensions: { [id: string]: any } | undefined = undefined; + // skip-url-encoding + if (param.type === "path") { + if (param.allowReserved) { + extensions = extensions ?? {}; + extensions["x-ms-skip-url-encoding"] = true; + } + } + // TODO: deprecate this logic of string/url for x-ms-skip-url-encoding if ( (param.type === "query" || param.type === "path") && param.param.type.kind === "Scalar" && schema instanceof UriSchema ) { - extensions = { "x-ms-skip-url-encoding": true }; + extensions = extensions ?? {}; + extensions["x-ms-skip-url-encoding"] = true; } if (this.supportsAdvancedVersioning()) { @@ -1096,9 +1100,8 @@ export class CodeModelBuilder { let explode = undefined; if (param.param.type.kind === "Model" && isArrayModelType(this.program, param.param.type)) { if (param.type === "query") { - const queryParamOptions = getQueryParamOptions(this.program, param.param); // eslint-disable-next-line deprecation/deprecation - const queryParamFormat = queryParamOptions?.format; + const queryParamFormat = param?.format; if (queryParamFormat) { switch (queryParamFormat) { case "csv": @@ -1128,17 +1131,16 @@ export class CodeModelBuilder { } } } else if (param.type === "header") { - const headerFieldOptions = getHeaderFieldOptions(this.program, param.param); - switch (headerFieldOptions?.format) { - case "csv": - style = SerializationStyle.Simple; - break; - - default: - if (headerFieldOptions?.format) { - this.logWarning(`Unrecognized header parameter format: '${headerFieldOptions?.format}'.`); - } - break; + if (param.format) { + switch (param.format) { + case "csv": + style = SerializationStyle.Simple; + break; + + default: + this.logWarning(`Unrecognized header parameter format: '${param.format}'.`); + break; + } } } } diff --git a/typespec-tests/tsp/server.tsp b/typespec-tests/tsp/server.tsp index a1cb101b67..55b850a1a0 100644 --- a/typespec-tests/tsp/server.tsp +++ b/typespec-tests/tsp/server.tsp @@ -59,8 +59,8 @@ namespace Cadl.ContosoServer { name: "Contoso.Sub.ContosoClient", service: Cadl.ContosoServer, }) - @route("/contoso/{group}") + @route("/contoso/") interface ServerOp { - get(@path group: url): OkResponse | NoContentResponse; + get(@path(#{ allowReserved: true }) group: string): OkResponse | NoContentResponse; } }