Skip to content

fix: make computed property support more robust #943

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

Merged
merged 1 commit into from
Nov 8, 2024
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
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'],
},
});