fix: dart_mappable default enum value#451
Merged
Merged
Conversation
Carapacik
approved these changes
Apr 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes enum generation for dart_mappable when
unknown_enum_value: trueis enabled.Problem
Generated enums were emitting:
@MappableEnum(defaultValue: 'unknown').However,
dart_mappableexpects the actual enum case, not the serialized string value. Because of this, the generated mapper could not resolve the fallback enum index and still generated a throwing decode path for unknown backend values.This caused older clients to crash when the backend introduced a new enum value.
Fix
The generator now emits the proper enum case reference instead of the encoded string value:
@MappableEnum(defaultValue: AnnouncementType.unknown)This restores the expected dart_mappable behavior where unknown enum values safely fall back to the generated unknown enum case instead of throwing MapperException.unknownEnumValue(value).
Current behavior
It looks like @MappableEnum(defaultValue: ...) does not fallback when decoding an unknown enum value, and instead still throws MapperException.unknownEnumValue(...).
Example
This enum:
generates
Problem
If the backend later returns a new enum value, for example:
{ "type": "education" }decoding fails with:MapperException.unknownEnumValue(value)instead of falling back to:Test.unknown.Expected behavior
When defaultValue is set, I would expect unknown enum values to decode to that default value, e.g.:
This is especially important for API clients, where backend enums may evolve over time and older app versions should remain backward-compatible instead of crashing on new enum values.