Skip to content

Commit 46e5049

Browse files
committed
feat: dereference openapi specs to fix broken refs in params
1 parent 5c34ec4 commit 46e5049

File tree

3 files changed

+228
-94
lines changed

3 files changed

+228
-94
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"dotenv": "^17.2.3",
9696
"express": "^5.1.0",
9797
"express-openapi-validator": "^5.6.0",
98-
"glob": "^11.0.3",
98+
"glob": "^13.0.0",
9999
"import-in-the-middle": "^2.0.0",
100100
"minimist": "^1.2.8",
101101
"moderndash": "^4.0.0",
@@ -118,8 +118,8 @@
118118
"@types/node": "^24.10.1",
119119
"@types/request-ip": "^0.0.41",
120120
"@types/supertest": "^6.0.3",
121-
"@typescript-eslint/eslint-plugin": "^8.46.4",
122-
"@typescript-eslint/parser": "^8.46.4",
121+
"@typescript-eslint/eslint-plugin": "^8.47.0",
122+
"@typescript-eslint/parser": "^8.47.0",
123123
"cpconfig": "^1.4.4",
124124
"eslint": "^9.39.1",
125125
"eslint-config-prettier": "^10.1.8",
@@ -132,7 +132,7 @@
132132
"tsconfig-paths": "^4.2.0",
133133
"tsx": "^4.20.6",
134134
"typescript": "^5.9.3",
135-
"vitest": "^4.0.9"
135+
"vitest": "^4.0.12"
136136
},
137137
"resolutions": {
138138
"qs": "^6.11.0"

src/openapi.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ export async function openApi<
8484

8585
const defaultOptions: OAPIOpts = {
8686
apiSpec: app.locals.openApiSpecification,
87+
// We force full dereferencing of the OpenAPI spec so that all `$ref` schemas are
88+
// inlined before express-openapi-validator builds its request coercion rules.
89+
// Without this, the coercer sees `schema: { $ref: ... }` for query parameters and
90+
// cannot resolve the underlying type, so it falls back to treating the parameter
91+
// as a potentially repeated value (i.e., an array). That causes AJV to reject
92+
// valid requests with “should be array” errors. Dereferencing ensures the
93+
// coercer sees the real primitive type/enum and stops misclassifying params.
94+
//
95+
// The one downside is that circular references are not supported, so if you
96+
// need that, you need to be very careful with enums, and override this option.
97+
//
98+
// See https://github.com/cdimascio/express-openapi-validator/issues/1119
99+
$refParser: {
100+
mode: 'dereference',
101+
},
87102
ignoreUndocumented: true,
88103
validateRequests: {
89104
allowUnknownQueryParameters: true,

0 commit comments

Comments
 (0)