Repository: google/json_serializable.dart

Issue #825: @JsonKey and @JsonValue combined with defaultValue doesn't seem to work
Created: 2021-03-07T12:23:38Z
Dart SDK version: 2.12.0 (stable) (Thu Feb 25 19:50:53 2021 +0100) on "macos_x64"
My class contains:
@jsonkey(name: 'activityLevel', defaultValue: ActivityLevel.unknown)
The enum is:
enum ActivityLevel {
@JsonValue('LEVEL1')
level1,
@JsonValue('LEVEL2')
level2,
@JsonValue('LEVEL3')
level3,
@JsonValue('LEVEL4')
level4,
@JsonValue('LEVEL5')
level5,
@JsonValue('UNKNOWN')
unknown,
}
This results in:
SEVERE] json_serializable:json_serializable on lib/model/user.dart:
line 1, column 14755 of package:newme_app/model/enum.dart: Could not resolve annotation for ActivityLevel level1
╷
1 │ @JsonValue('LEVEL1')
│ ^^^^^^^^^^^^^^^^^^^^
╵
When I comment all the @JsonValue lines, it works but ofc it won't translate correctly on runtime. Also, when I leave the @jsonkey values but I remove the defaultValue, it also works. I'd like to really include the defaultValue, but then the code won't build....so why do I need a different value on the enum key from the value? Backwards compatibility with old data...yup I know...
When I manually edit the .g.dart to add the 'LEVEL1' etc, it works correctly...

Duplicate issue #847: Code generation is unstable with enum classes
Created: 2021-03-23T05:13:11Z
We started migrating our Flutter app to use the sound null safety a few days ago. After upgrading our dependencies (json_serializable to version 4.1.0 and others along with it) we got an intermittent issue on the code generation step, where one value of a single enum class annotated with @JsonValue throws an error. Other classes and enums on the app don't present this behavior.
[FINE] json_serializable:json_serializable on lib/.../model.dart:Running JsonSerializableGenerator - 1 of 2
[WARNING] json_serializable:json_serializable on lib/.../model.dart:
An unexpected error was thrown trying to get location information on `MyEnumClass enumValue` (ConstFieldElementImpl_EnumValue).

InconsistentAnalysisException: Requested result might be inconsistent with previously returned results

package:analyzer/src/dart/analysis/session.dart 145:7      AnalysisSessionImpl._checkConsistency
package:analyzer/src/dart/analysis/session.dart 92:5       AnalysisSessionImpl.getParsedLibraryByElement
package:source_gen/src/type_checker.dart 304:12            UnresolvedAnnotationException._findSpan
package:source_gen/src/type_checker.dart 342:24            new UnresolvedAnnotationException._from
package:source_gen/src/type_checker.dart 113:43            TypeChecker._computeConstantValue
package:source_gen/src/type_checker.dart 138:21            TypeChecker._annotationsWhere.sync_op
dart:core                                                  Iterable.isEmpty
package:source_gen/src/type_checker.dart 94:20             TypeChecker.firstAnnotationOfExact
package:json_serializable/src/utils.dart 142:50            enumFieldsMap._generateEntry
dart:core                                                  new Map.fromEntries
package:json_serializable/src/utils.dart 170:9             enumFieldsMap
package:json_serializable/src/utils.dart 182:5             iterateEnumFields
package:json_serializable/src/json_key_utils.dart 123:11   _from._annotationValue
package:json_serializable/src/json_key_utils.dart 183:39   _from
package:json_serializable/src/json_key_utils.dart 19:32    jsonKeyForField
package:json_serializable/src/helper_core.dart 51:47       HelperCore.jsonKeyFor
package:json_serializable/src/generator_helper.dart 71:20  GeneratorHelper.generate.sync_op.<fn>
dart:collection                                            ListMixin.fold
package:json_serializable/src/generator_helper.dart 61:43  GeneratorHelper.generate.sync_op

[SEVERE] json_serializable:json_serializable on lib/.../model.dart:

Could not resolve annotation for `MyEnumClass enumValue`.

package:source_gen/src/type_checker.dart 113:7             TypeChecker._computeConstantValue
package:source_gen/src/type_checker.dart 138:21            TypeChecker._annotationsWhere.sync_op
dart:core                                                  Iterable.isEmpty
package:source_gen/src/type_checker.dart 94:20             TypeChecker.firstAnnotationOfExact
package:json_serializable/src/utils.dart 142:50            enumFieldsMap._generateEntry
dart:core                                                  new Map.fromEntries
package:json_serializable/src/utils.dart 170:9             enumFieldsMap
package:json_serializable/src/utils.dart 182:5             iterateEnumFields
package:json_serializable/src/json_key_utils.dart 123:11   _from._annotationValue
package:json_serializable/src/json_key_utils.dart 183:39   _from
package:json_serializable/src/json_key_utils.dart 19:32    jsonKeyForField
package:json_serializable/src/helper_core.dart 51:47       HelperCore.jsonKeyFor
package:json_serializable/src/generator_helper.dart 71:20  GeneratorHelper.generate.sync_op.<fn>
dart:collection                                            ListMixin.fold
package:json_serializable/src/generator_helper.dart 61:43  GeneratorHelper.generate.sync_op
Example source code
After some trial and error I was able to extract and reduce the code to something close to a minimal reproducible example (which fails less often, but still fails). You can find the source here: https://github.com/gustavogamino/json_enum_issue
Executing make it-fail will loop through the code generation indefinitely until the error occurs.
Additional information
Dart version
Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "macos_x64"
Dependencies:
# direct
json_annotation: 4.0.1
json_serializable: 4.1.0
build_runner: 1.12.2

# transitive
analyzer: 1.2.0
source_gen: 1.0.0

Closing comment (2021-03-23T15:21:22Z): duplicate of #825