Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't explode header params #989

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {}
}
});
Loading