OpenAPITools / OpenAPITools/openapi-generator
[REQ] Feature Request: Option to disable enum checks on unmarshal (Golang)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Go generator and the generated UnmarshalJSON behavior shown in the issue. Determine where a generator property could control enum validation, then verify that enabling disableEnumCheck allows unknown values while the default continues rejecting them. The issue names no source file or test, so repository exploration is required.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100