-
Notifications
You must be signed in to change notification settings - Fork 92
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
Conversation
var objectResult = objectConverter.fromSloppyValue(ctx, value); | ||
if (!objectResult.error && objectResult.value) | ||
return objectResult; | ||
var looksLikeJson = typeof value === 'string' && ( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, will extract.
26e683e
to
fa7ac40
Compare
LGTM, I agree that while JS says |
fa7ac40
to
2f81363
Compare
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 |
One can use
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? |
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"
2f81363
to
4b0b2e8
Compare
For example, let's take a LoopBack model called $ 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! |
I like the flag, because that allows anything in JSON format |
Why not unions? Seems better to allow ['object', 'array'] than a special On Sep 13, 2016 8:44 AM, "Richard Pringle" [email protected] wrote:
|
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. |
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. |
Thats a great idea 👍 I agree we should keep it out of the scope of this PR. LGTM |
Might be a good idea to accept any valid JSON with type-JSON too (also beyond the scope of the PR). |
Thank you all for your input. |
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