OpenAPITools / OpenAPITools/openapi-generator

[REQ] Feature Request: Option to disable enum checks on unmarshal (Golang)

Open
#13,591 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.