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

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

Open
babaliaris opened this issue Apr 26, 2021 · 2 comments
Open
Labels
bug Something isn't working

Comments

@babaliaris
Copy link

babaliaris commented Apr 26, 2021

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
@cdimascio
Copy link
Owner

cdimascio commented Apr 27, 2021

@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 cdimascio added the bug Something isn't working label Apr 27, 2021
@babaliaris
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants