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

Express res.json() returns wrong date when using open-api-validator!

Open babaliaris opened this issue 4 years ago • 2 comments

Describe the bug When you return a JSON response that contains a date object created by an ISO-8601 string, and in the API spec (.yaml) the date field is type Date, you get the wrong YYYY-MM-DD back! For example, if the date was created using "1996-01-03T22:00:00.000Z" then in the response you get 1996-01-03 instead of 1996-01-04 which is the correct YYYY-MM-DD representation of that UTC date. This only happens if you are using the open-api-validator, else what you get back is the actual ISO-8601 "1996-01-03T22:00:00.000Z". It seems like the open-api-validator cuts the string to only keep the first 10 characters.

  1. Just use the following example with the YAML spec to test it out. You will see that the response returns 1996-01-03 as the date.

  2. Now disable the open-api-validator middleware by commenting it out. What you will get as a response will be "1996-01-03T22:00:00.000Z" which of course is not the right format but it's definitely the correct DateTime!!!

Examples and context

index.js

const express   = require("express");
const validator = require("express-openapi-validator");

//Create Express APP.
const app = express();

//JSON Middleware.
app.use(express.json());


//Open API Validator Middleware.
app.use(
    validator.middleware({
      apiSpec           : './api.yaml',
      validateRequests  : true,
      validateResponses : true
    })
);



//GET /user
app.get("/user", (req, res)=>
{
    const user = {
        id  : 0,
        name: "John",
        date: new Date("1996-01-03T22:00:00.000Z")
    };

    res.status(200).json(user);
});



//Open API ERROR handler.
app.use((err, req, res, next) => {
    
    res.status(err.status || 500).json({
      message: err.message,
      errors: err.errors,
    });
});


//Start Listening.
app.listen(1996, ()=>
{
    console.log("Listening!!!!");
});

api.yaml

openapi: 3.0.0
servers:
  # Added by API Auto Mocking Plugin
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/babaliaris/test/1.0.0
  - description: My Computer
    url: http://localhost:1996
info:
  description: This is a simple API
  version: "1.0.0"
  title: Simple Inventory API
  contact:
    email: [email protected]
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'

paths:
  /user:
    get:
      tags:
        - Users
      summary: get user.
      operationId: getUser
      responses:
        
        '200':
          description: Get the json user data.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
        default:
          description: Something went wrong.

components:
  schemas:
    User:
      type: object
      required:
        - id
        - name
        - date
      properties:
        id:
          type: integer
        name:
          type: string
        date:
          type: string
          format: date

babaliaris avatar Apr 26 '21 10:04 babaliaris

@babaliaris thanks. You are correct. It's not handling the date probably. Here is the offending line Would u be willing to post a PR with the fix?

cdimascio avatar Apr 27 '21 03:04 cdimascio

Oh I see it! Indeed it splits the ISO string and keeps the first part. Yes I will create a PR, I'd be happy to contribute to this awesome project!

babaliaris avatar Apr 27 '21 06:04 babaliaris