microsoft/kiota

Generator does not handle empty strings in enum fields (Go)

Open

#4,329 opened on Mar 13, 2024

View on GitHub
 (2 comments) (0 reactions) (0 assignees)C# (326 forks)auto 404
generatorhelp wantedtype:bug

Repository metrics

Stars
 (3,783 stars)
PR merge metrics
 (PR metrics pending)

Description

We are trying to generate a Go client from the Clever API openapi specification available at the below link:

https://github.com/Clever/swagger-api/blob/master/v3.0.yml

This includes many enum definitions that follow this basic pattern.

gender:
  enum:
  - M
  - F
  - X
  - ""
  type: string

This is resulting in the following generated code

const (
    M_STUDENT_GENDER Student_gender = iota
    F_STUDENT_GENDER
    X_STUDENT_GENDER
)

func (i Student_gender) String() string {
    return []string{"M", "F", "X"}[i]
}
func ParseStudent_gender(v string) (any, error) {
    result := M_STUDENT_GENDER
    switch v {
        case "M":
            result = M_STUDENT_GENDER
        case "F":
            result = F_STUDENT_GENDER
        case "X":
            result = X_STUDENT_GENDER
        default:
            return 0, errors.New("Unknown Student_gender value: " + v)
    }
    return &result, nil
}

This results in the valid value of "" being interpreted as an error.

Contributor guide