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

Bug in toJSON Function with removeEnumPrefix=true and unrecognizedEnumValue=0 Option Combination #1086

Closed
kanziw opened this issue Aug 12, 2024 · 2 comments · Fixed by #1089 · May be fixed by https-github-com-okeamah/identity-protocol#5 or habyyb/CD-AdvancedDigitalIQ#3
Labels

Comments

@kanziw
Copy link
Contributor

kanziw commented Aug 12, 2024

I encountered a bug in the toJSON function generated by ts-proto when using both the removeEnumPrefix=true and unrecognizedEnumValue=0 options. The issue occurs with the following proto definition:

enum Quality {
  QUALITY_UNSPECIFIED = 0;
  QUALITY_STANDARD = 1;
  QUALITY_HD = 2;
}

When these options are applied, the generated qualityToJSON function looks like this:

export function qualityToJSON(object: Quality): string {
  switch (object) {
    case Quality.UNSPECIFIED:
      return "QUALITY_UNSPECIFIED";
    case Quality.STANDARD:
      return "QUALITY_STANDARD";
    case Quality.HD:
      return "QUALITY_HD";
    default:
      return "UNSPECIFIED";
  }
}

The problem lies in the default case, where the function returns "UNSPECIFIED" instead of "QUALITY_UNSPECIFIED". This leads to incorrect JSON serialization for unrecognized enum values. The default case should return "QUALITY_UNSPECIFIED".

@kanziw kanziw changed the title Fix Bug in toJSON Function with removeEnumPrefix=true and unrecognizedEnumValue=0 Option Combination Bug in toJSON Function with removeEnumPrefix=true and unrecognizedEnumValue=0 Option Combination Aug 12, 2024
@stephenh
Copy link
Owner

Hi @kanziw , you're right, that sounds like a bug!

If you could add a test case, maybe to this test:

https://github.com/stephenh/ts-proto/tree/main/integration/remove-enum-prefix

The bug is probably around in here:

https://github.com/stephenh/ts-proto/blob/main/src/enums.ts#L118

If you could submit a PR, that'd be great! Thanks!

stephenh pushed a commit that referenced this issue Aug 15, 2024
## [1.181.2](v1.181.1...v1.181.2) (2024-08-15)

### Bug Fixes

* toJSON Function with `removeEnumPrefix=true` and `unrecognizedEnumValue=0` Options ([#1089](#1089)) ([2401490](2401490)), closes [#1086](#1086) [#1086](#1086)
@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 1.181.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment