-
Notifications
You must be signed in to change notification settings - Fork 22
test: add tests for arrow function fallback behavior #1063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+128
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/openapi-generator/test/arrowFunctionWithParameter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import * as E from 'fp-ts/lib/Either'; | ||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
|
|
||
| import { TestProject } from './testProject'; | ||
| import { parsePlainInitializer, type Schema } from '../src'; | ||
| import { KNOWN_IMPORTS, type KnownImports } from '../src/knownImports'; | ||
|
|
||
| const ARROW_WITH_PARAMS_MAIN = ` | ||
| import * as t from 'io-ts'; | ||
| import { NonEmptyString } from './types'; | ||
| import { arrayFromArrayOrSingle } from './codecs'; | ||
|
|
||
| export const UpdateShardKeyRequestBody = { | ||
| shardKey: t.string, | ||
| collectionType: t.union([ | ||
| t.literal('user'), | ||
| t.literal('enterprise'), | ||
| t.literal('enterpriseTemplate') | ||
| ]), | ||
| ids: arrayFromArrayOrSingle(NonEmptyString), | ||
| enterpriseUpdateUsers: t.boolean, | ||
| } as const; | ||
| `; | ||
|
|
||
| const ARROW_WITH_PARAMS_TYPES = ` | ||
| import * as t from 'io-ts'; | ||
|
|
||
| export const NonEmptyString = t.string; | ||
| `; | ||
|
|
||
| const ARROW_WITH_PARAMS_CODECS = ` | ||
| import * as t from 'io-ts'; | ||
|
|
||
| export const arrayFromArrayOrSingle = <C extends t.Mixed>(codec: C) => | ||
| t.array(codec); | ||
| `; | ||
|
|
||
| test('arrow function with parameters uses custom codec definition', async () => { | ||
| const files = { | ||
| '/src/main.ts': ARROW_WITH_PARAMS_MAIN, | ||
| '/src/types.ts': ARROW_WITH_PARAMS_TYPES, | ||
| '/src/codecs.ts': ARROW_WITH_PARAMS_CODECS, | ||
| }; | ||
|
|
||
| // Custom codec for arrayFromArrayOrSingle, merged with default io-ts codecs | ||
| const knownImports: KnownImports = { | ||
| ...KNOWN_IMPORTS, | ||
| '.': { | ||
| ...KNOWN_IMPORTS['.'], | ||
| arrayFromArrayOrSingle: (_deref, innerSchema): E.Either<string, Schema> => | ||
| E.right({ type: 'array', items: innerSchema }), | ||
| }, | ||
| }; | ||
|
|
||
| const project = new TestProject(files, knownImports); | ||
| await project.parseEntryPoint('/src/main.ts'); | ||
| const sourceFile = project.get('/src/main.ts'); | ||
|
|
||
| assert.ok(sourceFile !== undefined, 'Source file not found'); | ||
|
|
||
| const declaration = sourceFile.symbols.declarations.find( | ||
| (s) => s.name === 'UpdateShardKeyRequestBody', | ||
| ); | ||
| assert.ok(declaration?.init !== undefined, 'Declaration not found'); | ||
|
|
||
| const result = parsePlainInitializer(project, sourceFile, declaration.init); | ||
|
|
||
| assert.ok( | ||
| E.isRight(result), | ||
| `Expected success, got: "${E.isLeft(result) ? result.left : ''}"`, | ||
| ); | ||
| assert.equal(result.right.type, 'object'); | ||
| if (result.right.type === 'object') { | ||
| assert.equal( | ||
| result.right.properties?.ids?.type, | ||
| 'array', | ||
| 'ids field should be array (from custom codec)', | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test('arrow function with parameters without custom codec returns ref', async () => { | ||
| const files = { | ||
| '/src/main.ts': ` | ||
| import * as t from 'io-ts'; | ||
| import { NonEmptyString } from './types'; | ||
| import { arrayFromArrayOrSingle } from './codecs'; | ||
|
|
||
| export const TestBody = { | ||
| ids: arrayFromArrayOrSingle(NonEmptyString), | ||
| } as const; | ||
| `, | ||
| '/src/types.ts': ARROW_WITH_PARAMS_TYPES, | ||
| '/src/codecs.ts': ARROW_WITH_PARAMS_CODECS, | ||
| }; | ||
|
|
||
| // No custom codec for arrayFromArrayOrSingle | ||
| const project = new TestProject(files, KNOWN_IMPORTS); | ||
| await project.parseEntryPoint('/src/main.ts'); | ||
| const sourceFile = project.get('/src/main.ts'); | ||
|
|
||
| assert.ok(sourceFile !== undefined, 'Source file not found'); | ||
|
|
||
| const declaration = sourceFile.symbols.declarations.find( | ||
| (s) => s.name === 'TestBody', | ||
| ); | ||
| assert.ok(declaration?.init !== undefined, 'Declaration not found'); | ||
|
|
||
| const result = parsePlainInitializer(project, sourceFile, declaration.init); | ||
|
|
||
| // Without custom codec, should return a ref (not an error) | ||
| assert.ok( | ||
| E.isRight(result), | ||
| `Expected success, got: "${E.isLeft(result) ? result.left : ''}"`, | ||
| ); | ||
| assert.equal(result.right.type, 'object'); | ||
| if (result.right.type === 'object') { | ||
| assert.equal( | ||
| result.right.properties?.ids?.type, | ||
| 'ref', | ||
| 'ids field should be ref (no custom codec)', | ||
| ); | ||
| } | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm understanding this correctly;
parseFunctionBodyfails on functions with parameters. The fix is to only parse the function body if it has no parameters.My question:
Is the fact that
parseFunctionBodyfails on arrow functions with parameters a flaw with the way we definedparseFunctionBody? Or is this intended behaviour?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is intended that
parseFunctionBodyonly handles zero-parameter functions, since that was the case that surfaced the need for function. Arrow functions with parameters are left to be handled via custom codec definitions in knownImports, which I believe manually defines the schema transformation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for answering my questions!