Skip to content

[dart-dio] Fix json_serializable serialize template to support hasFormParams#23579

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
Bazni:fix/dart-dio-json-serializable-form-params
Apr 21, 2026
Merged

[dart-dio] Fix json_serializable serialize template to support hasFormParams#23579
wing328 merged 2 commits intoOpenAPITools:masterfrom
Bazni:fix/dart-dio-json-serializable-form-params

Conversation

@Bazni
Copy link
Copy Markdown
Contributor

@Bazni Bazni commented Apr 17, 2026

Summary

The json_serializable dart-dio serialize template only handled {{#bodyParam}}, so any operation whose request body was application/x-www-form-urlencoded or multipart/form-data produced an empty try {} block: _bodyData was declared but never assigned, and the form fields were silently dropped. Only the built_value serialization library was correctly handling form params.

This PR adds the missing {{#hasFormParams}} handling to serialization/json_serializable/api/serialize.mustache, mirroring the pattern from the built_value counterpart but without built_value-specific encodeFormParameter / _serializers calls (consistent with how json_serializable's query_param.mustache already just outputs {{{paramName}}}).

Similar to #20314, but for the dart-dio generator.

Before

// updatePetWithForm — application/x-www-form-urlencoded
dynamic _bodyData;

try {
  // empty — form fields lost
} catch (error, stackTrace) { ... }

final _response = await _dio.request<Object>(
  _path,
  data: _bodyData, // always null
  ...
);

After

dynamic _bodyData;

try {
  _bodyData = <String, dynamic>{
    if (name != null) r'name': name,
    if (status != null) r'status': status,
  };
} catch (error, stackTrace) { ... }

For multipart/form-data (uploadFile), the map is wrapped in FormData.fromMap(...) from the dio package, and MultipartFile values are passed through directly ({{#isFile}}).

Reproduction

Minimal spec with an application/x-www-form-urlencoded body (e.g. updatePetWithForm from the petstore fake spec) generated with:

-g dart-dio --additional-properties=serializationLibrary=json_serializable

Before this PR, the generated _bodyData is always null. After, it is a populated Map<String, dynamic> (or FormData).

Changes

  • modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api/serialize.mustache: add hasFormParams handling for both isMultipart and non-multipart cases; preserve the existing bodyParam branch (with proper indentation so the generated code is consistent with built_value).
  • Regenerate samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake-json_serializable/** via ./bin/generate-samples.sh bin/configs/dart-dio*. Previously broken operations (updatePetWithForm, uploadFile, uploadFileWithRequiredFile, testEndpointParameters, testEnumParameters, testQueryParameterCollectionFormat) now serialize the form body correctly.
  • built_value samples, docs, and other generators are unaffected.

PR checklist

  • Read the contribution guidelines.
  • PR title clearly describes the work.
  • ./mvnw clean package -DskipTests -Dmaven.javadoc.skip=true (built via -pl modules/openapi-generator-cli -am).
  • ./bin/generate-samples.sh bin/configs/dart-dio*
  • ./bin/utils/export_docs_generators.sh (no diff).
  • All changed files committed.
  • Targets master.

cc @kuhnroyal @josh-burton @amondnet @ahmednfwela (dart-dio technical committee)


Summary by cubic

Fixes form parameter serialization in dart-dio with json_serializable, so application/x-www-form-urlencoded and multipart/form-data requests set _bodyData correctly instead of sending null.

  • Bug Fixes
    • Add {{#hasFormParams}} handling in serialization/json_serializable/api/serialize.mustache: build <String, dynamic>{...} for urlencoded, FormData.fromMap(...) for multipart; pass MultipartFile values (and arrays) through; include optional params only when non-null.
    • Preserve the existing {{#bodyParam}} branch and align indentation.
    • Regenerate petstore_client_lib_fake-json_serializable samples; operations like updatePetWithForm, uploadFile, testEndpointParameters, and testEnumParameters now serialize form bodies correctly.

Written for commit 23b1b94. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 7 files

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 21, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@Bazni
Copy link
Copy Markdown
Contributor Author

Bazni commented Apr 21, 2026

Thx I will do that asap.

Edit: @wing328 Should be better now

Bazni added 2 commits April 21, 2026 17:56
…FormParams`

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`).
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.
@Bazni Bazni force-pushed the fix/dart-dio-json-serializable-form-params branch 2 times, most recently from 4eee14b to 23b1b94 Compare April 21, 2026 09:58
@wing328 wing328 merged commit 209f7d1 into OpenAPITools:master Apr 21, 2026
16 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 21, 2026

thanks for the PR which has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants