Skip to content

Commit 23f8d3a

Browse files
author
Carmine DiMascio
committed
allowEmptyValue flag is ignored #190
1 parent 7cf9c06 commit 23f8d3a

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed

src/middlewares/openapi.request.validator.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class RequestValidator {
115115
mutator.modifyRequest(req);
116116

117117
if (!this.requestOpts.allowUnknownQueryParameters) {
118-
this.rejectUnknownQueryParams(
118+
this.processQueryParam(
119119
req.query,
120120
schema.properties.query,
121121
securityQueryParam,
@@ -141,22 +141,28 @@ export class RequestValidator {
141141
};
142142
}
143143

144-
private rejectUnknownQueryParams(
145-
query,
146-
schema,
147-
whiteList: string[] = [],
148-
): void {
144+
private processQueryParam(query, schema, whiteList: string[] = []) {
149145
if (!schema.properties) return;
150146
const knownQueryParams = new Set(Object.keys(schema.properties));
151147
whiteList.forEach(item => knownQueryParams.add(item));
152148
const queryParams = Object.keys(query);
149+
const allowedEmpty = schema.allowEmptyValue;
153150
for (const q of queryParams) {
154-
if (!knownQueryParams.has(q)) {
151+
if (
152+
!this.requestOpts.allowUnknownQueryParameters &&
153+
!knownQueryParams.has(q)
154+
) {
155155
throw validationError(
156156
400,
157157
`.query.${q}`,
158158
`Unknown query parameter ${q}`,
159159
);
160+
} else if (!allowedEmpty?.has(q) && (query[q] === '' || null)) {
161+
throw validationError(
162+
400,
163+
`.query.${q}`,
164+
`query parameter ${q} has empty value`,
165+
);
160166
}
161167
}
162168
}

src/middlewares/parsers/schema.parse.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export class ParametersSchemaParser {
4747
}
4848

4949
schemas[reqField].properties[name] = schema;
50+
if (reqField === 'query' && parameter.allowEmptyValue) {
51+
if (!schemas[reqField].allowEmptyValue) {
52+
schemas[reqField].allowEmptyValue = new Set<string>();
53+
}
54+
schemas[reqField].allowEmptyValue.add(name);
55+
}
5056
if (parameter.required) {
5157
if (!schemas[reqField].required) {
5258
schemas[reqField].required = [];

test/query.params.allow.unknown.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe(packageJson.name, () => {
3232
request(app)
3333
.get(`${app.basePath}/pets`)
3434
.query({
35+
name: 'max',
3536
tags: 'one,two,three',
3637
limit: 10,
3738
breed: 'german_shepherd',
@@ -43,6 +44,7 @@ describe(packageJson.name, () => {
4344
request(app)
4445
.get(`${app.basePath}/pets`)
4546
.query({
47+
name: 'max',
4648
tags: 'one,two,three',
4749
limit: 10,
4850
breed: 'german_shepherd',

test/query.params.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe(packageJson.name, () => {
2929
request(app)
3030
.get(`${app.basePath}/pets`)
3131
.query({
32+
name: 'max',
3233
tags: 'one,two,three',
3334
limit: 10,
3435
breed: 'german_shepherd',
@@ -40,6 +41,7 @@ describe(packageJson.name, () => {
4041
request(app)
4142
.get(`${app.basePath}/pets`)
4243
.query({
44+
name: 'max',
4345
tags: 'one,two,three',
4446
limit: 10,
4547
breed: 'german_shepherd',
@@ -50,4 +52,37 @@ describe(packageJson.name, () => {
5052
.then(r => {
5153
expect(r.body.errors).to.be.an('array');
5254
}));
55+
56+
it('should not allow empty query param value', async () =>
57+
request(app)
58+
.get(`${app.basePath}/pets`)
59+
.query({
60+
name: 'max',
61+
tags: 'one,two,three',
62+
limit: 10,
63+
breed: '',
64+
owner_name: 'carmine',
65+
})
66+
.expect(400)
67+
.then(r => {
68+
expect(r.body)
69+
.to.have.property('message')
70+
.that.equals('query parameter breed has empty value');
71+
expect(r.body.errors)
72+
.to.be.an('array')
73+
.with.length(1);
74+
expect(r.body.errors[0].path).to.equal('.query.breed');
75+
}));
76+
77+
it('should allow empty query param value with allowEmptyValue: true', async () =>
78+
request(app)
79+
.get(`${app.basePath}/pets`)
80+
.query({
81+
name: '',
82+
tags: 'one,two,three',
83+
limit: 10,
84+
breed: 'german_shepherd',
85+
owner_name: 'carmine',
86+
})
87+
.expect(200));
5388
});

test/resources/query.params.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ paths:
3535
description: |
3636
Returns all pets from the system that the user has access tp
3737
parameters:
38+
- name: name
39+
in: query
40+
description: name
41+
required: true
42+
schema:
43+
type: string
44+
allowEmptyValue: true
3845
- name: tags
3946
in: query
4047
description: tags to filter by

0 commit comments

Comments
 (0)