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

[SEMVER-MAJOR] Fix object type conversion #347

Merged
merged 2 commits into from
Sep 14, 2016
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Sep 12, 2016

  • Reject array/date values for object arguments
  • Add rest-coercion tests for custom object types

This patch builds on top of recently landed #343

Connect to #312
Connect to strongloop-internal/scrum-loopback#974
Connect to strongloop-internal/scrum-loopback#885

@Amir-61 or @richardpringle please review
cc @ritch @STRML

var objectResult = objectConverter.fromSloppyValue(ctx, value);
if (!objectResult.error && objectResult.value)
return objectResult;
var looksLikeJson = typeof value === 'string' && (
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it could be a nice utility function to pull out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, will extract.

@bajtos bajtos force-pushed the fix/object-type-coercion branch from 26e683e to fa7ac40 Compare September 12, 2016 14:54
@STRML
Copy link
Member

STRML commented Sep 12, 2016

LGTM, I agree that while JS says new Date() instanceof Object and Array() instanceof Object, that's nonsensical and clearly not what any developer would actually want.

@richardpringle
Copy link
Contributor

I completely agree for dates, but some might be using any JSON notation as type-object, which of course includes an array.

I personally don't think that we should be so strict, if a developer wants to assure that an object strictly starts with {, they should implement the type check themselves.

@bajtos
Copy link
Member Author

bajtos commented Sep 13, 2016

I completely agree for dates, but some might be using any JSON notation as type-object, which of course includes an array.

One can use type: 'any' for arguments that support multiple types.

I personally don't think that we should be so strict, if a developer wants to assure that an object strictly starts with {, they should implement the type check themselves.

My issue with this is that most developers won't realise their function can receive an array in an object argument.

IMO, the case where an array is not wanted will be much more common than the case where a method expects either an object or an array.

I am proposing to implement a new flag that will signal that an argument can accept both types and defer the implementation until there is a LoopBack user asking for this feature.

{ arg: 'data', type: 'object', allowArrays: true }

@richardpringle Thoughts?
cc @STRML @ritch

Having to distinguish between an array and a plain object in remote
methods is tedious. For example, LoopBack Model constructors expect
an object as the first argument, but will accept an array too, with
undefined result.

This commit changes the implementation of object converter to reject
values that are of a different strong-remoting type,
namely "array" and "date"
@bajtos bajtos force-pushed the fix/object-type-coercion branch from 2f81363 to 4b0b2e8 Compare September 13, 2016 13:29
@bajtos
Copy link
Member Author

bajtos commented Sep 13, 2016

My issue with this is that most developers won't realise their function can receive an array in an object argument.

For example, let's take a LoopBack model called Product:

$ node
> new Product([{ name: 'Pen' }, { name: 'Pencil' }])
{ '0': { name: 'Pen' }, '1': { name: 'Pencil' } }

Because the model implementation assumes the input is an object, it treats the array indices as property names and array items as property values. I don't think that's a result the caller was expecting!

@richardpringle
Copy link
Contributor

I like the flag, because that allows anything in JSON format

@STRML
Copy link
Member

STRML commented Sep 13, 2016

Why not unions? Seems better to allow ['object', 'array'] than a special
flag.

On Sep 13, 2016 8:44 AM, "Richard Pringle" [email protected] wrote:

I like the flag, because that allows anything in JSON format


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#347 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABJFPxFZWIF6_3LNXZ397ctaKfrTcMKyks5qpqi4gaJpZM4J6rCX
.

@bajtos
Copy link
Member Author

bajtos commented Sep 13, 2016

Why not unions? Seems better to allow ['object', 'array'] than a special flag.

Even better 👍

(Note that Swagger spec does not support union types yet, AFAIK.)

I still think this should be kept out of scope of this pull request though.

@STRML
Copy link
Member

STRML commented Sep 13, 2016

Yeah I agree.

In the future I'd like to see the ability to provide functions as types - could add the following extensions mirroring Model validators:

// First arg must match a provided arg in the remoting function
User.update.validate('firstname', function(err) {...})

And possibly even something to override coercion?

User.update.coerce('firstname', function(value) {...})

This way we could maintain swagger compat by still requiring a string type name.

@Amir-61
Copy link
Contributor

Amir-61 commented Sep 13, 2016

Why not unions? Seems better to allow ['object', 'array'] than a special
flag.

Thats a great idea 👍 I agree we should keep it out of the scope of this PR.

LGTM

@richardpringle
Copy link
Contributor

Might be a good idea to accept any valid JSON with type-JSON too (also beyond the scope of the PR).

@bajtos bajtos merged commit 4c7b16e into master Sep 14, 2016
@bajtos bajtos deleted the fix/object-type-coercion branch September 14, 2016 07:45
@bajtos bajtos removed the #review label Sep 14, 2016
@bajtos
Copy link
Member Author

bajtos commented Sep 14, 2016

Thank you all for your input.

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.

4 participants