express-openapi-validator
express-openapi-validator copied to clipboard
Serdes Does Not Work for Items in Object in Array
Describe the bug
Serdes performs well on properties in an object. However, when the owning object is part of an array, then serialize is never called.
To Reproduce
git clone https://github.com/cdimascio/express-openapi-validator.gitnpm run deps && npm icd express-openapi-validator/examples/7-response-date-serializationnpm startGET http://localhost:3000/v1/date-time
The above works fine.
Let's now modify the route in app.js to return an array of objects:
app.get('/v1/date-time', function (req, res, next) {
res.json(
// {
// id: 1,
// created_at: new Date(),
// }
[
{
id: 1,
created_at: new Date(),
}
]
);
});
We'll also modify the schema in api.yaml:
/date-time:
get:
responses:
200:
description: date-time handler
content:
application/json:
schema:
# type: object
# properties:
# created_at:
# type: string
# format: date-time
# id:
# type: number
type: array
items:
type: object
properties:
created_at:
type: string
format: date-time
id:
type: number
Actual behavior
{
"message": ".response[0].created_at should be string",
"errors": [
{
"path": ".response[0].created_at",
"message": "should be string",
"errorCode": "type.openapi.validation"
}
]
}
Expected behavior
[
{
"id": 1,
"created_at": "2022-01-20T17:40:41.816Z"
}
]
@YaakovR thanks so much for this issue. You isolated the issue I was having deep in a structure and made it much easier to find.
I believe I have a fix with a test that I'll submit as a PR.
Did this PR ever make it through? I think I'm having that issue myself now.
@Jackman3005 No it did not.
@cdimascio Can we get #704 reviewed and merged now?
@Fabiencdp
@robertjustjones +1 I will re-test and check everything as soon as i can this week. But as soon as i remember, your change fix the problem, and i did not found any breaking change
I've spent several days digging to the root cause by facing the same issue and happily found that there is a PR already, thank you @robertjustjones
Gentlemen, when do you think this can be fixed?
We are waiting for the approve of the repository owner
@cdimascio Are there any hints about when a release with this fix will be rolled out?
This is now available in v5.0.0