Skip to content

Commit d722c05

Browse files
committed
[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`).
1 parent 20e3283 commit d722c05

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

  • modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api
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}}

0 commit comments

Comments
 (0)