[dart-dio][built_value] Register BuilderFactory for nested additionalProperties shapes#24154
Merged
wing328 merged 4 commits intoJun 29, 2026
Conversation
For a property like
`{ additionalProperties: { type: array, items: { $ref: ... } } }` the
generator emits `Map<String, BuiltList<X>>` in the Dart class but never
registers the `BuiltList<X>` BuilderFactory. The only
factory-registration path that ran for additionalProperties looked at
`items.getAdditionalProperties()`, which is null for this very common
shape (a region map of arrays of $ref). built_value then fails at
runtime with `Bad state: No builder factory for BuiltList<X>` on the
first deserialization that touches the property.
Fix:
- `postProcessModelProperty` now also calls `registerNestedBuilderFactories`,
which walks the property's `items` tree top-down and registers a
factory for every container layer. Three small helpers
(`renderInnerFullType`, `renderDartType`, `renderBuilderFactory`)
compute the corresponding `FullType(...)` argument list and the
matching `XBuilder<...>` instantiation for arbitrary nesting:
`Map<String, List<X>>`, `List<Map<String, X>>`,
`Map<String, Map<String, X>>`, `List<List<X>>`, `Set<...>`, etc.
- `BuiltValueSerializer` gets a new `composite(fullTypeArgs,
builderInstantiation)` constructor that carries the pre-rendered
expressions. The existing `(isArray, uniqueItems, isMap,
isNullable, dataType)` form is unchanged -- needed because the
original model can't represent something like
`BuiltMap<String, BuiltList<X>>` (the `FullType` argument is
recursive and isn't expressible with a single `dataType` string).
`equals`/`hashCode` are extended so composite serializers dedup on
`(fullTypeArgs, builderInstantiation)` and never collide with simple
ones.
- `serializers.mustache` gets a new branch that emits the composite
fields verbatim when present; otherwise the existing
`isArray`/`isMap` dispatch runs unchanged.
Existing simple cases (direct return / parameter container types,
single-level additionalProperties already handled by the prior
branch) keep producing byte-identical output.
A new fixture `built_value_additional_properties_factory.yaml`
exercises the canonical `Map<String, List<$ref>>` shape, and a new
test
`DartDioClientCodegenTest.testNestedAdditionalPropertiesGetBuilderFactories`
asserts both the inner `BuiltList<WatchProviderEntry>` and the outer
`BuiltMap<String, BuiltList<WatchProviderEntry>>` factories appear in
the generated `serializers.dart`.
Full Dart suite: 115 tests, 0 failures, 0 regressions.
… of https://github.com/Homegan/openapi-generator into Homegan-fix/dart-dio-built-value-additional-properties-factory
Contributor
There was a problem hiding this comment.
2 issues found across 6 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart">
<violation number="1" location="samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart:204">
P1: Builder type mismatch: FullType.nullable(JsonObject) means list elements are JsonObject?, but ListBuilder<JsonObject> rejects nulls. At runtime, deserializing a list with null elements would throw.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java:730">
P2: Nested composite builder-factory generation drops `isNullable` information, so nullable nested container/value types are registered as non-nullable and can deserialize incorrectly.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Comment on lines
+204
to
+205
| const FullType(BuiltList, [FullType.nullable(JsonObject)]), | ||
| () => ListBuilder<JsonObject>(), |
Contributor
There was a problem hiding this comment.
P1: Builder type mismatch: FullType.nullable(JsonObject) means list elements are JsonObject?, but ListBuilder rejects nulls. At runtime, deserializing a list with null elements would throw.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/dart-dio/petstore_client_lib_fake/lib/src/serializers.dart, line 204:
<comment>Builder type mismatch: FullType.nullable(JsonObject) means list elements are JsonObject?, but ListBuilder<JsonObject> rejects nulls. At runtime, deserializing a list with null elements would throw.</comment>
<file context>
@@ -153,21 +193,45 @@ Serializers serializers = (_$serializers.toBuilder()
() => ListBuilder<ModelEnumClass>(),
)
+ ..addBuilderFactory(
+ const FullType(BuiltList, [FullType.nullable(JsonObject)]),
+ () => ListBuilder<JsonObject>(),
+ )
</file context>
Suggested change
| const FullType(BuiltList, [FullType.nullable(JsonObject)]), | |
| () => ListBuilder<JsonObject>(), | |
| const FullType(BuiltList, [FullType.nullable(JsonObject)]), | |
| () => ListBuilder<JsonObject?>(), |
| String collection = prop.getUniqueItems() ? "BuiltSet" : "BuiltList"; | ||
| String builder = prop.getUniqueItems() ? "SetBuilder" : "ListBuilder"; | ||
| return new BuilderFactoryExpr( | ||
| collection + ", [FullType(" + innerFullType + ")]", |
Contributor
There was a problem hiding this comment.
P2: Nested composite builder-factory generation drops isNullable information, so nullable nested container/value types are registered as non-nullable and can deserialize incorrectly.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java, line 730:
<comment>Nested composite builder-factory generation drops `isNullable` information, so nullable nested container/value types are registered as non-nullable and can deserialize incorrectly.</comment>
<file context>
@@ -661,10 +661,134 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
+ String collection = prop.getUniqueItems() ? "BuiltSet" : "BuiltList";
+ String builder = prop.getUniqueItems() ? "SetBuilder" : "ListBuilder";
+ return new BuilderFactoryExpr(
+ collection + ", [FullType(" + innerFullType + ")]",
+ builder + "<" + innerDart + ">");
+ }
</file context>
Closed
4 tasks
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.
based on #23662 with resolved merge conflicts
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Summary by cubic
Fixes missing builder factories for nested
additionalPropertiesin Dartdart-diobuilt_valueserializers. Prevents runtime errors like "Bad state: No builder factory for BuiltList" for shapes such as Map<String, List>.serializers.mustacheto emit nestedFullType(...)and matching builders.BuiltValueSerializerwith a composite mode and proper dedup viaequals/hashCode.Written for commit a7187d1. Summary will update on new commits.