Skip to content

fix: Add Ajv type coercion for Express 5 query parameter compatibility#1388

Merged
JamieMagee merged 3 commits intoclearlydefined:masterfrom
elaine-mattos:feat/express5-migration-query-coercion
Jan 30, 2026
Merged

fix: Add Ajv type coercion for Express 5 query parameter compatibility#1388
JamieMagee merged 3 commits intoclearlydefined:masterfrom
elaine-mattos:feat/express5-migration-query-coercion

Conversation

@elaine-mattos
Copy link
Copy Markdown
Contributor

@elaine-mattos elaine-mattos commented Jan 2, 2026

Description

This PR addresses query parameter type handling changes introduced by Express 5. (see #1340)

Express 5 introduced a breaking change where req.query is now a read-only getter instead of a writable property. This means that custom middleware can no longer reassign req.query to convert types, causing schema validation to fail when expecting any type but a string.

The solution uses Ajv's built-in coerceTypes feature to handle type conversion during schema validation.

Changes made

1 - Enabled Ajv type coercion: allows for automatic type conversion of query string values;
2 - Removed custom querystring middleware and its tests: no longer needed since Ajv handles type coercion;

@elaine-mattos elaine-mattos changed the title feat: Add Ajv type coercion for Express 5 query parameter compatibility fix: Add Ajv type coercion for Express 5 query parameter compatibility Jan 2, 2026
@JamieMagee JamieMagee force-pushed the feat/express5-migration-query-coercion branch from f8d5bcc to f21fc58 Compare January 14, 2026 19:37
Copy link
Copy Markdown
Contributor

@JamieMagee JamieMagee left a comment

Choose a reason for hiding this comment

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

LGTM!

If you want, I think you can also simplify some of the defensive code. definitions-1.0.0.js can be simplified from const force = req.query.force === true || req.query.force === 'true' to const force = req.query.force === true and definitions.js can be simplified from const matchCasing = !(request.query.matchCasing === 'false' || request.query.matchCasing === false) to const matchCasing = request.query.matchCasing === true

@JamieMagee JamieMagee force-pushed the feat/express5-migration-query-coercion branch from f21fc58 to d6d4868 Compare January 30, 2026 18:10
Primitives like null, numbers, and booleans are now coerced to strings,
so tests need to use objects/arrays to trigger type validation errors.
@JamieMagee
Copy link
Copy Markdown
Contributor

Fixed the failing tests. With coerceTypes: true, Ajv converts primitives (null, numbers, booleans) to strings automatically, so the old test cases were passing validation instead of failing. Updated the tests to use objects/arrays instead, which can't be coerced and still trigger the expected type errors.

@JamieMagee JamieMagee merged commit 0c8545d into clearlydefined:master Jan 30, 2026
4 checks passed
JamieMagee added a commit that referenced this pull request Mar 14, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
JamieMagee added a commit that referenced this pull request Mar 14, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
JamieMagee added a commit that referenced this pull request Mar 22, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants