Skip to content

fix: default missing required collections when filling response defaults - #2016

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/defaults-missing-collections
Oct 13, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/defaults-missing-collections

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

A required collection the spec gives no default stayed missing when a response omitted it.

// GET /presets/empty -> {"total": 0}
(await general.listEmptyPresets()).presets.length
// before: TypeError: Cannot read properties of undefined (reading 'length')
// after:  0
// before: InvalidArgumentException: Missing required field "presets" for PresetList
// after:  []

Models only filled required fields that carry a spec default, and the spec never gives one to a collection. Language::getModelDefault() now fills a required collection with [], the one value its type always admits. Every SDK that fills defaults reads it through the new modelDefault filter. Optional fields stay absent. Nested models stay absent too, because an empty object may not satisfy the fields they require. Go is unchanged: a nil slice already reads as empty, and adding the defaults UnmarshalJSON to every list model would decode each response twice.

E2E: a new listEmptyPresets mock returns {"total": 0} and every SDK prints empty presets: 0.

Now that list models carry defaults, three existing generator gaps showed up, and this PR fixes them too:

  • JS services imported applyDefaults for upload methods that never call it. The import is now skipped for those methods.
  • Swift wrote a trailing comma in a one-entry defaults literal, and the decode fallback for a collection went over the line limit. It now emits [].
  • Node service tests keyed their mock data as '\\$id', so the response never actually carried $id.

Closes #2017

The spec gives a model collection no default, and models only filled
required fields that carry one, so a response that omitted a required
collection left it undefined (JS) or failed to decode (every other SDK).
An empty list is the one value a collection's type always admits, so
required collections now fill with it; nested models stay absent.
@hansi-codes

hansi-codes Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The reviewed changes preserve the intended decoding behavior, and the Swift fallback renders a valid context-typed empty array.

Adds shared model-default handling so generated SDKs fill omitted required collection fields with empty arrays, while leaving optional fields and nested models absent. It adds an empty-presets mock plus generation and e2e coverage, and updates language templates and fixtures to support the behavior consistently.

Latest changes: The Swift model template now reuses the escaped field name and emits [] directly for empty collection defaults, avoiding oversized generated fallback expressions.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 9
File Change
src/SDK/Language.php; src/SDK/SDK.php; src/SDK/Language/Dart.php; src/SDK/Language/DotNet.php; src/SDK/Language/JS.php; src/SDK/Language/Kotlin.php; src/SDK/Language/Ruby.php; src/SDK/Language/Rust.php; src/SDK/Language/Swift.php Adds shared required-collection defaults and exposes them to language-specific generation.
templates/dart/lib/src/models/model.dart.twig; templates/dotnet/Package/Models/Model.cs.twig; templates/php/src/Models/Model.php.twig; templates/python/package/models/model.py.twig; templates/ruby/lib/container/models/model.rb.twig; templates/rust/src/models/model.rs.twig; templates/swift/Sources/Models/Model.swift.twig Updates generated model decoding to apply the model defaults; Swift emits empty collection fallbacks directly.
templates/deno/src/services/service.ts.twig; templates/node/src/services/template.ts.twig; templates/react-native/src/services/template.ts.twig; templates/web/src/services/template.ts.twig Avoids importing response-default helpers in upload-only service paths.
templates/node/test/services/service.test.js.twig; templates/rust/tests/tests.rs.twig Corrects Node response fixture keys and updates generated Rust test coverage for model defaults.
mock-server/app/http.php; tests/resources/spec-openapi3.json; tests/generation/GenerationTest.php; tests/e2e/Base.php Adds the empty-presets mock and fixture, generated-shape assertion, and shared e2e expected output.
tests/e2e/languages/android/Tests.kt; tests/e2e/languages/apple/Tests.swift; tests/e2e/languages/dart/tests.dart; tests/e2e/languages/deno/tests.ts; tests/e2e/languages/dotnet/Tests.cs; tests/e2e/languages/flutter/tests.dart; tests/e2e/languages/go-v2/tests.go; tests/e2e/languages/go/tests.go; tests/e2e/languages/kotlin/Tests.kt; tests/e2e/languages/node/test.js; tests/e2e/languages/php/test.php; tests/e2e/languages/python/tests.py; tests/e2e/languages/react-native/browser.js; tests/e2e/languages/ruby/tests.rb; tests/e2e/languages/rust/tests/tests.rs; tests/e2e/languages/swift/Tests.swift; tests/e2e/languages/unity/Tests.cs; tests/e2e/languages/web/index.html; tests/e2e/languages/web/node.js Exercises the empty-presets endpoint across SDKs and checks that each reports zero presets.
templates/node/test/services/service.test.js.twig Builds response fixture keys with the JavaScript key renderer.
tests/generation/GenerationTest.php Asserts generation of JavaScript response defaults for a required collection.
tests/e2e/Base.php Adds the expected output for the empty-presets e2e case.

Merged main (49bb526); no changes to this PR's code since 874fd13 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@hansi-codes hansi-codes Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

Required collections give most list models a defaults entry, which
surfaced three generator gaps: the JS services imported applyDefaults
for upload methods that never call it, Swift wrote a trailing comma in
a one-entry literal and a long decode fallback, and the Node service
tests keyed their mock data with an escaped dollar sign.
…collections

# Conflicts:
#	tests/e2e/languages/node/test.js
#	tests/e2e/languages/react-native/browser.js
#	tests/e2e/languages/web/index.html
#	tests/e2e/languages/web/node.js
@ChiragAgg5k
ChiragAgg5k merged commit 15d07f2 into main Oct 13, 2026
21 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/defaults-missing-collections branch October 13, 2026 11:46
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.

Default missing required collections when filling response defaults

1 participant