-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
How to apply default value if empty value provided by client during model binding? #4042
Comments
But I think when you pass it like: When not passed at all, its value will be Anyway, I am debugging it to find out some helpful/useful info to share once available. It should be something relevant to the zero value validation case. I do not think it is a bug so far. And I see it as intuitive and idiomatic behavior TBH. |
Here are the debugging steps in order "neglecting few non-important ones": Turns out that something I could catch for the first time which should be taken into account to enhance the performance & resource utilization further. As calling functions that do nothing except calling other functions without doing real job should be really avoidable specially in Go. "Due to its initial small stack size per routine, and to simplify the stack trace minimizing the transmitted meta data!" |
@ahmadSaeedGoda Hi, thanks for your reply and the debugging screenshots. That gives some nice information on how the Bind functionality works. However, I did not say in my question that the If that's not possible, then could we add this functionality somehow, such as adding a form tag EDIT: It turns out someone seems to have mentioned this same issue in 2019 if I'm not mistaken |
@takanuva15 Currently you can only handle the assignment problem manually, which is the least costly for you The question you raised, and I thought about it a little bit, is that the logical tree will converge to the point where the incoming value is null, and the default value, the default value is enabled. The scope of the impact of modifying gin's underlying code is difficult to estimate. Lines 136 to 166 in 3cb3067
The legacy code and github.com/go-playground/validator validation framework don't mix well. gin/binding/form_mapping_test.go Lines 70 to 82 in 3cb3067
If you have a strong idea, you are welcome to submit a PR here |
- Use provided default value in struct tags when binding a request input to struct for validation. Fixes: How to apply default value if empty value provided by client during model binding? gin-gonic#4042, #13042df, #a41721a
Kindly check the PR to lemme know if any! Cheers, |
@takanuva15 with the proposed PR You can have the default to kick in whether the user provides no value or not sent the field at all. I've decided that it should be done with no need for a new form tag such as Hopefully that makes sense, |
- Use specified default value in struct tags when binding a request input to struct for validation, even if sent empty, not only when not sent at all. - Add string field to `TestMappingDefault` test case. - Add test case for not sent form field to default to the value specified via code. - Add test case for form field sent empty to default to the value specified via code. Fixes: How to apply default value if empty value provided by client during model binding? gin-gonic#4042, #13042df, #a41721a
- Use specified default value in struct tags when binding a request input to struct for validation, even if sent empty, not only when not sent at all. - Add string field to `TestMappingDefault` test case. - Add test case for not sent form field to default to the value specified via code. - Add test case for form field sent empty to default to the value specified via code. Fixes: How to apply default value if empty value provided by client during model binding? #4042, #13042df, #a41721a
I was gonna say that the way your PR is implemented, it's a breaking change to existing servers that assume the default value is not applied if the client sends an empty value. Looks like your PR got merged anyways though and it's set for v1.11, so whatever lol |
@takanuva15 That's a good point. But shall we agree that "default value" "as the name suggests" is meant to be applied whenever the value passed is null or empty? So talking about existing servers that assume when client provide empty value, the default value should not be applied. Why in the first place they specify a default value? Framework is to take care of providing user(developer) some way to determine how the value should be validated. When user/developer require a field to be provided/passed/sent by client request, only 2 options/scenarios here are possible. Either client provide value to be set for this field, or empty value. When dev state default, it has to be set for empty because the field is required in the first place! Means that default is only meant for empty values because there can be only values or empty values in this scenario of a required field. Not possible to have nil or absent field value. It's either empty or has value. Then default value has only one purpose, which is to replace empty values provided by the client. So your suggestion can't be relevant for the above scenario I think, can it? Second scenario is when field is optional (not required by the server), in such a case client can either provide value, empty value, or not send it within the request at all. Here 3 options available. When developer say default value is I must admit I like your way of thinking, but just wanted to clarify for you my intention & reasoning behind appending that peace of code. So let's distill all the cases as follows in bullet points: 1. Required field: 2. Optional field: Hopefully I could make it a bit clearer, and kindly take a look at the function that does all what I've tried to explain above: Lines 217 to 270 in 3cb3067
It explains everything I listed here in a very concise manner. First checks if field is passed by client "set as key in the form map" and default value exists, if one of them isn't true the function stops execution and returns 🤷♂️ All of this has no contradiction with current implementation and all the responsibility falls entirely on the developers' shoulder to know what they are doing logically. Additionally, all the test cases pass BTW, which means my change shouldn't conflict with any other cases already available and provided by the current version of the framework. Moreover, I've appended some tests to already available case, along with 2 of mine to cover my introduced tiny change. It's confusing a bit by nature, and I highly appreciate your excellent point & your feedback. Just wanted to share with you my thoughts about what's already the case provided by the current implementation that doesn't allow for empty fields sent, to be saved empty with the existence of default value. |
Description
(Note I am not reporting a bug here, but rather asking a question)
I have a struct with a form tag specifying a default value. If the user provides an empty value for the query parameter during binding, I would like Gin to ignore it and use the default value provided in the form tag instead. Is there a combination of tags that would make this possible with Gin? (I looked at a lot of issues and couldn't seem to find anything that enabled this behavior)
(I am aware I can add a manual if-statement in the handler body to check-or-set my default fields. However, that adds duplicate code which I would prefer to simply resolve using the
default=
I already specified in the form tag. I also cannot simply force the client to stop sending the empty tag by using something likebinding:"required"
, because that would break existing clients using our APIs which I don't have the ability to repair at the moment)How to reproduce
Expectations
Actual result
Environment
go version go1.23.0 darwin/arm64
github.com/gin-gonic/gin v1.9.1
macOS Sonoma Version 14.6.1 (23G93)
The text was updated successfully, but these errors were encountered: