Skip to content
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
37 changes: 36 additions & 1 deletion packages/openapi-generator/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,20 @@ export function parsePlainInitializer(
}
}

function parseFunctionBody(
project: Project,
source: SourceFile,
func: swc.ArrowFunctionExpression,
): E.Either<string, Schema> {
if (func.body === undefined) {
return errorLeft('Function body is undefined');
}
if (func.body.type === 'BlockStatement') {
return errorLeft('BlockStatement arrow functions are not yet supported');
}
return parseCodecInitializer(project, source, func.body);
}

export function parseCodecInitializer(
project: Project,
source: SourceFile,
Expand All @@ -471,8 +485,29 @@ export function parseCodecInitializer(
} else if (init.type === 'CallExpression') {
const callee = init.callee;
if (callee.type !== 'Identifier' && callee.type !== 'MemberExpression') {
return errorLeft(`Unimplemented callee type ${init.callee.type}`);
return errorLeft(`Unimplemented callee type ${callee.type}`);
}

let calleeName: string | [string, string] | undefined;
if (callee.type === 'Identifier') {
calleeName = callee.value;
} else if (
callee.object.type === 'Identifier' &&
callee.property.type === 'Identifier'
) {
calleeName = [callee.object.value, callee.property.value];
}

if (calleeName !== undefined) {
const calleeInitE = findSymbolInitializer(project, source, calleeName);
if (E.isRight(calleeInitE)) {
const [calleeSourceFile, calleeInit] = calleeInitE.right;
if (calleeInit !== null && calleeInit.type === 'ArrowFunctionExpression') {
return parseFunctionBody(project, calleeSourceFile, calleeInit);
}
}
}

const identifierE = codecIdentifier(project, source, callee);
if (E.isLeft(identifierE)) {
return identifierE;
Expand Down
43 changes: 43 additions & 0 deletions packages/openapi-generator/test/externalModuleApiSpec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,46 @@ testCase(
},
[],
);

testCase(
'simple api spec with util type functions',
'test/sample-types/apiSpecWithArrow.ts',
{
openapi: '3.0.3',
info: {
title: 'simple api spec with util type functions',
version: '1.0.0',
description: 'simple api spec with util type functions',
},
paths: {
'/test': {
get: {
parameters: [],
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
hasLargeNumberOfAddresses: {
nullable: true,
type: 'boolean',
},
},
required: ['hasLargeNumberOfAddresses'],
},
},
},
},
},
},
},
},
components: {
schemas: {},
},
},
[],
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as h from '@api-ts/io-ts-http';
import * as t from 'io-ts';
import { BooleanFromString, fromNullable } from 'io-ts-types';

const BooleanFromNullableWithFallback = () =>
fromNullable(t.union([BooleanFromString, t.boolean]), false);

export const TEST_ROUTE = h.httpRoute({
path: '/test',
method: 'GET',
request: h.httpRequest({}),
response: {
200: t.type({
hasLargeNumberOfAddresses: BooleanFromNullableWithFallback(),
}),
},
});

export const apiSpec = h.apiSpec({
'api.test': {
get: TEST_ROUTE,
},
});
Loading