Skip to content

Serdes Does Not Work for Items in Object in Array #699

Closed
@YaakovR

Description

@YaakovR

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"
    }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions