-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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] [Go-Server] Required validation too strict #20496
Comments
I think I'm hitting a very closely related issue to this, which probably exposes the same underlying problem of the go-server generated code. But let's try to generalize. Your issueI don't think it's a My issueIn my case I have some properties which are not really When parsing the JSON, I think the unmarshaler doesn't really leave any bread-crumbs or something like that behind, to tell if a property wasn't present. Hence both the My structures are somewhat complex and... inflexible (because 5G APIs are not done by developers, but by a weird committee 🤮). So I can't change them easily and my only temporary solution was to modify the The root causeThe JSON decoder is not saving anywhere some sort of (For nullable properties, of course, that check is simply Other generated code (e.g. C++, I guess, which uses cJSON) does the validation during the JSON unmarshaling. But it also generates code for that, I think, while for Go we don't really have to bother (so far). In such a case, it's somewhat solved, as only if a parameter is present it is parsed and immediately checked. Possible solutions
The second seems best to me, but would love the opinion of original authors and/or more seasoned maintainers here, since I just started using this last week 😉. But it might require a 2nd set of data models, where everything is @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5 @wing328 |
Bug Report Checklist
Description
Hello! I'm currently trying to use the
go-server
generator, however I am running into an issue with the required field validation.If a schema is present which has an integer field marked as
required
, it will not pass validation if the field is present but has a value of0
.This causes a problem when a value of 0 is considered to be a valid input for schemas, as the generated code will reject such values.
openapi-generator version
Tested both with
7.10.0
and7.11.0-SNAPSHOT
.OpenAPI declaration file content or url
Generation Details
Steps to reproduce
java -jar openapi-generator-cli.jar generate -g go-server -i openapi.yaml -o out --additional-properties=router=chi
cd ./out
go mod tidy && go mod run ./main.go
Expected results:
Sending a HTTP PUT request to
http://localhost:8080/api/v1/config
with a body of{"n" : 1}
or a body of{"n" : 0}
both cause a501 Not Implemented
responseActual results:
Sending the request with n=1 results in the expected 501 status code, however the n=0 case causes a
422 Unprocessable Entity
. The error message is:"required field 'n' is zero value."
Related issues/PRs
Suggest a fix
I believe the issue is in the generated code for the model:
I think the required validator should not check if the value is some
zero
of its type, but only check the existence of the key in the property.As a workaround, the
AssertTestObjectRequired
function can be changed to only return nil, however this change must then be made "permanent" by adding the model to the ignore list, otherwise it will get overwritten again.If someone knows the specifics on how to approach this issue I'd be happy to try and help :)
The text was updated successfully, but these errors were encountered: