express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

Serdes Does Not Work for Items in Object in Array

Open YaakovR opened this issue 3 years ago • 6 comments

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

  1. git clone https://github.com/cdimascio/express-openapi-validator.git
  2. npm run deps && npm i
  3. cd express-openapi-validator/examples/7-response-date-serialization
  4. npm start
  5. GET 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 avatar Jan 20 '22 17:01 YaakovR

@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.

robertjustjones avatar Feb 12 '22 17:02 robertjustjones

Did this PR ever make it through? I think I'm having that issue myself now.

Jackman3005 avatar Aug 22 '22 05:08 Jackman3005

@Jackman3005 No it did not.

@cdimascio Can we get #704 reviewed and merged now?

@Fabiencdp

robertjustjones avatar Aug 22 '22 14:08 robertjustjones

@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

Fabiencdp avatar Aug 22 '22 18:08 Fabiencdp

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?

srgg avatar Aug 31 '22 03:08 srgg

We are waiting for the approve of the repository owner

Fabiencdp avatar Aug 31 '22 07:08 Fabiencdp

@cdimascio Are there any hints about when a release with this fix will be rolled out?

t0lik avatar Nov 19 '22 20:11 t0lik

This is now available in v5.0.0

cdimascio avatar Nov 20 '22 23:11 cdimascio