Skip to content

Commit 831c67f

Browse files
starfy84Copilot
authored andcommitted
Apply suggestions from code review
Co-authored-by: Copilot <[email protected]>
1 parent 465569a commit 831c67f

File tree

3 files changed

+120
-83
lines changed

3 files changed

+120
-83
lines changed

packages/openapi-generator/src/codec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,9 @@ export function parseCodecInitializer(
510510
if (E.isRight(calleeInitE)) {
511511
const [calleeSourceFile, calleeInit] = calleeInitE.right;
512512
if (calleeInit !== null && calleeInit.type === 'ArrowFunctionExpression') {
513-
const bodyResult = parseFunctionBody(project, calleeSourceFile, calleeInit);
514-
if (E.isRight(bodyResult)) {
515-
return bodyResult;
513+
const hasNoParameters = calleeInit.params.length === 0;
514+
if (hasNoParameters) {
515+
return parseFunctionBody(project, calleeSourceFile, calleeInit);
516516
}
517517
}
518518
}

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

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import * as E from 'fp-ts/lib/Either';
2+
import assert from 'node:assert/strict';
3+
import test from 'node:test';
4+
5+
import { TestProject } from './testProject';
6+
import { parsePlainInitializer, type Schema } from '../src';
7+
import { KNOWN_IMPORTS, type KnownImports } from '../src/knownImports';
8+
9+
const ARROW_WITH_PARAMS_MAIN = `
10+
import * as t from 'io-ts';
11+
import { NonEmptyString } from './types';
12+
import { arrayFromArrayOrSingle } from './codecs';
13+
14+
export const UpdateShardKeyRequestBody = {
15+
shardKey: t.string,
16+
collectionType: t.union([
17+
t.literal('user'),
18+
t.literal('enterprise'),
19+
t.literal('enterpriseTemplate')
20+
]),
21+
ids: arrayFromArrayOrSingle(NonEmptyString),
22+
enterpriseUpdateUsers: t.boolean,
23+
} as const;
24+
`;
25+
26+
const ARROW_WITH_PARAMS_TYPES = `
27+
import * as t from 'io-ts';
28+
29+
export const NonEmptyString = t.string;
30+
`;
31+
32+
const ARROW_WITH_PARAMS_CODECS = `
33+
import * as t from 'io-ts';
34+
35+
export const arrayFromArrayOrSingle = <C extends t.Mixed>(codec: C) =>
36+
t.array(codec);
37+
`;
38+
39+
test('arrow function with parameters uses custom codec definition', async () => {
40+
const files = {
41+
'/src/main.ts': ARROW_WITH_PARAMS_MAIN,
42+
'/src/types.ts': ARROW_WITH_PARAMS_TYPES,
43+
'/src/codecs.ts': ARROW_WITH_PARAMS_CODECS,
44+
};
45+
46+
// Custom codec for arrayFromArrayOrSingle, merged with default io-ts codecs
47+
const knownImports: KnownImports = {
48+
...KNOWN_IMPORTS,
49+
'.': {
50+
...KNOWN_IMPORTS['.'],
51+
arrayFromArrayOrSingle: (_deref, innerSchema): E.Either<string, Schema> =>
52+
E.right({ type: 'array', items: innerSchema }),
53+
},
54+
};
55+
56+
const project = new TestProject(files, knownImports);
57+
await project.parseEntryPoint('/src/main.ts');
58+
const sourceFile = project.get('/src/main.ts');
59+
60+
assert.ok(sourceFile !== undefined, 'Source file not found');
61+
62+
const declaration = sourceFile.symbols.declarations.find(
63+
(s) => s.name === 'UpdateShardKeyRequestBody',
64+
);
65+
assert.ok(declaration?.init !== undefined, 'Declaration not found');
66+
67+
const result = parsePlainInitializer(project, sourceFile, declaration.init);
68+
69+
assert.ok(E.isRight(result), `Expected success, got: "${E.isLeft(result) ? result.left : ''}"`);
70+
assert.equal(result.right.type, 'object');
71+
if (result.right.type === 'object') {
72+
assert.equal(
73+
result.right.properties?.ids?.type,
74+
'array',
75+
'ids field should be array (from custom codec)',
76+
);
77+
}
78+
});
79+
80+
test('arrow function with parameters without custom codec returns ref', async () => {
81+
const files = {
82+
'/src/main.ts': `
83+
import * as t from 'io-ts';
84+
import { NonEmptyString } from './types';
85+
import { arrayFromArrayOrSingle } from './codecs';
86+
87+
export const TestBody = {
88+
ids: arrayFromArrayOrSingle(NonEmptyString),
89+
} as const;
90+
`,
91+
'/src/types.ts': ARROW_WITH_PARAMS_TYPES,
92+
'/src/codecs.ts': ARROW_WITH_PARAMS_CODECS,
93+
};
94+
95+
// No custom codec for arrayFromArrayOrSingle
96+
const project = new TestProject(files, KNOWN_IMPORTS);
97+
await project.parseEntryPoint('/src/main.ts');
98+
const sourceFile = project.get('/src/main.ts');
99+
100+
assert.ok(sourceFile !== undefined, 'Source file not found');
101+
102+
const declaration = sourceFile.symbols.declarations.find((s) => s.name === 'TestBody');
103+
assert.ok(declaration?.init !== undefined, 'Declaration not found');
104+
105+
const result = parsePlainInitializer(project, sourceFile, declaration.init);
106+
107+
// Without custom codec, should return a ref (not an error)
108+
assert.ok(E.isRight(result), `Expected success, got: "${E.isLeft(result) ? result.left : ''}"`);
109+
assert.equal(result.right.type, 'object');
110+
if (result.right.type === 'object') {
111+
assert.equal(
112+
result.right.properties?.ids?.type,
113+
'ref',
114+
'ids field should be ref (no custom codec)',
115+
);
116+
}
117+
});

0 commit comments

Comments
 (0)