Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/support serialization in array items #666

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/middlewares/parsers/schema.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ export class SchemaPreprocessor {
const child = new Node(node, cschema, path);
recurse(node, child, opts);
});
} else if (schema.type === 'array' && !!schema.items) {
const child = new Node(node, schema.items, [...node.path, 'items']);
recurse(node, child, opts);
}
};

Expand Down
23 changes: 22 additions & 1 deletion test/resources/response.object.serializer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ paths:
id:
type: number

/array-of-date-times:
get:
responses:
200:
description: date-time handler
content:
application/json:
schema:
type: object
properties:
users:
type: array
items:
type: object
properties:
created_at:
type: string
format: date-time
id:
type: number

/date:
get:
responses:
Expand All @@ -41,4 +62,4 @@ components:
id:
type: number
created_at:
$ref: "#/components/schemas/Date"
$ref: "#/components/schemas/Date"
16 changes: 16 additions & 0 deletions test/response.object.serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ describe('response serializer', () => {
created_at: date,
});
});
app.get([`${app.basePath}/array-of-date-times`], (req, res) => {
let date = new Date('2020-12-20T07:28:19.213Z');
res.json({
users: [{
id: req.params.id,
created_at: date,
}],
});
});
app.get([`${app.basePath}/date`], (req, res) => {
let date = new Date('2020-12-20T07:28:19.213Z');
res.json({
Expand Down Expand Up @@ -66,5 +75,12 @@ describe('response serializer', () => {
.then((r) => {
expect(r.body.created_at).to.equal('2020-12-20');
}));
it('should validate and serialize date-time in object from array', async () =>
request(app)
.get(`${app.basePath}/array-of-date-times`)
.expect(200)
.then((r) => {
expect(r.body.users[0].created_at).to.equal('2020-12-20T07:28:19.213Z');
}));
});
});