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

Missing required argument doesn't cause an error when that argument type is boolean #2092

Closed
CarlosCuevas opened this issue Feb 19, 2016 · 12 comments
Assignees

Comments

@CarlosCuevas
Copy link

Person.remoteMethod('createPerson', {
        accepts: [
            { arg: 'name', type: 'string', required: true },
            { arg: 'age', type: 'number', required: true },
            { arg: 'is_alive', type: 'boolean', required: true }
        ],
        http: {
            path: '/',
            verb: 'post'
        }
    });

If I send a request to this endpoint that is missing 'name' or 'age', it will return an error. If I send a request missing 'is_alive', it will proceed with 'is_alive' set to false.

@Neil-UWA
Copy link

I guess that you may have set a default value for is_alive in your model defination

@CarlosCuevas
Copy link
Author

@Neil-UWA Thanks, but there are many cases where it would be inappropriate to set a default value (exactly when you'd want to set a field as required).

@richardpringle
Copy link
Contributor

Hey @CarlosCuevas, I'm guessing that somewhere in the code that is_alive = undefined is evaluating to false. I will look into the details of the issue.

Thanks!

@richardpringle
Copy link
Contributor

@bajtos, I found the source of the issue: https://github.com/strongloop/strong-remoting/blob/master/lib/dynamic.js#L98-L117

Boolean(undefined) // returns false;

This is an easy fix, but before I do so, I just want to double check to make sure there aren't any use-cases where you wouldn't want the return value to remain "undefined" for the Dynamic.define('boolean', ... function in general.

@bajtos bajtos added this to the #Epic: Coercion Cleanup milestone Mar 2, 2016
@bajtos
Copy link
Member

bajtos commented Mar 2, 2016

@richardpringle thanks for checking, a change like this can have subtle side effects, so it's best to consider it thoroughly.

We have recently discovered quite few flaws in strong-remoting's argument coercion (see strongloop/strong-remoting#264, #1806, strongloop/strong-remoting#208), there is a spike story to investigate the issues and come up with a more robust design, see https://github.com/strongloop-internal/scrum-loopback/issues/706.

So much for the background context.

As for fixing boolean conversion, see also strongloop/strong-remoting@b924a79 (which has been reverted as it did not work well).

I found the source of the issue: https://github.com/strongloop/strong-remoting/blob/master/lib/dynamic.js#L98-L117

return Boolean(undefined) // returns false;
// we should return undefined instead

This is an easy fix, but before I do so, I just want to double check to make sure there aren't any use-cases where you wouldn't want the return value to remain "undefined"

I am inclining towards changing boolean converter to preserve undefined values, as you are proposing @richardpringle.

@ritch @raymondfeng @STRML do you have any opinion on this? Can you see a use case where the proposed change would be a problem?

@STRML
Copy link
Member

STRML commented Mar 2, 2016

FYI @bajtos we can't see those internal issues.
On Mar 2, 2016 6:55 PM, "Miroslav Bajtoš" [email protected] wrote:

@richardpringle https://github.com/richardpringle thanks for checking,
a change like this can have subtle side effects, so it's best to consider
it thoroughly.

We have recently discovered quite few flaws in strong-remoting's argument
coercion (see strongloop/strong-remoting#264
strongloop/strong-remoting#264,
#1806
#1806, strongloop/
strong-r emoting#208
strongloop/strong-remoting#208), there is a
spike story to investigate the issues and come up with a more robust
design, see strongloop-internal/scrum-loopback#706
https://github.com/strongloop-internal/scrum-loopback/issues/706.

So much for the background context.

As for fixing boolean conversion, see also strongloop/strong-remoting@
b924a79
strongloop/strong-remoting@b924a79
(which has been reverted as it did not work well).

I found the source of the issue:
https://github.com/strongloop/strong-remoting/blob/master/lib/dynamic.js#L98-L117

return Boolean(undefined) // returns false;// we should return undefined instead

This is an easy fix, but before I do so, I just want to double check to
make sure there aren't any use-cases where you wouldn't want the return
value to remain "undefined"

I am inclining towards changing boolean converter to preserve undefined
values, as you are proposing @richardpringle
https://github.com/richardpringle.

@ritch https://github.com/ritch @raymondfeng
https://github.com/raymondfeng @STRML https://github.com/STRML do you
have any opinion on this? Can you see a use case where the proposed change
would be a problem?


Reply to this email directly or view it on GitHub
#2092 (comment)
.

@bajtos
Copy link
Member

bajtos commented Mar 2, 2016

@STRML we can't see those internal issues.

That internal issue is just a placeholder that helps us to plan our work in sprints. It lists the three issues I mentioned in my comment above - strongloop/strong-remoting#264, #1806, strongloop/strong-remoting#208.

I know the internal issues are not ideal, but let's keep that matter out of the discussion in this particular GH issue.

@STRML
Copy link
Member

STRML commented Mar 2, 2016

Not familiar with the term "spike story", assumed there was something of
substance since you linked it.
On Mar 2, 2016 7:26 PM, "Miroslav Bajtoš" [email protected] wrote:

@STRML https://github.com/STRML we can't see those internal issues.

That internal issue is just a placeholder that helps us to plan our work
in sprints. It lists the three issues I mentioned in my comment above -
strongloop/strong-remoting#264
strongloop/strong-remoting#264,
#1806
#1806,
strongloop/strong-remoting#208
strongloop/strong-remoting#208.

I know the internal issues are not ideal, but let's keep that matter out
of the discussion in this particular GH issue.


Reply to this email directly or view it on GitHub
#2092 (comment)
.

@richardpringle
Copy link
Contributor

@STRML: Spike - research and come up with a proposal for a change.

There's nothing of substance in "spike" story, @bajtos just wanted to let me know that we are aware there is a problem.

@CarlosCuevas
Copy link
Author

@richardpringle Just caught that this is also the case when type is "array".

Person.remoteMethod('createPerson', {
        accepts: [
            { arg: 'name', type: 'string', required: true },
            { arg: 'age', type: 'number', required: true },
            { arg: 'children', type: 'array', required: true }
        ],
        http: {
            path: '/',
            verb: 'post'
        }
    });

failing to passing children will not trigger an error.

additionally, if you pass a non array value such as a number, that value becomes a string in an array. for example, if i were to pass the number 123, it becomes [ "123" ]

@bajtos
Copy link
Member

bajtos commented Jun 2, 2016

Related: #603

I expected the method to return a 400 bad request or something when the argument payload (i.e.: the body of the post in the case) isn't an array of objects. However, it seems to take any valid json.

@bajtos
Copy link
Member

bajtos commented Sep 13, 2016

The problem will be fixed in LoopBack 3.0, see strongloop/strong-remoting#343.

@bajtos bajtos closed this as completed Sep 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants