Skip to content

Commit

Permalink
fix: make computed property support more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
bitgopatmcl committed Nov 5, 2024
1 parent dd2c671 commit 04cda61
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
34 changes: 17 additions & 17 deletions packages/openapi-generator/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,26 @@ function parseObjectExpression(

let name: string = '';
if (property.key.type === 'Computed') {
if (property.key.expression.type !== 'Identifier') {
return errorLeft(
`Unimplemented computed property value type ${property.value.type}`,
);
}

const initE = findSymbolInitializer(
project,
source,
property.key.expression.value,
);
if (E.isLeft(initE)) {
return initE;
}
const [newSourceFile, init] = initE.right;
const valueE = parsePlainInitializer(project, newSourceFile, init);
const valueE = parseCodecInitializer(project, source, property.key.expression);
if (E.isLeft(valueE)) {
return valueE;
}
const schema = valueE.right;
let schema = valueE.right;
if (schema.type === 'ref') {
const realInitE = findSymbolInitializer(project, source, schema.name);
if (E.isLeft(realInitE)) {
return realInitE;
}
const schemaE = parsePlainInitializer(
project,
realInitE.right[0],
realInitE.right[1],
);
if (E.isLeft(schemaE)) {
return schemaE;
}
schema = schemaE.right;
}
if (
(schema.type === 'string' || schema.type === 'number') &&
schema.enum !== undefined
Expand Down
16 changes: 14 additions & 2 deletions packages/openapi-generator/test/codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,12 @@ testCase('object assign is parsed', OBJECT_ASSIGN, {
const COMPUTED_PROPERTY = `
import * as t from 'io-ts';
const key = 'foo';
const obj = {
bar: 'bar',
}
export const FOO = t.type({
[key]: t.number,
[obj.bar]: t.string,
});
`;

Expand All @@ -862,11 +866,19 @@ testCase('computed property is parsed', COMPUTED_PROPERTY, {
type: 'object',
properties: {
foo: { type: 'number', primitive: true },
bar: { type: 'string', primitive: true },
},
required: ['foo'],
required: ['foo', 'bar'],
},
key: {
type: 'string',
enum: ['foo'],
}
},
obj: {
type: 'object',
properties: {
bar: { type: 'string', enum: ['bar'] },
},
required: ['bar'],
},
});

0 comments on commit 04cda61

Please sign in to comment.