Skip to content

Commit 48b2786

Browse files
0237hastahmer
authored andcommitted
Allow for finer marking of optional parameters
Current behavior allows only for marking *all* parameters as optional, or none. This change checks first if all parameters are optional, keeping the old behavior if that's the case, otherwise iterates through the parameters to mark only those that **should** be optional from the OpenAPI spec. Resolves: #33
1 parent ac07adc commit 48b2786

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,22 @@ export const mapOpenApiEndpoints = (doc: OpenAPIObject) => {
8888
// Make parameters optional if none of them are required
8989
if (params) {
9090
const t = createBoxFactory({}, ctx);
91-
if (params.query && lists.query.length && lists.query.every((param) => !param.required)) {
92-
if (!params.query) params.query = {};
93-
params.query = t.reference("Partial", [t.object(params.query)]) as any;
94-
}
95-
if (params.path && lists.path.length && lists.path.every((param) => !param.required)) {
96-
params.path = t.reference("Partial", [t.object(params.path)]) as any;
97-
}
98-
if (params.header && lists.header.length && lists.header.every((param) => !param.required)) {
99-
params.header = t.reference("Partial", [t.object(params.header)]) as any;
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+
}
103+
}
104+
}
105+
}
106+
}
100107
}
101108

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

0 commit comments

Comments
 (0)