Skip to content

Commit e7011e3

Browse files
committed
fix: don't explode headers
1 parent 75d1efa commit e7011e3

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

packages/openapi-generator/src/route.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,25 @@ function parseRequestUnion(
157157
});
158158
}
159159
if (headerSchema.schemas.length > 0) {
160-
parameters.push({
161-
type: 'header',
162-
name: 'union',
163-
explode: true,
164-
required: true,
165-
schema: headerSchema,
166-
});
160+
// For headers in unions, take properties from first schema that has headers
161+
// Also not perfect but we cannot use the `explode: true` trick for headers
162+
const firstHeaderSchema = schema.schemas.find(
163+
(s) => s.type === 'object' && s.properties['headers']?.type === 'object',
164+
);
165+
if (
166+
firstHeaderSchema?.type === 'object' &&
167+
firstHeaderSchema.properties['headers']?.type === 'object'
168+
) {
169+
const headers = firstHeaderSchema.properties['headers'];
170+
for (const [name, prop] of Object.entries(headers.properties)) {
171+
parameters.push({
172+
type: 'header',
173+
name,
174+
schema: prop,
175+
required: headers.required.includes(name),
176+
});
177+
}
178+
}
167179
}
168180

169181
const firstSubSchema = schema.schemas[0];

packages/openapi-generator/test/openapi/union.test.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,58 @@ testCase("route with unknown unions", ROUTE_WITH_UNKNOWN_UNIONS, {
373373
}
374374
}
375375
},
376-
});
376+
});
377+
378+
const ROUTE_WITH_REQUEST_UNION = `
379+
import * as t from 'io-ts';
380+
import * as h from '@api-ts/io-ts-http';
381+
import { BooleanFromString, BooleanFromNumber, NumberFromString } from 'io-ts-types';
382+
383+
export const route = h.httpRoute({
384+
path: '/foo',
385+
method: 'GET',
386+
request: t.union([
387+
h.httpRequest({
388+
headers: {
389+
foo: t.string,
390+
},
391+
}),
392+
h.httpRequest({}),
393+
]),
394+
response: {
395+
200: t.string,
396+
},
397+
});
398+
`;
399+
400+
testCase("route with request union", ROUTE_WITH_REQUEST_UNION, {
401+
info: {
402+
title: 'Test',
403+
version: '1.0.0'
404+
},
405+
openapi: '3.0.3',
406+
paths: {
407+
'/foo': {
408+
get: {
409+
parameters: [
410+
{ in: 'header', name: 'foo', required: true, schema: { type: 'string' } },
411+
],
412+
responses: {
413+
'200': {
414+
description: 'OK',
415+
content: {
416+
'application/json': {
417+
schema: {
418+
type: 'string'
419+
}
420+
}
421+
}
422+
}
423+
}
424+
}
425+
}
426+
},
427+
components: {
428+
schemas: {}
429+
}
430+
});

0 commit comments

Comments
 (0)