Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 20, 2025

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:

enum Priority {
  high(1),
  medium(2),
  low(3);
  
  const Priority(this.value);
  final int value;
  int toJson() => value;
}

Before this fix:

  • Query parameter: @Query('priority') Priority priority → generates priority=1 (uses toJson()) ✅
  • Form data: @Part() Priority priority → generates priority=Priority.high (uses toString()) ❌

After this fix:

  • Query parameter: @Query('priority') Priority priority → generates priority=1 (uses toJson()) ✅
  • Form data: @Part() Priority priority → generates priority=1 (uses toJson()) ✅

Solution

Updated the FormData enum handling code to match the query parameter behavior:

  1. Check if the enum has a toJson() method
  2. If yes, use toJson()
  3. If no, use .name

Changes Made

  1. 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)
  2. Updated FormData enum handling in two locations:

    • Single enum values: Now uses helper method to check for toJson()
    • Enum values inside lists: Now uses helper method to check for toJson()
  3. Updated test expectations to reflect the new consistent behavior

Examples

Enum without toJson

enum Status { active, inactive }

@POST('/update')
Future<void> updateStatus(@Part() Status status);

Generated code: _data.fields.add(MapEntry('status', status.name));

Result: Sends "active" or "inactive" in form data

Enum with toJson

enum Priority {
  high(1),
  medium(2),
  low(3);
  
  const Priority(this.value);
  final int value;
  int toJson() => value;
}

@POST('/task')
Future<void> createTask(@Part() Priority priority);

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
    • Triggering command: wget -qO- REDACTED (dns block)
  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)
  • https://storage.googleapis.com/flutter_infra_release/flutter/d2913632a4578ee4d0b8b1c4a69888c8a0672c4b/dart-sdk-linux-x64.zip
    • Triggering command: 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

This section details on the original issue you should resolve

<issue_title>Enum values in FormData should use toJson method, like enum values in query parameters do</issue_title>
<issue_description>Describe the bug
An enum value in form data currently uses the enum .name. An enum value in query parameters will use toJson if available.

Expected behavior
Behaviour should match and toJson should be used.</issue_description>

Comments on the Issue (you are @copilot in this section)

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.

@Copilot Copilot AI changed the title [WIP] Fix enum values in FormData to use toJson method Fix enum values in FormData to use toJson method like query parameters Oct 20, 2025
@Copilot Copilot AI requested a review from trevorwang October 20, 2025 12:57
Copilot finished work on behalf of trevorwang October 20, 2025 12:57
@trevorwang trevorwang marked this pull request as ready for review October 22, 2025 05:08
@trevorwang trevorwang merged commit 7ad6710 into master Oct 22, 2025
9 checks passed
@trevorwang trevorwang deleted the copilot/fix-enum-values-in-formdata branch October 22, 2025 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum values in FormData should use toJson method, like enum values in query parameters do

2 participants