Fix enum values in FormData to use toJson method like query parameters #805
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.
Problem
Enum values in FormData were using
.toString()
(which returns the enum's.name
), while enum values in query parameters correctly use.toJson()
if the enum has that method defined. This inconsistency led to different serialization behavior across parameter types.For example, given an enum with a custom
toJson()
method:Before this fix:
@Query('priority') Priority priority
→ generatespriority=1
(usestoJson()
) ✅@Part() Priority priority
→ generatespriority=Priority.high
(usestoString()
) ❌After this fix:
@Query('priority') Priority priority
→ generatespriority=1
(usestoJson()
) ✅@Part() Priority priority
→ generatespriority=1
(usestoJson()
) ✅Solution
Updated the FormData enum handling code to match the query parameter behavior:
toJson()
methodtoJson()
.name
Changes Made
Added helper methods to centralize enum serialization logic and reduce code duplication:
_getEnumValueExpression()
: For enum serialization in string expressions (List contexts)_getEnumValueReference()
: For enum serialization in code_builder References (direct field contexts)Updated FormData enum handling in two locations:
toJson()
toJson()
Updated test expectations to reflect the new consistent behavior
Examples
Enum without toJson
Generated code:
_data.fields.add(MapEntry('status', status.name));
Result: Sends "active" or "inactive" in form data
Enum with toJson
Generated code:
_data.fields.add(MapEntry('priority', priority.toJson()));
Result: Sends 1, 2, or 3 in form data
Impact
This change ensures consistent and predictable enum serialization across all parameter types in Retrofit. Enum handling in FormData now matches the behavior in query parameters, providing a unified API experience.
Note: This is a minor breaking change for users who were relying on the old buggy behavior where enum FormData fields included the enum type name (e.g., "Status.active" instead of "active"). However, this fix aligns the behavior with what users would naturally expect and with the existing behavior for query parameters.
Fixes #[issue_number]
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
dl-ssl.google.com
wget -qO- REDACTED
(dns block)esm.ubuntu.com
/usr/lib/apt/methods/https
(dns block)https://storage.googleapis.com/flutter_infra_release/flutter/d2913632a4578ee4d0b8b1c4a69888c8a0672c4b/dart-sdk-linux-x64.zip
curl --retry 3 --continue-at - --location --output /tmp/flutter/bin/cache/dart-sdk-linux-x64.zip REDACTED
(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fixes #751
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.