You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
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 of 0.
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.
Get the latest release (in my case version 7.10.0)
Run 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 a 501 Not Implemented response
Actual 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:
// In model_test_object.go// AssertTestObjectRequired checks if the required fields are not zero-edfuncAssertTestObjectRequired(objTestObject) error {
elements:=map[string]interface{}{
"n": obj.N,
}
forname, el:=rangeelements {
ifisZero:=IsZeroValue(el); isZero {
return&RequiredError{Field: name}
}
}
returnnil
}
//In helper.go// IsZeroValue checks if the val is the zero-ed value.funcIsZeroValue(valinterface{}) bool {
returnval==nil||reflect.DeepEqual(val, reflect.Zero(reflect.TypeOf(val)).Interface())
}
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:
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: