Skip to content

Commit

Permalink
Fix missing parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Jan 19, 2024
1 parent 2d3679a commit 747611b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-hounds-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Fix missing params when they have same name, different location
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;
};
};
};
}`,
},
});
});
});

0 comments on commit 747611b

Please sign in to comment.