Skip to content

Commit 9fcd9b9

Browse files
starfy84Copilot
authored andcommitted
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 465569a commit 9fcd9b9

File tree

3 files changed

+128
-83
lines changed

3 files changed

+128
-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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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(
70+
E.isRight(result),
71+
`Expected success, got: "${E.isLeft(result) ? result.left : ''}"`,
72+
);
73+
assert.equal(result.right.type, 'object');
74+
if (result.right.type === 'object') {
75+
assert.equal(
76+
result.right.properties?.ids?.type,
77+
'array',
78+
'ids field should be array (from custom codec)',
79+
);
80+
}
81+
});
82+
83+
test('arrow function with parameters without custom codec returns ref', async () => {
84+
const files = {
85+
'/src/main.ts': `
86+
import * as t from 'io-ts';
87+
import { NonEmptyString } from './types';
88+
import { arrayFromArrayOrSingle } from './codecs';
89+
90+
export const TestBody = {
91+
ids: arrayFromArrayOrSingle(NonEmptyString),
92+
} as const;
93+
`,
94+
'/src/types.ts': ARROW_WITH_PARAMS_TYPES,
95+
'/src/codecs.ts': ARROW_WITH_PARAMS_CODECS,
96+
};
97+
98+
// No custom codec for arrayFromArrayOrSingle
99+
const project = new TestProject(files, KNOWN_IMPORTS);
100+
await project.parseEntryPoint('/src/main.ts');
101+
const sourceFile = project.get('/src/main.ts');
102+
103+
assert.ok(sourceFile !== undefined, 'Source file not found');
104+
105+
const declaration = sourceFile.symbols.declarations.find(
106+
(s) => s.name === 'TestBody',
107+
);
108+
assert.ok(declaration?.init !== undefined, 'Declaration not found');
109+
110+
const result = parsePlainInitializer(project, sourceFile, declaration.init);
111+
112+
// Without custom codec, should return a ref (not an error)
113+
assert.ok(
114+
E.isRight(result),
115+
`Expected success, got: "${E.isLeft(result) ? result.left : ''}"`,
116+
);
117+
assert.equal(result.right.type, 'object');
118+
if (result.right.type === 'object') {
119+
assert.equal(
120+
result.right.properties?.ids?.type,
121+
'ref',
122+
'ids field should be ref (no custom codec)',
123+
);
124+
}
125+
});

0 commit comments

Comments
 (0)