-
Notifications
You must be signed in to change notification settings - Fork 422
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
[BUG] Option of CommaSeparated has a strange behavior when no value is provided #3890
Comments
I think the current design is consistent with how non-comma-separated parameters are represented. That is, if you have a If "empty value" is a valid input it should be properly handled by the server logic - in your case, it's discarding/using a default value, in others it might be quite different. |
I agree with your example of a non-list value. It's actually quite simple, I imagine, with Tapir, to validate the String and set a minimum length if necessary. However, in the case of an argument with a list, I don't think it's the responsibility of the logic, but of the codec. That's why I think that managing non-blank in a codec option list string, in the comma separated, or a little earlier (I don't know tapir well enough), seems to me to make sense and be of value to the library. This would make it possible to manage my case, but also the case with an enum, because you won't get a parsing error if you supply ?x=. If you want an error in this case, you can do it cleanly, by declaring a validator that checks that at least one value is supplied. That's my opinion, but after all it's up to you :) Thank you for your reply. |
I'd say that if I declare that I want a list of comma-separated values parsed as an enum, and the user passes |
This is quite a special case I agree, my opinion is that the behavior should be:
As a result, with this reasoning, if we declare Note that this approach gives the final choice to the user: if I want the list to be empty, I have the possibility (unlike now); if I don't want the list to be empty, I add a validator to check that I have at least one occurrence. I hope I've convinced you :) NB: In terms of implementation, this could be, if I'm not mistaken, a modification to the codec |
I agree with your reasoning, and indeed there's currently no way of representing empty lists. On the other hand, if we deserialise |
Yes, why not, it could also be a parameter to the current codec, the most important thing is to have control over the behaviour |
Tapir version: *** 1.10.10
Scala version: *** 2.13
Describe the bug
I need to have an optional list of parameters. This list comes from a query parameter that is either not present in the query at all, defined without a value, or defined with a comma-separated list of values.
Use case:
For example, let's take the parameter "sort" and "desc", which controls the sorting of an API result. Sort indicates the list of fields to be sorted, in order, and desc indicates the direction. If desc is:
This use case is in fact an implementation of a design proposed by Octo on the sorting of a REST API.
I then expect to have to declare something like:
In reality I have to do:
What is the problem?
The problem is that in the case where the parameter is supplied without a value, I should have
Some(Nil)
as the decoded value, but I actually haveSome(List(""))
. And if the codec type is not a string but an enumeration or something else, then the decoding return an error for invalid value.For my opinion, this result is not what we can expect when we declare a query input as "option of list".
What do you think about that ?
The text was updated successfully, but these errors were encountered: