Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matt-tyler-qoria opened this issue Mar 13, 2024 · 2 comments
Open

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

matt-tyler-qoria opened this issue Mar 13, 2024 · 2 comments
Labels
generator Issues or improvements relater to generation capabilities. help wanted Issue caused by core project dependency modules or library type:bug A broken experience
Milestone

Comments

@matt-tyler-qoria
Copy link

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.

@andrueastman
Copy link
Member

Thanks for raising this @matt-tyler-qoria

Looks like the generator prevents the addition of enum options if the enum option value or name is an empty string.

To fix this we need to

  • remove the empty string filter at
    .Where(static x => !x.Value.Equals("null", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(x.Value))
  • initialize a default name for option when the name is empty too at
    Name = (optionDescription?.Name is string name && !string.IsNullOrEmpty(name) ?
    so that its not removed
  • Add unit tests to validate/catch the change

Any chance you'd be willing to submit a PR with a unit test to fix this?

@andrueastman andrueastman added type:bug A broken experience generator Issues or improvements relater to generation capabilities. Needs: Author Feedback labels Mar 18, 2024
@andrueastman andrueastman added this to the Backlog milestone Mar 18, 2024
@baywet baywet added the help wanted Issue caused by core project dependency modules or library label Mar 18, 2024
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generator Issues or improvements relater to generation capabilities. help wanted Issue caused by core project dependency modules or library type:bug A broken experience
Projects
Status: Todo 📃
Development

No branches or pull requests

3 participants