Skip to content

Allow fromJSON to set keys to dirty #1295

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

Merged
merged 12 commits into from
Feb 11, 2021
Merged

Conversation

dblythy
Copy link
Member

@dblythy dblythy commented Feb 11, 2021

Currently, there is no way to save fields to a Parse.Object using fromJSON, as dirtyKeys doesn't get set.

This can be confusing as at first glance, you would expect the keys to be set as they are defined. However, after the object is fetched, the keys will be null.

it('from json save data', async (done) => {
    const json = {
      className : 'TestObject',
      date: new Date(),
      array: [],
      object: {},
      string: ''
    }
    const obj = Parse.Object.fromJSON(json);
    expect(obj.get('date')).toBeDefined()
    expect(obj.get('date')).toBeInstanceOf(Date);
    expect(obj.get('array')).toBeDefined()
    expect(obj.get('array')).toBeInstanceOf(Array);
    expect(obj.get('object')).toBeDefined()
    expect(obj.get('object')).toBeInstanceOf(Object);
    expect(obj.get('string')).toBeDefined()
    expect(obj.get('string')).toBeInstanceOf(String);
    await obj.save();
    // all keys are defined


    await obj.fetch();
    // only created at and updated at are defined
    expect(obj.get('date')).toBeDefined()
    expect(obj.get('date')).toBeInstanceOf(Date);
    expect(obj.get('array')).toBeDefined()
    expect(obj.get('array')).toBeInstanceOf(Array);
    expect(obj.get('object')).toBeDefined()
    expect(obj.get('object')).toBeInstanceOf(Object);
    expect(obj.get('string')).toBeDefined()
    expect(obj.get('string')).toBeInstanceOf(String);
    done();
  });

This PR allows a 3rd parameter to fromJSON, where you can specify whether the keys should be set to dirty.

    const json = {
      className : 'TestObject',
      date: new Date(),
      array: [],
      object: {},
      string: ''
    }
    const obj = Parse.Object.fromJSON(json, false, true);
    // now keys will be dirty

Properly closes #161 and #482

@codecov
Copy link

codecov bot commented Feb 11, 2021

Codecov Report

Merging #1295 (3f6c456) into master (832de92) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1295   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           59        59           
  Lines         5792      5794    +2     
  Branches      1301      1302    +1     
=========================================
+ Hits          5792      5794    +2     
Impacted Files Coverage Δ
src/ParseObject.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 832de92...3f6c456. Read the comment docs.

Copy link
Member

@dplewis dplewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I remember this being an issue a while ago.

@dplewis dplewis merged commit 635ba56 into parse-community:master Feb 11, 2021
@dblythy dblythy deleted the toJSON-date branch February 11, 2021 04:08
@ChinaeduPascal
Copy link

Why is it I cannot find the changes in the latest version 3.4.0. The method fromJSON still only takes these parameters:
fromJSON(json: any, override?: boolean): T;.

@mtrezza
Copy link
Member

mtrezza commented Nov 14, 2021

@ChinaeduPascal This may be just a TypeScript issue. I suggest you look into the SDK code and whether the feature has been removed again. The tests in integration/test/ParseObjectTest.js should demo the feature. If not, please open a new issue.

@ChinaeduPascal
Copy link

@mtrezza You are right. The @types/parse definitions are not up-to-date. That's why I won't see the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unable to save Object.fromJSON() because it's not dirty
4 participants