Skip to content

[dart-dio][built_value] Register BuilderFactory for nested additionalProperties shapes#24154

Merged
wing328 merged 4 commits into
masterfrom
Homegan-fix/dart-dio-built-value-additional-properties-factory
Jun 29, 2026
Merged

[dart-dio][built_value] Register BuilderFactory for nested additionalProperties shapes#24154
wing328 merged 4 commits into
masterfrom
Homegan-fix/dart-dio-built-value-additional-properties-factory

Conversation

@wing328

@wing328 wing328 commented Jun 29, 2026

Copy link
Copy Markdown
Member

based on #23662 with resolved merge conflicts

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes missing builder factories for nested additionalProperties in Dart dart-dio built_value serializers. Prevents runtime errors like "Bad state: No builder factory for BuiltList" for shapes such as Map<String, List>.

  • Bug Fixes
    • Walk container trees and register builder factories for each nested layer (e.g., Map<String, List>, List<Map<String, X>>).
    • Add composite serializer support in serializers.mustache to emit nested FullType(...) and matching builders.
    • Extend BuiltValueSerializer with a composite mode and proper dedup via equals/hashCode.
    • Add regression test and fixture covering Map<String, List<$ref>>; update generated petstore samples.

Written for commit a7187d1. Summary will update on new commits.

Review in cubic

a-ledu and others added 4 commits May 9, 2026 09:40
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.
@wing328 wing328 changed the title Homegan fix/dart dio built value additional properties factory [dart-dio][built_value] Register BuilderFactory for nested additionalProperties shapes Jun 29, 2026
@wing328 wing328 added this to the 7.24.0 milestone Jun 29, 2026
@wing328 wing328 marked this pull request as ready for review June 29, 2026 09:23

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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>(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 + ")]",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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>

@wing328 wing328 merged commit b7bf0c4 into master Jun 29, 2026
99 of 206 checks passed
@wing328 wing328 deleted the Homegan-fix/dart-dio-built-value-additional-properties-factory branch June 29, 2026 10:50
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