Skip to content

Commit 2078d09

Browse files
committed
feat: handle TS enums in openapi-generator
1 parent 2eb02c2 commit 2078d09

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

packages/openapi-generator/src/symbol.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,45 @@ function parseDeclaration(
150150
}
151151
}
152152
});
153+
} else if (subDeclaration.type === 'TsEnumDeclaration') {
154+
const comment =
155+
prev !== undefined
156+
? leadingComment(src, srcSpanStart, prev.span.end, decl.span.start)[0]
157+
: undefined;
158+
// Construct a synthetic object declaration
159+
const properties: swc.KeyValueProperty[] = subDeclaration.members.map((member) => {
160+
return {
161+
type: 'KeyValueProperty',
162+
key: {
163+
type: 'Identifier',
164+
value: member.id.value,
165+
span: member.id.span,
166+
optional: false,
167+
},
168+
value: member.init ?? {
169+
type: 'StringLiteral',
170+
value: member.id.value,
171+
span: member.id.span,
172+
},
173+
};
174+
});
175+
const syntheticObject: swc.ObjectExpression = {
176+
type: 'ObjectExpression',
177+
span: subDeclaration.span,
178+
properties,
179+
};
180+
result.declarations.push({
181+
name: subDeclaration.id.value,
182+
init: syntheticObject,
183+
comment,
184+
});
185+
if (decl.type === 'ExportDeclaration') {
186+
result.exports.push({
187+
type: 'named',
188+
exportedName: subDeclaration.id.value,
189+
localName: subDeclaration.id.value,
190+
});
191+
}
153192
}
154193
return result;
155194
}
@@ -179,7 +218,8 @@ export function parseTopLevelSymbols(
179218
symbols.imports.push(...newSyms);
180219
} else if (
181220
item.type === 'VariableDeclaration' ||
182-
item.type === 'ExportDeclaration'
221+
item.type === 'ExportDeclaration' ||
222+
item.type === 'TsEnumDeclaration'
183223
) {
184224
const newSyms = parseDeclaration(src, srcSpanStart, items[idx - 1], item);
185225
addTable(newSyms);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,33 @@ testCase('record type is parsed', RECORD, {
259259
FOO: { type: 'record', codomain: { type: 'primitive', value: 'number' } },
260260
});
261261

262+
const ENUM = `
263+
import * as t from 'io-ts';
264+
enum Foo {
265+
Foo = 'foo',
266+
Bar = 'bar',
267+
}
268+
export const TEST = t.keyof(Foo);
269+
`;
270+
271+
testCase('enum type is parsed', ENUM, {
272+
Foo: {
273+
type: 'object',
274+
properties: {
275+
Foo: { type: 'literal', kind: 'string', value: 'foo' },
276+
Bar: { type: 'literal', kind: 'string', value: 'bar' },
277+
},
278+
required: ['Foo', 'Bar'],
279+
},
280+
TEST: {
281+
type: 'union',
282+
schemas: [
283+
{ type: 'literal', kind: 'string', value: 'Foo' },
284+
{ type: 'literal', kind: 'string', value: 'Bar' },
285+
],
286+
},
287+
});
288+
262289
const STRING_LITERAL = `
263290
import * as t from 'io-ts';
264291
export const FOO = t.literal('foo');

0 commit comments

Comments
 (0)