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

Fix mapping nil transformer output back to NSNull #792

Closed
wants to merge 1 commit into from

Conversation

dtrofimov
Copy link

Bug: if a transformer encodes a value to nil, it's converted to a missing value, not to a null value.

Example. My model has an integer value. In case of equality to INVALID_VALUE constant, it should be encoded as null. So I implement the transformer as follows:

return [MTLValueTransformer transformerUsingForwardBlock:^id(NSNumber *num, BOOL *success, NSError *__autoreleasing *error) {
    return num?: @(INVALID_VALUE);
} reverseBlock:^id(NSNumber *num, BOOL *success, NSError *__autoreleasing *error) {
    return (num.intValue == INVALID_VALUE)? nil: num;
}];

I convert a structure with an invalid value in a following manner: model → JSON → model. And the value is transformed as INVALID_VALUE → absent value → 0 (not assigned).

The reason is a mistake in MTLJSONAdapter. As stated in comments, it maps NSNull → nil to use the transformer, and then back after the transformer. But the reverse mapping isn't performed in this specific case.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Jan 20, 2020
@github-actions github-actions bot closed this Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant