Skip to content

Fix missing parameters #1509

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 2 commits into from
Jan 19, 2024
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
6 changes: 6 additions & 0 deletions packages/openapi-typescript/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# openapi-typescript

## 6.7.4

### Patch Changes

- [#1509](https://github.com/drwpow/openapi-typescript/pull/1509) [`747611bcd0d056d5027841f985aed0aefcf6b63d`](https://github.com/drwpow/openapi-typescript/commit/747611bcd0d056d5027841f985aed0aefcf6b63d) Thanks [@drwpow](https://github.com/drwpow)! - Fix missing params when they have same name, different location

## 6.7.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "openapi-typescript",
"description": "Convert OpenAPI 3.0 & 3.1 schemas to TypeScript",
"version": "6.7.3",
"version": "6.7.4",
"author": {
"name": "Drew Powers",
"email": "[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function transformParameterObjectArray(parameterObjectArray: (Par
output.push(
indent(
`${key}: ${transformParameterObject(node, {
path: `${path}/${node.name}`,
path: `${path}/${node.in}/${node.name}`,
ctx: { ...ctx, indentLv: ctx.indentLv + 1 },
})};`,
ctx.indentLv,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function transformPathItemObject(pathItem: PathItemObject, { path
// important: OperationObject parameters come last, and will override any conflicts with PathItem parameters
for (const parameter of [...(pathItem.parameters ?? []), ...(operationObject.parameters ?? [])]) {
// note: the actual key doesn’t matter here, as long as it can match between PathItem and OperationObject
keyedParameters["$ref" in parameter ? parameter.$ref : parameter.name] = parameter;
keyedParameters["$ref" in parameter ? parameter.$ref : `${parameter.in}/${parameter.name}`] = parameter;
}
}

Expand Down
61 changes: 61 additions & 0 deletions packages/openapi-typescript/test/path-item-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,65 @@ describe("Path Item Object", () => {
get: components["schemas"]["GetUserOperation"]
}`);
});

it("operations with parameters with same name, different location", () => {
const operations: GlobalContext["operations"] = {};
const schema: PathItemObject = {
get: {
operationId: "getUserById",
summary: "Returns a user by ID.",
parameters: [
{
in: "path",
name: "user_id",
required: true,
schema: { type: "string" },
},
{
in: "query",
name: "user_id",
schema: { type: "string" },
},
],
responses: {
"200": {
description: "OK",
content: {
"application/json": {
schema: { type: "string" },
},
},
},
},
},
};
const generated = transformPathItemObject(schema, { ...options, ctx: { ...options.ctx, operations } });
expect(generated).toBe(`{
/** Returns a user by ID. */
get: operations["getUserById"];
}`);
expect(operations).toEqual({
getUserById: {
comment: "/** Returns a user by ID. */",
operationType: `{
parameters: {
query?: {
user_id?: string;
};
path: {
user_id: string;
};
};
responses: {
/** @description OK */
200: {
content: {
"application/json": string;
};
};
};
}`,
},
});
});
});