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

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

Comments

@YaakovR
Copy link

YaakovR commented Jan 20, 2022

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"
    }
]
robertjustjones added a commit to robertjustjones/express-openapi-validator that referenced this issue Feb 12, 2022
@robertjustjones
Copy link
Contributor

@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 added a commit to robertjustjones/express-openapi-validator that referenced this issue Feb 12, 2022
robertjustjones added a commit to robertjustjones/express-openapi-validator that referenced this issue Apr 5, 2022
robertjustjones added a commit to robertjustjones/express-openapi-validator that referenced this issue Apr 8, 2022
robertjustjones added a commit to robertjustjones/express-openapi-validator that referenced this issue Jun 16, 2022
@Jackman3005
Copy link

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

@robertjustjones
Copy link
Contributor

@Jackman3005 No it did not.

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

@Fabiencdp

@Fabiencdp
Copy link

@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

robertjustjones added a commit to robertjustjones/express-openapi-validator that referenced this issue Aug 25, 2022
@srgg
Copy link

srgg commented Aug 31, 2022

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?

@Fabiencdp
Copy link

Fabiencdp commented Aug 31, 2022

We are waiting for the approve of the repository owner

@t0lik
Copy link

t0lik commented Nov 19, 2022

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

@cdimascio
Copy link
Owner

This is now available in v5.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment