Skip to content

Commit 90d54b9

Browse files
0237hastahmer
authored andcommitted
Change parameter filtering to be explicit
In order to not pick up unwanted properties from the `params` object in case it changed in the future, we explicitly filter the keys using the `query`, `path` and `header` values (with additional TS typing).
1 parent ef2edea commit 90d54b9

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

packages/typed-openapi/src/map-openapi-endpoints.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,23 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
8585
}
8686
}
8787

88-
// Make parameters optional if none of them are required
88+
// Make parameters optional if all or some of them are not required
8989
if (params) {
9090
const t = createBoxFactory({}, ctx);
91-
92-
let k: keyof typeof params;
93-
for (k in params) {
94-
if (k !== "body") {
95-
if (params[k] && lists[k].length) {
96-
if (lists[k].every((param) => !param.required)) {
97-
params[k] = t.reference("Partial", [t.object(params[k]!)]) as any;
98-
} else {
99-
for (const p of lists[k]) {
100-
if (!p.required) {
101-
params[k]![p.name] = t.optional(params[k]![p.name] as any);
102-
}
91+
const filtered_params = ["query", "path", "header"] as Array<keyof Pick<typeof params, "query" | "path" | "header">>;
92+
93+
for (const k of filtered_params) {
94+
if (params[k] && lists[k].length) {
95+
if (lists[k].every((param) => !param.required)) {
96+
params[k] = t.reference("Partial", [t.object(params[k]!)]) as any;
97+
} else {
98+
for (const p of lists[k]) {
99+
if (!p.required) {
100+
params[k]![p.name] = t.optional(params[k]![p.name] as any);
103101
}
104102
}
105-
}
106-
}
103+
}
104+
}
107105
}
108106

109107
// No need to pass empty objects, it's confusing

0 commit comments

Comments
 (0)