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

[FEATURE ds-boolean-transform-allow-null] allow null for boolean #4022

Merged
merged 1 commit into from
Mar 18, 2016
Merged

[FEATURE ds-boolean-transform-allow-null] allow null for boolean #4022

merged 1 commit into from
Mar 18, 2016

Conversation

pangratz
Copy link
Member

NOTE: this description is not completely accurate anymore, since the implementation changed so the allowNull flag needs to be passed to the options for the attribute. See discussion below for further context.


This feature allows null / undefined values for DS.attr('boolean')
attributes. Currently DS.BooleanTransform converts those values to
false; which means that a DS.attr('boolean') cannot be set to null
from the server. Other transforms (string, number) do allow null
values, so this is an inconsistency between the transforms.

The new feature ds-boolean-transform-allow-null changes this behavior
so null / undefined are converted to null. Since this is a semver
breaking change
, a corresponding deprecation warning should be added
before this is accepted in ember data.

Current behavior of DS.attr('boolean'):

incoming DS serialized
null false false
undefined false false

Behavior of DS.attr('boolean'), having
ds-boolean-transform-allow-null feature enabled:

incoming DS serialized
null null null
undefined null null

Note that this feature only works if ds-transform-pass-options is
enabled as well, since passing the options from DS.attr to the
transform is added with that feature.


This addresses #3785.


TODO

@fivetanley
Copy link
Member

@pangratz During the team discussion someone suggested passing an options hash to DS.attr({allowNull: true}) so we could maintain backwards compatibility. What do you think about this change?

@pangratz
Copy link
Member Author

pangratz commented Jan 5, 2016

Seems good to me. But for this to work emberjs/rfcs#1 (also see discussion in #3111) needs to be go'ed and implemented, since currently the hash is not passed to transformations.

Would the eventual default in 3.x be that null are allowed for DS.attr('boolean'), so it is consistent with number, string and date transforms?

@pangratz
Copy link
Member Author

pangratz commented Jan 6, 2016

Alright, emberjs/rfcs#1 has been accepted. Once that RFC is implemented I will update this PR so the allowNull flag is considered.

@pangratz
Copy link
Member Author

Alrighty, I rebased onto latest master and now null is returned for null/undefined if the allowNull flag is set to true:

export default Model.extend({
  leBool: attr('boolean', { allowNull: true })
});

@@ -38,6 +50,14 @@ export default Transform.extend({
},

serialize(deserialized) {
if (isEnabled('ds-transform-pass-options')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be checking the 2nd argument for { allowNull: true }?

This feature allows `null` / `undefined` values for `DS.attr('boolean')`
attributes. Currently `DS.BooleanTransform` converts those values to
`false`; which means that a `DS.attr('boolean')` cannot be set to `null`
from the server. Other transforms (`string`, `number`) do allow `null`
values, so this is an inconsistency.

Current behavior of `DS.attr('boolean')`:

| incoming    | DS      | serialized |
|-------------|---------|------------|
| `null`      | `false` | `false`    |
| `undefined` | `false` | `false`    |

Behavior of `DS.attr('boolean')`, having
`ds-boolean-transform-allow-null` feature enabled and having an
attribute `DS.attr('boolean', { allowNull: true })`:

| incoming    | DS      | serialized |
|-------------|---------|------------|
| `null`      | `null`  | `null`     |
| `undefined` | `null`  | `null`     |

---

Note that this feature only works if `ds-transform-pass-options` is
enabled as well, since passing the options from `DS.attr` to the
transform is added with that feature.
@pangratz
Copy link
Member Author

@bmac I somehow forgot to commit the changes considering the allowNull flag 🤦 sorry for the jump start.

I - once again - updated the PR; now it should be ready for review 😉

@btecu
Copy link
Contributor

btecu commented Feb 17, 2016

Wouldn't defaultValue work for this?
I think it should work at least for undefined.

@pangratz
Copy link
Member Author

I think this wouldn't work since defaultValue is only used when the value of an attribute is retrieved from a DS.Model, but not when an attribute is serialized...

var type = typeof serialized;

if (isEnabled('ds-transform-pass-options')) {
if (isEnabled('ds-boolean-transform-allow-null')) {
if (isNone(serialized) && options.allowNull === true) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we use Ember.get(options, 'allowNull') in case options is undefined?

Copy link
Member Author

Choose a reason for hiding this comment

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

Theoretically options should never be undefined because of this and ultimately because of this.

It would only be undefined if the transform is used outside of regular ember-data usage...

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.

bmac added a commit that referenced this pull request Mar 18, 2016
[FEATURE ds-boolean-transform-allow-null] allow null for boolean
@bmac bmac merged commit 24dd96f into emberjs:master Mar 18, 2016
@pangratz pangratz deleted the boolean-transform-allow-null branch March 18, 2016 21:28
@sandstrom
Copy link
Contributor

sandstrom commented Jul 26, 2016

This is a great adjustment! 🎆

@bmac @fivetanley for 3.0, would you mind flipping the default of the option (or removing the option altogether)? In our app I'd say 99% of boolean values across all our models would use the allowNull option. Also, it's inconsistent with string, number etc. where no option is required.

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.

7 participants