openapi-generator
openapi-generator copied to clipboard
[REQ] Feature Request: Option to disable enum checks on unmarshal (Golang)
Is your feature request related to a problem? Please describe.
When unmarshalling a JSON request model into a generated Go struct, enum values are checked upon unmarshalling and an error is thrown if the provided value is not part of the enum. See the generated code below or more detail.
func (v *Status) UnmarshalJSON(src []byte) error {
var value string
err := json.Unmarshal(src, &value)
if err != nil {
return err
}
enumTypeValue := Status(value)
for _, existing := range AllowedStatusEnumValues {
if existing == enumTypeValue {
*v = enumTypeValue
return nil
}
}
return fmt.Errorf("%+v is not a valid Status", value)
}
Describe the solution you'd like
Provide an additional-property for the go generator to disable this check. E.g. disableEnumCheck=true, default: false
Describe alternatives you've considered
We could catch the error and handle it on unmarshalling, but since we have our own validation logic after the unmarshalling we do not want to throw on error here.
Another alternative would be to throw an openapi-generator enum-specific error instead of a common error as it is implemented right now, but this would imply that an additional error type has to be generated every time.
...
return fmt.Errorf("%+v is not a valid Status", value)
=>
return EnumError("%+v is not a valid Status", value)
@jonrosner i believe your alternative approach of using different error would be good. i can give it a try for that
is this still an option? I could maybe take this and transform some errors into dedicated errors rather than fmt.Errorf
errors.
I ran into the same problem again when in a version update a check for required fields was added.