Skip to content

Commit

Permalink
Change parameter filtering to be explicit
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
0237h authored and astahmer committed May 24, 2024
1 parent ef2edea commit 90d54b9
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions packages/typed-openapi/src/map-openapi-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,23 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
}
}

// Make parameters optional if none of them are required
// Make parameters optional if all or some of them are not required
if (params) {
const t = createBoxFactory({}, ctx);

let k: keyof typeof params;
for (k in params) {
if (k !== "body") {
if (params[k] && lists[k].length) {
if (lists[k].every((param) => !param.required)) {
params[k] = t.reference("Partial", [t.object(params[k]!)]) as any;
} else {
for (const p of lists[k]) {
if (!p.required) {
params[k]![p.name] = t.optional(params[k]![p.name] as any);
}
const filtered_params = ["query", "path", "header"] as Array<keyof Pick<typeof params, "query" | "path" | "header">>;

for (const k of filtered_params) {
if (params[k] && lists[k].length) {
if (lists[k].every((param) => !param.required)) {
params[k] = t.reference("Partial", [t.object(params[k]!)]) as any;
} else {
for (const p of lists[k]) {
if (!p.required) {
params[k]![p.name] = t.optional(params[k]![p.name] as any);
}
}
}
}
}
}
}

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

0 comments on commit 90d54b9

Please sign in to comment.