diff --git a/packages/openapi-generator/src/codec.ts b/packages/openapi-generator/src/codec.ts index 79e39dda..e58d2c46 100644 --- a/packages/openapi-generator/src/codec.ts +++ b/packages/openapi-generator/src/codec.ts @@ -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 diff --git a/packages/openapi-generator/test/codec.test.ts b/packages/openapi-generator/test/codec.test.ts index 6955c7d3..c5d28b52 100644 --- a/packages/openapi-generator/test/codec.test.ts +++ b/packages/openapi-generator/test/codec.test.ts @@ -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, }); `; @@ -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'], + }, });