-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fail to mount an API with default values on mutually_exclusive params #1717
Comments
Does removing |
Definitely, did! Also, error message now shows the correct message. (not for mutually) Instead of default value, I set |
Still a bug, right? |
Yes. Default value can be useful. Exactly one of means one of them will be empty or have a default value I gave. Update: Oh, yes, also giving a default value turns them to mutually exists parameters which is a bug. |
Would appreciate a pull request even if it's just failing specs. |
Before I send a PR, lets make sure my steps are correct. After I review the code, I found that, In fact So, I no longer thinks this is a bug. This is an expected behavior. I think clearly stating this on documentation will be enough. |
Aha. If both values have a default it already violates |
Yes. To be clear; Definition:
Request:
It will fail. Why? Because I only give I believe current behavior is the more efficient and correct behavior. We just need to tell that "do not assign even At first I thought giving a default value should be okay. But we should not. Exactly one parameter should be exists in params bag. So we can check which key is exists not by nil or emptyness. To prevent empty and nil values, we should use proc or regexp. |
Correct. A default of |
This has been clarified by adding a note. #1720. |
I think https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#nil-values-for-structures is causing new problems similar to this issue. params do
optional :folder, type: Array[String], default: []
optional :tag, type: Array[String], default: []
mutually_exclusive :folder, :tag
end This will error even if neither param is provided. |
If neither param is provided then both get the default value and those are mutually exclusive, which errors by design. LGTM. |
The problem for me is this is a regression - it wasn't an issue before https://github.com/ruby-grape/grape/blob/master/UPGRADING.md#nil-values-for-structures, because we didn't need to provide a Now, requests that previously wouldn't fail will now fail. The alternative is to not use # 1.3.1 = []
# 1.3.2 = nil |
I understand this is not the way it behaved before, but I would say that this was a bug, fixed since. We probably didn't even have specs for this, hence a minor version bump and a regression. I am not saying we did it on purpose, but we're trying to avoid similar problems moving forward. If you have a mutually exclusive set of values that are both set when none are provided by the caller they are no longer mutually exclusive, which violates the principle of least surprise. Saying "they are mutually exclusive to be supplied by the user" is interpreting I think the workaround is straightforward: params do
optional :folder, type: Array[String]
optional :tag, type: Array[String]
mutually_exclusive :folder, :tag
end
get do
folder_or_tag = params[:folder] || params[:tag] || []
...
end Thoughts? |
Taking this back. An API with mutually exclusive and default values should not mount. This is the feature request. |
Okay. I’m happy with that.
…On Tuesday, May 26, 2020, Daniel Doubrovkine (dB.) @dblockdotorg < ***@***.***> wrote:
Now that we have more attention on this issue, I think we should close
this. IMHO it behaves as intended.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1717 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD4PDJ5BG2WPT3UL3XI5UDRTRUURANCNFSM4EINFIKA>
.
|
Definition:
header:
request body:
or
got:
The text was updated successfully, but these errors were encountered: