Skip to content

Comments

fix: correct ajvFilePlugin type to be compatible with Fastify ajv.plugins#606

Open
nahyeongjin1 wants to merge 10 commits intofastify:mainfrom
nahyeongjin1:fix/ajv-file-plugin-type
Open

fix: correct ajvFilePlugin type to be compatible with Fastify ajv.plugins#606
nahyeongjin1 wants to merge 10 commits intofastify:mainfrom
nahyeongjin1:fix/ajv-file-plugin-type

Conversation

@nahyeongjin1
Copy link

Summary

  • Fix ajvFilePlugin type definition to be compatible with Fastify's ajv.plugins option
  • Add ajv to devDependencies for type definitions
  • Change return type from void to Ajv

Problem

The ajvFilePlugin was typed as (ajv: any): void, but Fastify's ajv.plugins expects Plugin<unknown> which requires returning Ajv.

Solution

// Before
export function ajvFilePlugin (ajv: any): void

// After
import type Ajv from 'ajv'
export function ajvFilePlugin (ajv: Ajv): Ajv

Test plan

  • npm run test:typescript passes
  • Verified fix resolves the type error when using with Fastify

Fixes #605

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you ads a test for this type? We use tsd.

@nahyeongjin1
Copy link
Author

Added type test in types/avj-plugin.test-d.ts with expectType<Ajv> assertion.

Note: The tsd tests are located in types/ directory (not test/), as the test/ folder contains only JS unit tests.

Also noticed the filename has a typo: avj-plugin.test-d.ts should be ajv-plugin.test-d.ts. Would you like me to rename it in this PR as well?

@nahyeongjin1 nahyeongjin1 requested a review from mcollina February 3, 2026 06:57
@mrazauskas
Copy link

The tsd tests are located in types/ directory (not test/), as the test/ folder contains only JS unit tests.

This also means tsd checks only the index.test-d.ts file.

I think the best fix would be to follow the pattern from other fastify packages:

  • move all type test files to the test/types directory (fix the typo in the file name as well)
  • set the package.json#tsd.directory field to "test/types"

@nahyeongjin1
Copy link
Author

Thanks @mrazauskas for the suggestion!

Done. Moved all type test files to test/types/ directory and fixed the typo (avjajv).

The tsd.directory is already set to test, which includes subdirectories, so no config change needed.

@mrazauskas
Copy link

Thanks! I checked out this branch. All works as expected.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeScript type incompatibility between ajvFilePlugin and Fastify's ajv.plugins option. The plugin was incorrectly typed as returning void when it should return Ajv to satisfy Fastify's Plugin<unknown> interface requirement.

Changes:

  • Updated ajvFilePlugin type signature to accept Ajv instead of any and return Ajv instead of void
  • Added ajv v8.17.1 to devDependencies for type definitions
  • Fixed test file organization by moving from types/ to test/types/ and correcting a filename typo ("avj" → "ajv")

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
types/index.d.ts Updated ajvFilePlugin type signature and added Ajv type import
package.json Added ajv to devDependencies for type definitions
test/types/ajv-plugin.test-d.ts New comprehensive test file for ajvFilePlugin compatibility with proper path
types/avj-plugin.test-d.ts Removed old test file with typo in filename
test/types/named-import.test-d.ts Updated import path from '..' to '../..' to reflect new test location
test/types/index.test-d.ts Updated import path from '..' to '../..' to reflect new test location

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

mcollina commented Feb 9, 2026

Ci looks red

@nahyeongjin1 nahyeongjin1 force-pushed the fix/ajv-file-plugin-type branch 3 times, most recently from 751fbec to 431f3ee Compare February 10, 2026 02:37
@nahyeongjin1
Copy link
Author

nahyeongjin1 commented Feb 10, 2026

