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

Nested Date type attributes are saved differently at different levels #7575

Closed
4 tasks done
Tracked by #8225
hariprasadiit opened this issue Sep 14, 2021 · 6 comments · Fixed by #8209
Closed
4 tasks done
Tracked by #8225

Nested Date type attributes are saved differently at different levels #7575

hariprasadiit opened this issue Sep 14, 2021 · 6 comments · Fixed by #8209
Labels
state:breaking Breaking change requires major version increment and `BREAKING CHANGE` commit message state:released Released as stable version state:released-alpha Released as alpha version state:released-beta Released as beta version type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@hariprasadiit
Copy link

hariprasadiit commented Sep 14, 2021

New Issue Checklist

Issue Description

If we try to create an object with below code

const myObj = new Parse.Object('Company3');
await myObj.save({
  prop1: 'test1',
  prop2: {
    test: {
      date: new Date()
    }
  },
  prop3: {
    date: new Date()
  }
}, {useMasterKey: true});

This is how the saved object in mongo DB looks like

{
  "_id": "5DTIqZcZv9fhbHu0qTZq",
  "prop2": {
    "test": {
      "date": {
        "__type": "Date",
        "iso": "2021-09-14T14:50:37.303Z"
      }
    }
  },
  "prop3": {
    "date": {
      "$date": "2021-09-14T14:50:37.303Z"
    }
  },
  "prop1": "test1",
  "_created_at": {
    "$date": "2021-09-14T14:50:38.108Z"
  },
  "_updated_at": {
    "$date": "2021-09-14T14:50:38.108Z"
  }
}

prop2.test.date is saved as JSON object instead of $date object while prop3.date is properly saved.
This breaks date queries

Steps to reproduce

  1. Create new object with below code
const myObj = new Parse.Object('Company3');
await myObj.save({
  prop1: 'test1',
  prop2: {
    test: {
      date: new Date()
    }
  },
  prop3: {
    date: new Date()
  }
}, {useMasterKey: true});
  1. Observe different date types at different levels in mongoDB Compass

Actual Outcome

{
  "_id": "5DTIqZcZv9fhbHu0qTZq",
  "prop2": {
    "test": {
      "date": {
        "__type": "Date",
        "iso": "2021-09-14T14:50:37.303Z"
      }
    }
  },
  "prop3": {
    "date": {
      "$date": "2021-09-14T14:50:37.303Z"
    }
  },
  "prop1": "test1",
  "_created_at": {
    "$date": "2021-09-14T14:50:38.108Z"
  },
  "_updated_at": {
    "$date": "2021-09-14T14:50:38.108Z"
  }
}

Expected Outcome

{
  "_id": "5DTIqZcZv9fhbHu0qTZq",
  "prop2": {
    "test": {
      "date": {
        "$date": "2021-09-14T14:50:37.303Z"
      }
    }
  },
  "prop3": {
    "date": {
      "$date": "2021-09-14T14:50:37.303Z"
    }
  },
  "prop1": "test1",
  "_created_at": {
    "$date": "2021-09-14T14:50:38.108Z"
  },
  "_updated_at": {
    "$date": "2021-09-14T14:50:38.108Z"
  }
}

Environment

Server

  • Parse Server version: 4.10.3
  • Operating system: Mac OS
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): Local

Database

  • System (MongoDB or Postgres): MongoDB
  • Database version: 4.4.4
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): Local

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): Javascript
  • SDK version: 3.3.0

Old issue related to this #6840

@parse-github-assistant
Copy link

parse-github-assistant bot commented Sep 14, 2021

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

@mtrezza
Copy link
Member

mtrezza commented Sep 14, 2021

I was able to reproduce the issue. Would you want to open a PR with a failing test?

I classified this as bug with severity S3 (normal):

  • Workaround is to use a different data structure in the object that does not nest date fields deeper than 1 level

@mtrezza mtrezza added type:bug Impaired feature or lacking behavior that is likely assumed S3 labels Sep 14, 2021
hariprasadiit added a commit to vizmo-vms/vizmo-parse-server that referenced this issue Sep 15, 2021
@cbaker6
Copy link
Contributor

cbaker6 commented May 21, 2022

I suspect this is more of a JS SDK issue. According to the REST documentation, Parse treats Date as a special data type:

The Date type contains a field iso which contains a UTC timestamp stored in ISO 8601 format with millisecond precision: YYYY-MM-DDTHH:MM:SS.MMMZ.

{
  "__type": "Date",
  "iso": "2022-01-01T12:23:45.678Z"
}

So the outcome of prop2 looks correct IMO and the others are wrong:

"prop2": {
    "test": {
      "date": {
        "__type": "Date",
        "iso": "2021-09-14T14:50:37.303Z"
      }
    }
  }

My explanation #8001 (comment) may provide some more context on what the JS SDK should be doing. From your current post it seems the JS SDK is partially encoding date correctly, but not handling date correctly on top level dates (i.e. prop3)

I suspect the issue is here in the JS SDK: https://github.com/parse-community/Parse-SDK-JS/blob/e2bd6be145f55fab322633a8f144be34e7fcb202/src/encode.js#L62-L67. If you add an if statement to check if the current top level object is a date and use a similar block transforming the date to the iso format, it should fix this issue.

@mtrezza mtrezza mentioned this issue Nov 2, 2022
31 tasks
@mtrezza mtrezza added the state:breaking Breaking change requires major version increment and `BREAKING CHANGE` commit message label Nov 9, 2022
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 6.0.0-alpha.15

@parseplatformorg parseplatformorg added the state:released-alpha Released as alpha version label Dec 20, 2022
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 6.0.0-beta.1

@parseplatformorg parseplatformorg added the state:released-beta Released as beta version label Jan 31, 2023
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 6.0.0

@parseplatformorg parseplatformorg added the state:released Released as stable version label Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state:breaking Breaking change requires major version increment and `BREAKING CHANGE` commit message state:released Released as stable version state:released-alpha Released as alpha version state:released-beta Released as beta version type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants