Skip to content

Commit 209f7d1

Browse files
authored
[dart-dio] Fix json_serializable serialize template to support hasFormParams (#23579)
* [dart-dio] Fix `json_serializable` serialize template to support `hasFormParams` The `json_serializable` dart-dio serialize template only handled `{{#bodyParam}}`, which meant operations using `application/x-www-form-urlencoded` or `multipart/form-data` produced an empty `try {}` block and a `_bodyData` that was never assigned. The request body was silently dropped. This mirrors the handling already present in the `built_value` template for the same generator, but without `built_value`-specific `encodeFormParameter` / `_serializers` calls since `json_serializable` passes values directly (consistent with its `query_param.mustache`). - `application/x-www-form-urlencoded` -> `Map<String, dynamic>` - `multipart/form-data` -> `FormData.fromMap(<String, dynamic>{...})` - Optional / non-required + non-nullable params are conditionally included with `if (paramName != null)`. - The existing `bodyParam` branch is preserved, just properly indented (to match `built_value`). * [dart-dio] Update samples after `json_serializable` form-param fix Regenerates the `petstore_client_lib_fake-json_serializable` sample via `./bin/generate-samples.sh bin/configs/dart-dio*` to reflect the updated `serialize.mustache` template. Previously empty `try {}` blocks in operations using `application/x-www-form-urlencoded` or `multipart/form-data` (e.g. `updatePetWithForm`, `uploadFile`, `testEndpointParameters`, `testEnumParameters`, `testQueryParameterCollectionFormat`) now correctly populate `_bodyData`. Existing body-param operations are regenerated with consistent indentation.
1 parent ba91cc6 commit 209f7d1

7 files changed

Lines changed: 103 additions & 25 deletions

File tree

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
{{#bodyParam}}_bodyData=jsonEncode({{{paramName}}});{{/bodyParam}}
1+
{{#hasFormParams}}
2+
{{#isMultipart}}
3+
_bodyData = FormData.fromMap(<String, dynamic>{
4+
{{#formParams}}
5+
{{^required}}{{^isNullable}}if ({{{paramName}}} != null) {{/isNullable}}{{/required}}r'{{{baseName}}}': {{#isFile}}{{{paramName}}}{{#isArray}}.toList(){{/isArray}}{{/isFile}}{{^isFile}}{{{paramName}}}{{/isFile}},
6+
{{/formParams}}
7+
});
8+
{{/isMultipart}}
9+
{{^isMultipart}}
10+
_bodyData = <String, dynamic>{
11+
{{#formParams}}
12+
{{^required}}{{^isNullable}}if ({{{paramName}}} != null) {{/isNullable}}{{/required}}r'{{{baseName}}}': {{{paramName}}},
13+
{{/formParams}}
14+
};
15+
{{/isMultipart}}
16+
{{/hasFormParams}}
17+
{{#bodyParam}}
18+
_bodyData = jsonEncode({{{paramName}}});
19+
{{/bodyParam}}

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/another_fake_api.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class AnotherFakeApi {
5757
dynamic _bodyData;
5858

5959
try {
60-
_bodyData=jsonEncode(modelClient);
60+
_bodyData = jsonEncode(modelClient);
61+
6162
} catch(error, stackTrace) {
6263
throw DioException(
6364
requestOptions: _options.compose(

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_api.dart

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ _responseData = rawData == null ? null : deserialize<HealthCheckResult, HealthCh
293293
dynamic _bodyData;
294294

295295
try {
296-
_bodyData=jsonEncode(pet);
296+
_bodyData = jsonEncode(pet);
297+
297298
} catch(error, stackTrace) {
298299
throw DioException(
299300
requestOptions: _options.compose(
@@ -360,7 +361,8 @@ _bodyData=jsonEncode(pet);
360361
dynamic _bodyData;
361362

362363
try {
363-
_bodyData=jsonEncode(body);
364+
_bodyData = jsonEncode(body);
365+
364366
} catch(error, stackTrace) {
365367
throw DioException(
366368
requestOptions: _options.compose(
@@ -450,7 +452,8 @@ _responseData = rawData == null ? null : deserialize<bool, bool>(rawData, 'bool'
450452
dynamic _bodyData;
451453

452454
try {
453-
_bodyData=jsonEncode(outerComposite);
455+
_bodyData = jsonEncode(outerComposite);
456+
454457
} catch(error, stackTrace) {
455458
throw DioException(
456459
requestOptions: _options.compose(
@@ -540,7 +543,8 @@ _responseData = rawData == null ? null : deserialize<OuterComposite, OuterCompos
540543
dynamic _bodyData;
541544

542545
try {
543-
_bodyData=jsonEncode(body);
546+
_bodyData = jsonEncode(body);
547+
544548
} catch(error, stackTrace) {
545549
throw DioException(
546550
requestOptions: _options.compose(
@@ -630,7 +634,8 @@ _responseData = rawData == null ? null : deserialize<num, num>(rawData, 'num', g
630634
dynamic _bodyData;
631635

632636
try {
633-
_bodyData=jsonEncode(body);
637+
_bodyData = jsonEncode(body);
638+
634639
} catch(error, stackTrace) {
635640
throw DioException(
636641
requestOptions: _options.compose(
@@ -720,7 +725,8 @@ _responseData = rawData == null ? null : deserialize<String, String>(rawData, 'S
720725
dynamic _bodyData;
721726

722727
try {
723-
_bodyData=jsonEncode(outerObjectWithEnumProperty);
728+
_bodyData = jsonEncode(outerObjectWithEnumProperty);
729+
724730
} catch(error, stackTrace) {
725731
throw DioException(
726732
requestOptions: _options.compose(
@@ -810,7 +816,8 @@ _responseData = rawData == null ? null : deserialize<OuterObjectWithEnumProperty
810816
dynamic _bodyData;
811817

812818
try {
813-
_bodyData=jsonEncode(requestBody);
819+
_bodyData = jsonEncode(requestBody);
820+
814821
} catch(error, stackTrace) {
815822
throw DioException(
816823
requestOptions: _options.compose(
@@ -875,7 +882,8 @@ _bodyData=jsonEncode(requestBody);
875882
dynamic _bodyData;
876883

877884
try {
878-
_bodyData=jsonEncode(body);
885+
_bodyData = jsonEncode(body);
886+
879887
} catch(error, stackTrace) {
880888
throw DioException(
881889
requestOptions: _options.compose(
@@ -940,7 +948,8 @@ _bodyData=jsonEncode(body);
940948
dynamic _bodyData;
941949

942950
try {
943-
_bodyData=jsonEncode(fileSchemaTestClass);
951+
_bodyData = jsonEncode(fileSchemaTestClass);
952+
944953
} catch(error, stackTrace) {
945954
throw DioException(
946955
requestOptions: _options.compose(
@@ -1011,7 +1020,8 @@ _bodyData=jsonEncode(fileSchemaTestClass);
10111020
dynamic _bodyData;
10121021

10131022
try {
1014-
_bodyData=jsonEncode(user);
1023+
_bodyData = jsonEncode(user);
1024+
10151025
} catch(error, stackTrace) {
10161026
throw DioException(
10171027
requestOptions: _options.compose(
@@ -1078,7 +1088,8 @@ _bodyData=jsonEncode(user);
10781088
dynamic _bodyData;
10791089

10801090
try {
1081-
_bodyData=jsonEncode(modelClient);
1091+
_bodyData = jsonEncode(modelClient);
1092+
10821093
} catch(error, stackTrace) {
10831094
throw DioException(
10841095
requestOptions: _options.compose(
@@ -1200,6 +1211,22 @@ _responseData = rawData == null ? null : deserialize<ModelClient, ModelClient>(r
12001211
dynamic _bodyData;
12011212

12021213
try {
1214+
_bodyData = <String, dynamic>{
1215+
if (integer != null) r'integer': integer,
1216+
if (int32 != null) r'int32': int32,
1217+
if (int64 != null) r'int64': int64,
1218+
r'number': number,
1219+
if (float != null) r'float': float,
1220+
r'double': double_,
1221+
if (string != null) r'string': string,
1222+
r'pattern_without_delimiter': patternWithoutDelimiter,
1223+
r'byte': byte,
1224+
if (binary != null) r'binary': binary,
1225+
if (date != null) r'date': date,
1226+
if (dateTime != null) r'dateTime': dateTime,
1227+
if (password != null) r'password': password,
1228+
if (callback != null) r'callback': callback,
1229+
};
12031230

12041231
} catch(error, stackTrace) {
12051232
throw DioException(
@@ -1291,6 +1318,10 @@ _responseData = rawData == null ? null : deserialize<ModelClient, ModelClient>(r
12911318
dynamic _bodyData;
12921319

12931320
try {
1321+
_bodyData = <String, dynamic>{
1322+
if (enumFormStringArray != null) r'enum_form_string_array': enumFormStringArray,
1323+
if (enumFormString != null) r'enum_form_string': enumFormString,
1324+
};
12941325

12951326
} catch(error, stackTrace) {
12961327
throw DioException(
@@ -1431,7 +1462,8 @@ _responseData = rawData == null ? null : deserialize<ModelClient, ModelClient>(r
14311462
dynamic _bodyData;
14321463

14331464
try {
1434-
_bodyData=jsonEncode(requestBody);
1465+
_bodyData = jsonEncode(requestBody);
1466+
14351467
} catch(error, stackTrace) {
14361468
throw DioException(
14371469
requestOptions: _options.compose(
@@ -1496,7 +1528,8 @@ _bodyData=jsonEncode(requestBody);
14961528
dynamic _bodyData;
14971529

14981530
try {
1499-
_bodyData=jsonEncode(testInlineFreeformAdditionalPropertiesRequest);
1531+
_bodyData = jsonEncode(testInlineFreeformAdditionalPropertiesRequest);
1532+
15001533
} catch(error, stackTrace) {
15011534
throw DioException(
15021535
requestOptions: _options.compose(
@@ -1563,6 +1596,10 @@ _bodyData=jsonEncode(testInlineFreeformAdditionalPropertiesRequest);
15631596
dynamic _bodyData;
15641597

15651598
try {
1599+
_bodyData = <String, dynamic>{
1600+
r'param': param,
1601+
r'param2': param2,
1602+
};
15661603

15671604
} catch(error, stackTrace) {
15681605
throw DioException(
@@ -1628,7 +1665,8 @@ _bodyData=jsonEncode(testInlineFreeformAdditionalPropertiesRequest);
16281665
dynamic _bodyData;
16291666

16301667
try {
1631-
_bodyData=jsonEncode(childWithNullable);
1668+
_bodyData = jsonEncode(childWithNullable);
1669+
16321670
} catch(error, stackTrace) {
16331671
throw DioException(
16341672
requestOptions: _options.compose(
@@ -1763,7 +1801,8 @@ _bodyData=jsonEncode(childWithNullable);
17631801
dynamic _bodyData;
17641802

17651803
try {
1766-
_bodyData=jsonEncode(requestBody);
1804+
_bodyData = jsonEncode(requestBody);
1805+
17671806
} catch(error, stackTrace) {
17681807
throw DioException(
17691808
requestOptions: _options.compose(

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/fake_classname_tags123_api.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class FakeClassnameTags123Api {
6464
dynamic _bodyData;
6565

6666
try {
67-
_bodyData=jsonEncode(modelClient);
67+
_bodyData = jsonEncode(modelClient);
68+
6869
} catch(error, stackTrace) {
6970
throw DioException(
7071
requestOptions: _options.compose(

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/pet_api.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class PetApi {
6363
dynamic _bodyData;
6464

6565
try {
66-
_bodyData=jsonEncode(pet);
66+
_bodyData = jsonEncode(pet);
67+
6768
} catch(error, stackTrace) {
6869
throw DioException(
6970
requestOptions: _options.compose(
@@ -432,7 +433,8 @@ _responseData = rawData == null ? null : deserialize<Pet, Pet>(rawData, 'Pet', g
432433
dynamic _bodyData;
433434

434435
try {
435-
_bodyData=jsonEncode(pet);
436+
_bodyData = jsonEncode(pet);
437+
436438
} catch(error, stackTrace) {
437439
throw DioException(
438440
requestOptions: _options.compose(
@@ -506,6 +508,10 @@ _bodyData=jsonEncode(pet);
506508
dynamic _bodyData;
507509

508510
try {
511+
_bodyData = <String, dynamic>{
512+
if (name != null) r'name': name,
513+
if (status != null) r'status': status,
514+
};
509515

510516
} catch(error, stackTrace) {
511517
throw DioException(
@@ -580,6 +586,10 @@ _bodyData=jsonEncode(pet);
580586
dynamic _bodyData;
581587

582588
try {
589+
_bodyData = FormData.fromMap(<String, dynamic>{
590+
if (additionalMetadata != null) r'additionalMetadata': additionalMetadata,
591+
if (file != null) r'file': file,
592+
});
583593

584594
} catch(error, stackTrace) {
585595
throw DioException(
@@ -679,6 +689,10 @@ _responseData = rawData == null ? null : deserialize<ApiResponse, ApiResponse>(r
679689
dynamic _bodyData;
680690

681691
try {
692+
_bodyData = FormData.fromMap(<String, dynamic>{
693+
if (additionalMetadata != null) r'additionalMetadata': additionalMetadata,
694+
r'requiredFile': requiredFile,
695+
});
682696

683697
} catch(error, stackTrace) {
684698
throw DioException(

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/store_api.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ _responseData = rawData == null ? null : deserialize<Order, Order>(rawData, 'Ord
253253
dynamic _bodyData;
254254

255255
try {
256-
_bodyData=jsonEncode(order);
256+
_bodyData = jsonEncode(order);
257+
257258
} catch(error, stackTrace) {
258259
throw DioException(
259260
requestOptions: _options.compose(

samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/lib/src/api/user_api.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class UserApi {
5757
dynamic _bodyData;
5858

5959
try {
60-
_bodyData=jsonEncode(user);
60+
_bodyData = jsonEncode(user);
61+
6162
} catch(error, stackTrace) {
6263
throw DioException(
6364
requestOptions: _options.compose(
@@ -122,7 +123,8 @@ _bodyData=jsonEncode(user);
122123
dynamic _bodyData;
123124

124125
try {
125-
_bodyData=jsonEncode(user);
126+
_bodyData = jsonEncode(user);
127+
126128
} catch(error, stackTrace) {
127129
throw DioException(
128130
requestOptions: _options.compose(
@@ -187,7 +189,8 @@ _bodyData=jsonEncode(user);
187189
dynamic _bodyData;
188190

189191
try {
190-
_bodyData=jsonEncode(user);
192+
_bodyData = jsonEncode(user);
193+
191194
} catch(error, stackTrace) {
192195
throw DioException(
193196
requestOptions: _options.compose(
@@ -498,7 +501,8 @@ _responseData = rawData == null ? null : deserialize<String, String>(rawData, 'S
498501
dynamic _bodyData;
499502

500503
try {
501-
_bodyData=jsonEncode(user);
504+
_bodyData = jsonEncode(user);
505+
502506
} catch(error, stackTrace) {
503507
throw DioException(
504508
requestOptions: _options.compose(

0 commit comments

Comments
 (0)