Skip to content

fix: support nested path parameters in --path-params-as-types #1130

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

Merged
merged 1 commit into from
May 23, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/wise-badgers-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

support nested path parameters in --path-params-as-types
28 changes: 21 additions & 7 deletions packages/openapi-typescript/src/transform/paths-object.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import type { GlobalContext, PathsObject } from "../types.js";
import type { GlobalContext, PathsObject, PathItemObject, ParameterObject, ReferenceObject, OperationObject } from "../types.js";
import { escStr, getEntries, indent } from "../utils.js";
import transformParameterObject from "./parameter-object.js";
import transformPathItemObject from "./path-item-object.js";

const OPERATIONS = ["get", "post", "put", "delete", "options", "head", "patch", "trace"];

function extractPathParams(obj?: ReferenceObject | PathItemObject | OperationObject | undefined): Map<string, ParameterObject> {
const params = new Map();
obj &&
"parameters" in obj &&
obj.parameters?.forEach((p) => {
if ("in" in p && p.in === "path") {
params.set(p.name, p);
}
});
return params;
}

export default function transformPathsObject(pathsObject: PathsObject, ctx: GlobalContext): string {
let { indentLv } = ctx;
const output: string[] = ["{"];
indentLv++;
for (const [url, pathItemObject] of getEntries(pathsObject, ctx.alphabetize, ctx.excludeDeprecated)) {
let path = url;

const pathParams = new Map([...extractPathParams(pathItemObject), ...OPERATIONS.flatMap((op) => Array.from(extractPathParams(pathItemObject[op as keyof PathItemObject])))]);

// build dynamic string template literal index
if (ctx.pathParamsAsTypes && pathItemObject.parameters) {
for (const p of pathItemObject.parameters) {
if ("in" in p && p.in === "path") {
const paramType = transformParameterObject(p, { path: `#/paths/${url}/parameters/${p.name}`, ctx });
path = path.replace(`{${p.name}}`, `\${${paramType}}`);
}
if (ctx.pathParamsAsTypes && pathParams) {
for (const p of pathParams.values()) {
const paramType = transformParameterObject(p, { path: `#/paths/${url}/parameters/${p.name}`, ctx });
path = path.replace(`{${p.name}}`, `\${${paramType}}`);
}
path = `[path: \`${path}\`]`;
} else {
Expand Down
81 changes: 81 additions & 0 deletions packages/openapi-typescript/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,87 @@ export type components = Record<string, never>;

export type external = Record<string, never>;

export type operations = Record<string, never>;
`);
});
});

describe("pathParamsAsTypes (with nested parameters)", () => {
const schema: OpenAPI3 = {
openapi: "3.1",
info: { title: "Test", version: "1.0" },
paths: {
"/user/{user_id}": {
get: {
parameters: [{ name: "user_id", in: "path" }],
},
put: {
parameters: [{ name: "user_id", in: "path" }],
}
},
},
};

test("false", async () => {
const generated = await openapiTS(schema, { pathParamsAsTypes: false });
expect(generated).toBe(`${BOILERPLATE}
export interface paths {
"/user/{user_id}": {
get: {
parameters: {
path: {
user_id: string;
};
};
};
put: {
parameters: {
path: {
user_id: string;
};
};
};
};
}

export type webhooks = Record<string, never>;

export type components = Record<string, never>;

export type external = Record<string, never>;

export type operations = Record<string, never>;
`);
});

test("true", async () => {
const generated = await openapiTS(schema, { pathParamsAsTypes: true });
expect(generated).toBe(`${BOILERPLATE}
export interface paths {
[path: \`/user/\${string}\`]: {
get: {
parameters: {
path: {
user_id: string;
};
};
};
put: {
parameters: {
path: {
user_id: string;
};
};
};
};
}

export type webhooks = Record<string, never>;

export type components = Record<string, never>;

export type external = Record<string, never>;

export type operations = Record<string, never>;
`);
});
Expand Down