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

Maps with enum values do not generate fromJSON/fromPartial correctly when stringEnums=true #382

Closed
wrobbinz opened this issue Nov 10, 2021 · 1 comment · Fixed by #529
Closed
Labels

Comments

@wrobbinz
Copy link

When stringEnums flag is set to true, the generated types for a map<string, T> (where T is an enum) do not generate correctly. It seems that the generated code is not respecting that stringEnums=true and is assuming the value is a number.

TS2322: Type 'number' is not assignable to type 'SomeEnumType'.

Example:

Input

enum SomeEnumType {
  UNDEFINED_SOME_ENUM_TYPE = 0;
  DEFINED_SOME_ENUM_TYPE = 1;
}

message Entity {
  map<string, SomeEnumType> parameters = 1;
}

Output

export enum SomeEnumType {
  UNDEFINED_SOME_ENUM_TYPE = 'UNDEFINED_SOME_ENUM_TYPE',
  DEFINED_SOME_ENUM_TYPE = 'DEFINED_SOME_ENUM_TYPE',

}

export interface Entity {
  parameters: { [key: string]: SomeEnumType };
}

// Entity
fromJSON(object: any): Entity {
    const message = { ...baseEntity } as Entity;

    if (object.parameters !== undefined && object.parameters !== null) {
      Object.entries(object.parameters).forEach(([key, value]) => {
        message.parameters[key] = value as number; // <-- error is here
      });
    }
stephenh added a commit that referenced this issue Mar 13, 2022
* fix: Type Error when map contains string enums

* regenerate ts

* update bins

* Refactor detectMapType to return keyField+valueField as well.

Co-authored-by: Stephen Haberman <stephen.haberman@gmail.com>
stephenh pushed a commit that referenced this issue Mar 13, 2022
## [1.109.1](v1.109.0...v1.109.1) (2022-03-13)

### Bug Fixes

* Type Error when map contains string enums [#382](#382) ([#529](#529)) ([c2107b9](c2107b9))
@stephenh
Copy link
Owner

🎉 This issue has been resolved in version 1.109.1 🎉

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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants