[dart-dio] Fix json_serializable serialize template to support hasFormParams#23579
Open
Bazni wants to merge 2 commits intoOpenAPITools:masterfrom
Open
[dart-dio] Fix json_serializable serialize template to support hasFormParams#23579Bazni wants to merge 2 commits intoOpenAPITools:masterfrom
json_serializable serialize template to support hasFormParams#23579Bazni wants to merge 2 commits intoOpenAPITools:masterfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
json_serializabledart-dioserialize template only handled{{#bodyParam}}, so any operation whose request body wasapplication/x-www-form-urlencodedormultipart/form-dataproduced an emptytry {}block:_bodyDatawas declared but never assigned, and the form fields were silently dropped. Only thebuilt_valueserialization library was correctly handling form params.This PR adds the missing
{{#hasFormParams}}handling toserialization/json_serializable/api/serialize.mustache, mirroring the pattern from thebuilt_valuecounterpart but withoutbuilt_value-specificencodeFormParameter/_serializerscalls (consistent with howjson_serializable'squery_param.mustachealready just outputs{{{paramName}}}).Similar to #20314, but for the dart-dio generator.
Before
After
For
multipart/form-data(uploadFile), the map is wrapped inFormData.fromMap(...)from thediopackage, andMultipartFilevalues are passed through directly ({{#isFile}}).Reproduction
Minimal spec with an
application/x-www-form-urlencodedbody (e.g.updatePetWithFormfrom the petstore fake spec) generated with:Before this PR, the generated
_bodyDatais alwaysnull. After, it is a populatedMap<String, dynamic>(orFormData).Changes
modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/json_serializable/api/serialize.mustache: addhasFormParamshandling for bothisMultipartand non-multipart cases; preserve the existingbodyParambranch (with proper indentation so the generated code is consistent withbuilt_value).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_valuesamples, docs, and other generators are unaffected.PR checklist
./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).master.cc @kuhnroyal @josh-burton @amondnet @ahmednfwela (dart-dio technical committee)
Summary by cubic
Fixes form parameter serialization in
dart-diowithjson_serializable, soapplication/x-www-form-urlencodedandmultipart/form-datarequests populate_bodyDatainstead of sending null.{{#hasFormParams}}handling inserialization/json_serializable/api/serialize.mustache: build<String, dynamic>{...}for urlencoded andFormData.fromMap(...)for multipart; passMultipartFilevalues through; include optional fields only when non-null.{{#bodyParam}}branch with consistent indentation.petstore_client_lib_fake-json_serializablesamples; operations likeupdatePetWithFormanduploadFilenow serialize form bodies correctly.Written for commit 71e7ead. Summary will update on new commits.