Skip to content

Commit

Permalink
Merge pull request #989 from BitGo/dont-explode-headers
Browse files Browse the repository at this point in the history
Don't explode header params
  • Loading branch information
ericcrosson-bitgo authored Jan 17, 2025
2 parents 75d1efa + e7011e3 commit 45f5fcb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
26 changes: 19 additions & 7 deletions packages/openapi-generator/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,25 @@ function parseRequestUnion(
});
}
if (headerSchema.schemas.length > 0) {
parameters.push({
type: 'header',
name: 'union',
explode: true,
required: true,
schema: headerSchema,
});
// For headers in unions, take properties from first schema that has headers
// Also not perfect but we cannot use the `explode: true` trick for headers
const firstHeaderSchema = schema.schemas.find(
(s) => s.type === 'object' && s.properties['headers']?.type === 'object',
);
if (
firstHeaderSchema?.type === 'object' &&
firstHeaderSchema.properties['headers']?.type === 'object'
) {
const headers = firstHeaderSchema.properties['headers'];
for (const [name, prop] of Object.entries(headers.properties)) {
parameters.push({
type: 'header',
name,
schema: prop,
required: headers.required.includes(name),
});
}
}
}

const firstSubSchema = schema.schemas[0];
Expand Down
56 changes: 55 additions & 1 deletion packages/openapi-generator/test/openapi/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,58 @@ testCase("route with unknown unions", ROUTE_WITH_UNKNOWN_UNIONS, {
}
}
},
});
});

const ROUTE_WITH_REQUEST_UNION = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
import { BooleanFromString, BooleanFromNumber, NumberFromString } from 'io-ts-types';
export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: t.union([
h.httpRequest({
headers: {
foo: t.string,
},
}),
h.httpRequest({}),
]),
response: {
200: t.string,
},
});
`;

testCase("route with request union", ROUTE_WITH_REQUEST_UNION, {
info: {
title: 'Test',
version: '1.0.0'
},
openapi: '3.0.3',
paths: {
'/foo': {
get: {
parameters: [
{ in: 'header', name: 'foo', required: true, schema: { type: 'string' } },
],
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'string'
}
}
}
}
}
}
}
},
components: {
schemas: {}
}
});

0 comments on commit 45f5fcb

Please sign in to comment.