Skip to content

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

Open
Bazni wants to merge 2 commits intoOpenAPITools:masterfrom
Bazni:fix/dart-dio-json-serializable-form-params
Open

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

Conversation

@Bazni
Copy link
Copy Markdown

@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 populate _bodyData instead of sending null.

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

Written for commit 71e7ead. Summary will update on new commits.

Bazni added 2 commits April 17, 2026 08:54
…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`).

Made-with: Cursor
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.

Made-with: Cursor
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

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.

1 participant