Fixed the CI failures:

  1. Node.js 22+ test runner was auto-discovering .test-d.ts files in test/ and trying to execute them as regular tests.
    Fixed by specifying an explicit glob pattern: node --test 'test/*.test.js'

  2. tsd.directory: Narrowed from test to test/types for more precise targeting, as @mrazauskas suggested.

  3. Swagger schema assertion: @fastify/swagger v9.7.0 introduced requestBody.required when schema.body exists fix(openapi): always set requestBody.required to true when schema.body exists fastify-swagger#903, making the full paths assertion version-dependent.
    Changed the test to only verify ajvFilePlugin's core behavior (isFile: true{ type: 'string', format: 'binary' }), which is what this test should be asserting regardless of swagger version.

Could you re-run the CI? Thanks!

* requestBody.required (fastify/fastify-swagger#903), making the exact
* shape version-dependent.
*/
// t.assert.deepStrictEqual(fastify.swagger().paths, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of commenting it out, could you check other parts of the requestBody like the content?
Additionally, you should be able also to check the operation id.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nahyeongjin1
nit: shouldn't you remove this huge comment and keep just the one below?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed the commented-out block 👍

@nahyeongjin1
Copy link
Author

@ilteoood
Done. Now asserting operationId, requestBody.content, and responses individually.

requestBody.required is intentionally skipped,
because @fastify/swagger v9.7.0 introduced it (fastify/fastify-swagger#903), making it version-dependent.

@nahyeongjin1
Copy link
Author

Fixed the Node.js 20 CI failure: glob quotes in node --test 'test/*.test.js' were treated as a literal path.
Removed quotes so the shell handles glob expansion.

Verified all tests pass on Node.js 20, 22, and 24.

ilteoood
ilteoood previously approved these changes Feb 20, 2026
@ilteoood ilteoood dismissed their stale review February 20, 2026 07:57

CI still red

@nahyeongjin1
Copy link
Author

nahyeongjin1 commented Feb 20, 2026

@ilteoood
The CI failure on Node.js 22 + macOS (should finish with error on partial upload - saveRequestFiles) is unrelated to this PR.
It's a flaky test caused by a race condition.
The test checks temp file deletion immediately after receiving the response, but cleanup may not have completed yet on macOS.

I've added a fix using exponential backoff retry for the file deletion check.
Since this is outside the scope of this PR, I can either include it here or open a separate PR for it. Let me know which you'd prefer.

…gins

The ajvFilePlugin return type was incorrectly declared as void instead of Ajv.This caused TypeScript errors when using the plugin with Fastify ajv.plugins option

Changes:
- Add ajv to devDependencies for type definitions
- Import Ajv type in types/index.d.ts
- Change ajvFilePlugin signature from (ajv: any): void to (ajv: Ajv): Ajv

Fixes fastify#605
Added expectType<Ajv> assertion to verify ajvFilePlugin correctly returns Ajv instance.
- Move type test files from types/ to test/types/
- Fix filename typo: avj-plugin.test-d.ts -> ajv-plugin.test-d.ts
- Update import paths from '..' to '../..'
Node.js 22+ test runner auto-discovers .ts files in the test/ directory,
causing tsd type test files (.test-d.ts) to be executed as regular tests.

Changes:
- Specify explicit glob pattern to only run .test.js files
- Narrow tsd directory from test/ to test/types/ for precise targeting
- Update swagger schema assertion to include requestBody.required field
  added by newer @fastify/swagger versions
Only verify ajvFilePlugin's core behavior (isFile -> string/binary)
instead of the full paths object. The requestBody.required field was
introduced in @fastify/swagger v9.7.0 (fastify/fastify-swagger#903),
making the exact shape version-dependent.
@nahyeongjin1 nahyeongjin1 force-pushed the fix/ajv-file-plugin-type branch from 74f4f71 to 6fee76d Compare February 23, 2026 05:00
@nahyeongjin1
Copy link
Author

Rebased on main.
Note that #607 added required: true to the full deepStrictEqual assertion.
But this PR takes a different approach — asserting operationId, requestBody.content, and responses individually, intentionally skipping requestBody.required since it's version-dependent (@fastify/swagger v9.7.0+).

If you'd prefer the #607 approach (full assertion with required: true), let me know and I'll revert to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: ajvFilePlugin type incompatible with Fastify's ajv.plugins option

4 participants