Skip to content

Commit 1795aed

Browse files
authored
Merge pull request #943 from BitGo/expand-computed-property-support
fix: make computed property support more robust
2 parents 9d426e4 + 04cda61 commit 1795aed

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

packages/openapi-generator/src/codec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -239,26 +239,26 @@ function parseObjectExpression(
239239

240240
let name: string = '';
241241
if (property.key.type === 'Computed') {
242-
if (property.key.expression.type !== 'Identifier') {
243-
return errorLeft(
244-
`Unimplemented computed property value type ${property.value.type}`,
245-
);
246-
}
247-
248-
const initE = findSymbolInitializer(
249-
project,
250-
source,
251-
property.key.expression.value,
252-
);
253-
if (E.isLeft(initE)) {
254-
return initE;
255-
}
256-
const [newSourceFile, init] = initE.right;
257-
const valueE = parsePlainInitializer(project, newSourceFile, init);
242+
const valueE = parseCodecInitializer(project, source, property.key.expression);
258243
if (E.isLeft(valueE)) {
259244
return valueE;
260245
}
261-
const schema = valueE.right;
246+
let schema = valueE.right;
247+
if (schema.type === 'ref') {
248+
const realInitE = findSymbolInitializer(project, source, schema.name);
249+
if (E.isLeft(realInitE)) {
250+
return realInitE;
251+
}
252+
const schemaE = parsePlainInitializer(
253+
project,
254+
realInitE.right[0],
255+
realInitE.right[1],
256+
);
257+
if (E.isLeft(schemaE)) {
258+
return schemaE;
259+
}
260+
schema = schemaE.right;
261+
}
262262
if (
263263
(schema.type === 'string' || schema.type === 'number') &&
264264
schema.enum !== undefined

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,12 @@ testCase('object assign is parsed', OBJECT_ASSIGN, {
852852
const COMPUTED_PROPERTY = `
853853
import * as t from 'io-ts';
854854
const key = 'foo';
855+
const obj = {
856+
bar: 'bar',
857+
}
855858
export const FOO = t.type({
856859
[key]: t.number,
860+
[obj.bar]: t.string,
857861
});
858862
`;
859863

@@ -862,11 +866,19 @@ testCase('computed property is parsed', COMPUTED_PROPERTY, {
862866
type: 'object',
863867
properties: {
864868
foo: { type: 'number', primitive: true },
869+
bar: { type: 'string', primitive: true },
865870
},
866-
required: ['foo'],
871+
required: ['foo', 'bar'],
867872
},
868873
key: {
869874
type: 'string',
870875
enum: ['foo'],
871-
}
876+
},
877+
obj: {
878+
type: 'object',
879+
properties: {
880+
bar: { type: 'string', enum: ['bar'] },
881+
},
882+
required: ['bar'],
883+
},
872884
});

0 commit comments

Comments
 (0)