graphql-code-generator-community icon indicating copy to clipboard operation
graphql-code-generator-community copied to clipboard

Flutter json_serializable use `JsonValue` as the json key of Enum

Open whlsxl opened this issue 1 year ago • 1 comments

Which packages are impacted by your issue?

@graphql-codegen/flutter-freezed

Describe the bug

The current version of json_serializable uses JsonValue as the Enum json serialize key.

Your Example Website or App

https://stackblitz.com/edit/github-mc3hpg?file=app_models.dart

Steps to Reproduce the Bug or Issue

Schema.graphql

enum BookCategory {
  BusinessCareer
  Family
}

Generate

enum BookCategory {
  @JsonKey(name: 'BusinessCareer')
  businessCareer,
  @JsonKey(name: 'Family')
  family,
}

freezed generate


const _$BookCategoryEnumMap = {
  BookCategory.businessCareer: 'businessCareer',
  BookCategory.family: 'family',
}

freezed use $enumDecode(_$BookCategoryEnumMap, e); to trans from json.

got error

Invalid argument(s): `BusinessCareer` is not one of the supported values: businessCareer, family,

Expected behavior

enum BookCategory {
  @JsonValue('BusinessCareer')
  businessCareer,
  @JsonValue('Family')
  family,
}

Screenshots or Videos

No response

Platform

    "@graphql-codegen/cli": "5.0.2",
    "@graphql-codegen/flutter-freezed": "^4.0.0",
    "@graphql-codegen/introspection": "4.0.3",
    "@graphql-codegen/typescript": "4.0.6",
    "@graphql-codegen/typescript-resolvers": "4.0.6",

Codegen Config File

No response

Additional context

No response

whlsxl avatar May 11 '24 03:05 whlsxl

@whlsxl The fix for this issue is quite simple...

https://github.com/dotansimha/graphql-code-generator-community/blob/main/packages/plugins/dart/flutter-freezed/src/freezed-declaration-blocks/enum-block.ts#L71

Thanks for contributing

Parables avatar Jun 09 '25 03:06 Parables