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
9 changes: 8 additions & 1 deletion packages/openapi-generator/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,14 @@ function parseFunctionBody(
return errorLeft('Function body is undefined');
}
if (func.body.type === 'BlockStatement') {
return errorLeft('BlockStatement arrow functions are not yet supported');
const returnStmt = func.body.stmts.find((s) => s.type === 'ReturnStatement');
if (!returnStmt || returnStmt.type !== 'ReturnStatement') {
return errorLeft('BlockStatement must contain a return statement');
}
if (!returnStmt.argument) {
return errorLeft('Return statement must have an argument');
}
return parseCodecInitializer(project, source, returnStmt.argument);
}
return parseCodecInitializer(project, source, func.body);
}
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 @@ -411,3 +411,46 @@ testCase(
},
[],
);

testCase(
'simple api spec with block statement arrow functions',
'test/sample-types/apiSpecWithBlockArrow.ts',
{
openapi: '3.0.3',
info: {
title: 'simple api spec with block statement arrow functions',
version: '1.0.0',
description: 'simple api spec with block statement arrow 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,24 @@
import * as h from '@api-ts/io-ts-http';
import * as t from 'io-ts';
import { BooleanFromString, fromNullable } from 'io-ts-types';

const BooleanFromNullableWithFallback = () => {
return 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