From 90d54b9ece21be63703a793be5fc182d7eb37747 Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Thu, 23 May 2024 13:53:02 -0400 Subject: [PATCH] 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). --- .../src/map-openapi-endpoints.ts | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/typed-openapi/src/map-openapi-endpoints.ts b/packages/typed-openapi/src/map-openapi-endpoints.ts index b4892d8..b5967ae 100644 --- a/packages/typed-openapi/src/map-openapi-endpoints.ts +++ b/packages/typed-openapi/src/map-openapi-endpoints.ts @@ -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>; + + 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