Skip to content

Commit 10519ec

Browse files
Update fix to changes in master.
1 parent e4a67e3 commit 10519ec

File tree

3 files changed

+99
-24
lines changed

3 files changed

+99
-24
lines changed

src/middlewares/parsers/schema.preprocessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class SchemaPreprocessor {
199199
const child = new Node(node, s, [...node.path, 'anyOf', i + '']);
200200
recurse(node, child, opts);
201201
});
202-
} else if (/*schema.type == 'array' && */ schema.items) {
202+
} else if (schema.type === 'array' && schema.items) {
203203
const child = new Node(node, schema.items, [...node.path, 'items']);
204204
recurse(node, child, opts);
205205
} else if (schema.properties) {
@@ -323,7 +323,7 @@ export class SchemaPreprocessor {
323323
...(o.properties ?? {}),
324324
...(newSchema.properties ?? {}),
325325
};
326-
if(Object.keys(newProperties).length > 0) {
326+
if (Object.keys(newProperties).length > 0) {
327327
newSchema.properties = newProperties;
328328
}
329329

test/699.spec.ts

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createApp } from './common/app';
55

66
import { date, dateTime } from '../src/framework/base.serdes';
77

8-
const apiSpecPath = path.join('test', '699.yaml');
8+
const apiSpecPath = path.join('test', 'resources', '699.yaml');
99

1010
class ObjectID {
1111
id: string;
@@ -62,6 +62,7 @@ describe('699', () => {
6262
id: req.params.id,
6363
creationDateTime: date,
6464
creationDate: date,
65+
shortOrLong: 'a',
6566
history: [{
6667
modificationDate: date
6768
}],
@@ -80,7 +81,6 @@ describe('699', () => {
8081
res.json(req.body);
8182
});
8283
app.use((err, req, res, next) => {
83-
console.error(err)
8484
res.status(err.status ?? 500).json({
8585
message: err.message,
8686
code: err.status ?? 500,
@@ -101,7 +101,7 @@ describe('699', () => {
101101
.get(`${app.basePath}/users/1234`)
102102
.expect(400)
103103
.then((r) => {
104-
expect(r.body.message).to.equal('request.params.id should match pattern "^[0-9a-fA-F]{24}$"');
104+
expect(r.body.message).to.equal('request/params/id must match pattern "^[0-9a-fA-F]{24}$"');
105105
}));
106106

107107
it('should control GOOD id format and get a response in expected format', async () =>
@@ -121,7 +121,8 @@ describe('699', () => {
121121
.send({
122122
id: '5fdefd13a6640bb5fb5fa925',
123123
creationDateTime: '2020-12-20T07:28:19.213Z',
124-
creationDate: '2020-12-20'
124+
creationDate: '2020-12-20',
125+
shortOrLong: 'ab',
125126
})
126127
.set('Content-Type', 'application/json')
127128
.expect(200)
@@ -137,12 +138,13 @@ describe('699', () => {
137138
.send({
138139
id: '5fdefd13a6640bb5fb5fa',
139140
creationDateTime: '2020-12-20T07:28:19.213Z',
140-
creationDate: '2020-12-20'
141+
creationDate: '2020-12-20',
142+
shortOrLong: 'abcd',
141143
})
142144
.set('Content-Type', 'application/json')
143145
.expect(400)
144146
.then((r) => {
145-
expect(r.body.message).to.equal('request.body.id should match pattern "^[0-9a-fA-F]{24}$"');
147+
expect(r.body.message).to.equal('request/body/id must match pattern "^[0-9a-fA-F]{24}$"');
146148
}));
147149

148150
it('should POST throw error on invalid schema Date', async () =>
@@ -156,9 +158,29 @@ describe('699', () => {
156158
.set('Content-Type', 'application/json')
157159
.expect(400)
158160
.then((r) => {
159-
expect(r.body.message).to.equal('request.body.creationDate should match format "date"');
161+
expect(r.body.message).to.equal('request/body/creationDate must match format "date"');
160162
}));
161163

164+
it('should enforce anyOf validations', async () =>
165+
request(app)
166+
.post(`${app.basePath}/users`)
167+
.send({
168+
id: '5fdefd13a6640bb5fb5fa925',
169+
creationDateTime: '2020-12-20T07:28:19.213Z',
170+
creationDate: '2020-12-20',
171+
shortOrLong: 'abc',
172+
})
173+
.set('Content-Type', 'application/json')
174+
.expect(400)
175+
.then((r) => {
176+
expect(r.body.message).to.equal(
177+
[
178+
'request/body/shortOrLong must NOT have more than 2 characters',
179+
'request/body/shortOrLong must NOT have fewer than 4 characters',
180+
'request/body/shortOrLong must match a schema in anyOf',
181+
].join(', '),
182+
);
183+
}));
162184
});
163185

164186

@@ -198,6 +220,7 @@ describe('699 serialize response components only', () => {
198220
id: new ObjectID(req.params.id),
199221
creationDateTime: date,
200222
creationDate: undefined,
223+
shortOrLong: 'a',
201224
history: [{
202225
modificationDate: date,
203226
}],
@@ -229,7 +252,6 @@ describe('699 serialize response components only', () => {
229252
res.json(req.body);
230253
});
231254
app.use((err, req, res, next) => {
232-
console.error(err)
233255
res.status(err.status ?? 500).json({
234256
message: err.message,
235257
code: err.status ?? 500,
@@ -250,7 +272,7 @@ describe('699 serialize response components only', () => {
250272
.get(`${app.basePath}/users/1234`)
251273
.expect(400)
252274
.then((r) => {
253-
expect(r.body.message).to.equal('request.params.id should match pattern "^[0-9a-fA-F]{24}$"');
275+
expect(r.body.message).to.equal('request/params/id must match pattern "^[0-9a-fA-F]{24}$"');
254276
}));
255277

256278
it('should control GOOD id format and get a response in expected format', async () =>
@@ -291,7 +313,7 @@ describe('699 serialize response components only', () => {
291313
.set('Content-Type', 'application/json')
292314
.expect(400)
293315
.then((r) => {
294-
expect(r.body.message).to.equal('request.body.id should match pattern "^[0-9a-fA-F]{24}$"');
316+
expect(r.body.message).to.equal('request/body/id must match pattern "^[0-9a-fA-F]{24}$"');
295317
}));
296318

297319
it('should POST throw error on invalid schema Date', async () =>
@@ -305,7 +327,7 @@ describe('699 serialize response components only', () => {
305327
.set('Content-Type', 'application/json')
306328
.expect(400)
307329
.then((r) => {
308-
expect(r.body.message).to.equal('request.body.creationDate should match format "date"');
330+
expect(r.body.message).to.equal('request/body/creationDate must match format "date"');
309331
}));
310332

311333
it('should throw error 500 on invalid object type instead of Date expected', async () =>
@@ -314,8 +336,30 @@ describe('699 serialize response components only', () => {
314336
.query({ baddateresponse: 'functionNotExists' })
315337
.expect(500)
316338
.then((r) => {
317-
console.log(r);
318-
expect(r.body.message).to.equal('.response.creationDate format is invalid');
339+
expect(r.body.message).to.equal(
340+
'/response/creationDate format is invalid',
341+
);
342+
}));
343+
344+
it('should enforce anyOf validations', async () =>
345+
request(app)
346+
.post(`${app.basePath}/users`)
347+
.send({
348+
id: '5fdefd13a6640bb5fb5fa925',
349+
creationDateTime: '2020-12-20T07:28:19.213Z',
350+
creationDate: '2020-12-20',
351+
shortOrLong: 'abc',
352+
})
353+
.set('Content-Type', 'application/json')
354+
.expect(400)
355+
.then((r) => {
356+
expect(r.body.message).to.equal(
357+
[
358+
'request/body/shortOrLong must NOT have more than 2 characters',
359+
'request/body/shortOrLong must NOT have fewer than 4 characters',
360+
'request/body/shortOrLong must match a schema in anyOf',
361+
].join(', '),
362+
);
319363
}));
320364

321365
/*
@@ -327,15 +371,14 @@ describe('699 serialize response components only', () => {
327371
.query({baddateresponse : 'functionBadFormat'})
328372
.expect(200)
329373
.then((r) => {
330-
console.log(r.body);
331374
expect(r.body.message).to.equal('Something saying that date is not date-time format');
332375
}));
333376
334377
*/
335378

336379
});
337380

338-
describe('699 with jsonType array type string-list', () => {
381+
describe('699 with array type string-list', () => {
339382
let app = null;
340383

341384
before(async () => {
@@ -359,7 +402,6 @@ describe('699 with jsonType array type string-list', () => {
359402
},
360403
{
361404
format: 'string-list',
362-
jsonType: 'array',
363405
deserialize: (s): string[] => s.split(',').map(s => s.trim()),
364406
serialize: (o): string => (o as string[]).join(','),
365407
},
@@ -398,7 +440,6 @@ describe('699 with jsonType array type string-list', () => {
398440
res.json(req.body);
399441
});
400442
app.use((err, req, res, next) => {
401-
console.error(err)
402443
res.status(err.status ?? 500).json({
403444
message: err.message,
404445
code: err.status ?? 500,
@@ -419,7 +460,7 @@ describe('699 with jsonType array type string-list', () => {
419460
.get(`${app.basePath}/users/1234`)
420461
.expect(400)
421462
.then((r) => {
422-
expect(r.body.message).to.equal('request.params.id should match pattern "^[0-9a-fA-F]{24}$"');
463+
expect(r.body.message).to.equal('request/params/id must match pattern "^[0-9a-fA-F]{24}$"');
423464
}));
424465

425466
it('should control GOOD id format and get a response in expected format', async () => {
@@ -443,7 +484,8 @@ describe('699 with jsonType array type string-list', () => {
443484
id: '5fdefd13a6640bb5fb5fa925',
444485
tags: 'aa,bb,cc',
445486
creationDateTime: '2020-12-20T07:28:19.213Z',
446-
creationDate: '2020-12-20'
487+
creationDate: '2020-12-20',
488+
shortOrLong: 'abcdef',
447489
})
448490
.set('Content-Type', 'application/json')
449491
.expect(200)
@@ -466,7 +508,7 @@ describe('699 with jsonType array type string-list', () => {
466508
.set('Content-Type', 'application/json')
467509
.expect(400)
468510
.then((r) => {
469-
expect(r.body.message).to.equal('request.body.id should match pattern "^[0-9a-fA-F]{24}$"');
511+
expect(r.body.message).to.equal('request/body/id must match pattern "^[0-9a-fA-F]{24}$"');
470512
}));
471513

472514
it('should POST throw error on invalid schema Date', async () =>
@@ -481,7 +523,7 @@ describe('699 with jsonType array type string-list', () => {
481523
.set('Content-Type', 'application/json')
482524
.expect(400)
483525
.then((r) => {
484-
expect(r.body.message).to.equal('request.body.creationDate should match format "date"');
526+
expect(r.body.message).to.equal('request/body/creationDate must match format "date"');
485527
}));
486528

487529
it('should POST throw error for deserialize on request of non-string format', async () =>
@@ -496,9 +538,29 @@ describe('699 with jsonType array type string-list', () => {
496538
.set('Content-Type', 'application/json')
497539
.expect(400)
498540
.then((r) => {
499-
expect(r.body.message).to.equal('request.body.tags must be a string');
541+
expect(r.body.message).to.equal('request/body/tags must be string');
500542
}));
501543

544+
it('should enforce anyOf validations', async () =>
545+
request(app)
546+
.post(`${app.basePath}/users`)
547+
.send({
548+
id: '5fdefd13a6640bb5fb5fa925',
549+
creationDateTime: '2020-12-20T07:28:19.213Z',
550+
creationDate: '2020-12-20',
551+
shortOrLong: 'abc',
552+
})
553+
.set('Content-Type', 'application/json')
554+
.expect(400)
555+
.then((r) => {
556+
expect(r.body.message).to.equal(
557+
[
558+
'request/body/shortOrLong must NOT have more than 2 characters',
559+
'request/body/shortOrLong must NOT have fewer than 4 characters',
560+
'request/body/shortOrLong must match a schema in anyOf',
561+
].join(', '),
562+
);
563+
}));
502564
});
503565

504566

test/699.yaml renamed to test/resources/699.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ paths:
1818
schema:
1919
type: string
2020
enum: ['functionNotExists', 'functionBadFormat']
21+
- name: shortOrLong
22+
in: query
23+
required: false
24+
schema:
25+
$ref: "#/components/schemas/ShortOrLong"
2126
responses:
2227
200:
2328
description: ""
@@ -51,6 +56,12 @@ components:
5156
DateTime:
5257
type: string
5358
format: date-time
59+
ShortOrLong:
60+
type: string
61+
format: mongo-objectid
62+
anyOf:
63+
- maxLength: 2
64+
- minLength: 4
5465
HistoryItem:
5566
type: object
5667
properties:
@@ -70,6 +81,8 @@ components:
7081
$ref: "#/components/schemas/DateTime"
7182
creationDate:
7283
$ref: "#/components/schemas/Date"
84+
shortOrLong:
85+
$ref: "#/components/schemas/ShortOrLong"
7386
history:
7487
type: array
7588
items:

0 commit comments

Comments
 (0)