From ee074c46088a2be2a79c23754d555b878cf1e0a9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Oct 2025 12:40:16 +0000 Subject: [PATCH 1/3] Initial plan From 843dcdf726f968e2cff407931b3b1c73fa81b79f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Oct 2025 12:46:50 +0000 Subject: [PATCH 2/3] Fix enum values in FormData to use toJson method like query parameters Co-authored-by: trevorwang <121966+trevorwang@users.noreply.github.com> --- generator/lib/src/generator.dart | 13 +++---------- generator/test/src/generator_test_src.dart | 6 ++---- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index fac0d8670..04e7a4799 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -2455,7 +2455,7 @@ MultipartFile.fromBytes(i, _isExactly(BuiltList, innerType)))) { var value = ''; if (innerType != null && _isEnum(innerType)) { - value = 'i'; + value = _hasToJson(innerType) ? 'i.toJson()' : 'i.name'; } else if (_isBasicType(innerType)) { value = 'i'; if (innerType != null && !_isExactly(String, innerType)) { @@ -2554,15 +2554,8 @@ MultipartFile.fromFileSync(i.path, refer(p.displayName) else if (_isEnum(p.type)) _hasToJson(p.type) - ? refer(p.displayName) - .property('toJson') - .call([]) - .ifNullThen( - refer( - p.displayName, - ).property('toString').call([]), - ) - : refer(p.displayName).property('toString').call([]) + ? refer(p.displayName).property('toJson').call([]) + : refer(p.displayName).property('name') else refer(p.displayName).property('toString').call([]), ]), diff --git a/generator/test/src/generator_test_src.dart b/generator/test/src/generator_test_src.dart index 77d12eb4e..3ee934f43 100644 --- a/generator/test/src/generator_test_src.dart +++ b/generator/test/src/generator_test_src.dart @@ -1344,12 +1344,10 @@ enum TestEnumWithToJson { final _data = FormData.fromMap(map); ''', contains: true) @ShouldGenerate(''' -_data.fields.add(MapEntry('enumValue', enumValue.toString())); +_data.fields.add(MapEntry('enumValue', enumValue.name)); ''', contains: true) @ShouldGenerate(''' - _data.fields.add( - MapEntry('enumValue', enumValue.toJson() ?? enumValue.toString()), - ); + _data.fields.add(MapEntry('enumValue', enumValue.toJson())); ''', contains: true) @ShouldGenerate(''' final _data = FormData(); From a883c9b7971e40861424db026122ce4652178c72 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Oct 2025 12:51:48 +0000 Subject: [PATCH 3/3] Add helper methods to reduce code duplication for enum serialization Co-authored-by: trevorwang <121966+trevorwang@users.noreply.github.com> --- generator/lib/src/generator.dart | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/generator/lib/src/generator.dart b/generator/lib/src/generator.dart index 04e7a4799..a6f38d683 100644 --- a/generator/lib/src/generator.dart +++ b/generator/lib/src/generator.dart @@ -1871,6 +1871,20 @@ if (T != dynamic && return dartType.element3.getMethod2('toJson') != null; } + /// Gets the expression for serializing an enum value in FormData as a string. + /// Uses toJson() if available, otherwise uses .name. + String _getEnumValueExpression(DartType enumType, String variableName) { + return _hasToJson(enumType) ? '$variableName.toJson()' : '$variableName.name'; + } + + /// Gets the Reference for serializing an enum value in FormData. + /// Uses toJson() if available, otherwise uses .name. + Expression _getEnumValueReference(DartType enumType, String variableName) { + return _hasToJson(enumType) + ? refer(variableName).property('toJson').call([]) + : refer(variableName).property('name'); + } + /// Generates the query parameters code block. void _generateQueries( MethodElement2 m, @@ -2455,7 +2469,7 @@ MultipartFile.fromBytes(i, _isExactly(BuiltList, innerType)))) { var value = ''; if (innerType != null && _isEnum(innerType)) { - value = _hasToJson(innerType) ? 'i.toJson()' : 'i.name'; + value = _getEnumValueExpression(innerType, 'i'); } else if (_isBasicType(innerType)) { value = 'i'; if (innerType != null && !_isExactly(String, innerType)) { @@ -2553,9 +2567,7 @@ MultipartFile.fromFileSync(i.path, if (_isExactly(String, p.type)) refer(p.displayName) else if (_isEnum(p.type)) - _hasToJson(p.type) - ? refer(p.displayName).property('toJson').call([]) - : refer(p.displayName).property('name') + _getEnumValueReference(p.type, p.displayName) else refer(p.displayName).property('toString').call([]), ]),