Skip to content

Commit 012e0e5

Browse files
Stop stripping explit query parameters from url in autorest emitter
1 parent 8f9bf3b commit 012e0e5

3 files changed

Lines changed: 254 additions & 237 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: fix
4+
packages:
5+
- "@azure-tools/typespec-autorest"
6+
---
7+
8+
Preserve explicit query parameters defined in the route by including them in `x-ms-paths`

packages/typespec-autorest/src/openapi.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,6 @@ export async function getOpenAPIForService(
566566
return isShared;
567567
}
568568

569-
function getPathWithoutQuery(path: string): string {
570-
// strip everything from the key including and after the ?
571-
return path.replace(/\/?\?.*/, "");
572-
}
573-
574569
function getFinalStateVia(metadata: LroMetadata): XMSLongRunningFinalState | undefined {
575570
switch (metadata.finalStateVia) {
576571
case FinalStateValue.azureAsyncOperation:
@@ -610,36 +605,38 @@ export async function getOpenAPIForService(
610605
return undefined;
611606
}
612607

613-
function emitOperation(operation: HttpOperation) {
614-
let { path: fullPath, operation: op, verb, parameters } = operation;
608+
/** Initialize the openapi PathItem object where this operation should be added. */
609+
function initPathItem(operation: HttpOperation): OpenAPI2PathItem {
610+
let { path, operation: op, verb } = operation;
615611
let pathsObject: Record<string, OpenAPI2PathItem> = root.paths;
616612

617-
const pathWithoutAnyQuery = getPathWithoutQuery(fullPath);
618-
619-
if (root.paths[pathWithoutAnyQuery]?.[verb] === undefined) {
620-
fullPath = pathWithoutAnyQuery;
613+
if (root.paths[path]?.[verb] === undefined && !path.includes("?")) {
621614
pathsObject = root.paths;
622-
} else if (requiresXMsPaths(fullPath, op)) {
623-
// if the key already exists in x-ms-paths, append
624-
// the operation id.
625-
if (fullPath.includes("?")) {
626-
if (root["x-ms-paths"]?.[fullPath] !== undefined) {
627-
fullPath += `&_overload=${operation.operation.name}`;
615+
} else if (requiresXMsPaths(path, op)) {
616+
// if the key already exists in x-ms-paths, append the operation id.
617+
if (path.includes("?")) {
618+
if (root["x-ms-paths"]?.[path] !== undefined) {
619+
path += `&_overload=${operation.operation.name}`;
628620
}
629621
} else {
630-
fullPath += `?_overload=${operation.operation.name}`;
622+
path += `?_overload=${operation.operation.name}`;
631623
}
632624
pathsObject = root["x-ms-paths"] as any;
633625
} else {
634626
// This should not happen because http library should have already validated duplicate path or the routes must have been using shared routes and so goes in previous condition.
635-
compilerAssert(false, `Duplicate route "${fullPath}". This is unexpected.`);
627+
compilerAssert(false, `Duplicate route "${path}". This is unexpected.`);
636628
}
637629

638-
if (!pathsObject[fullPath]) {
639-
pathsObject[fullPath] = {};
630+
if (!pathsObject[path]) {
631+
pathsObject[path] = {};
640632
}
641633

642-
const currentPath = pathsObject[fullPath];
634+
return pathsObject[path];
635+
}
636+
637+
function emitOperation(operation: HttpOperation) {
638+
const { operation: op, verb, parameters } = operation;
639+
const currentPath = initPathItem(operation);
643640
if (!currentPath[verb]) {
644641
currentPath[verb] = {} as any;
645642
}

0 commit comments

Comments
 (0)