Skip to content

fix(dart): keep top-level fromJson/toJson names with --from-map#3000

Merged
schani merged 1 commit into
masterfrom
agent/fix-issue-1719
Jul 20, 2026
Merged

fix(dart): keep top-level fromJson/toJson names with --from-map#3000
schani merged 1 commit into
masterfrom
agent/fix-issue-1719

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

Bug

With --from-map (methodNamesWithMap), quicktype's Dart output renamed the
top-level free functions sensordataFromJson/sensordataToJson to
sensordataFromMap/sensordataToMap, even though those top-level functions
take/return a JSON String (via json.decode/json.encode), not a Map.
Only the class-level factory constructor and instance method — which really
do operate on Map<String, dynamic> — should change name when --from-map
is used.

Repro:

node dist/index.js -l dart --from-map --top-level Sensordata sensordata.json

Before the fix:

Sensordata sensordataFromMap(String str) => Sensordata.fromMap(json.decode(str));
String sensordataToMap(Sensordata data) => json.encode(data.toMap());

After the fix:

Sensordata sensordataFromJson(String str) => Sensordata.fromMap(json.decode(str));
String sensordataToJson(Sensordata data) => json.encode(data.toMap());

Root cause

DartRenderer.makeTopLevelDependencyNames built the top-level encoder/decoder
names using the toJson/fromJson getters, which are conditional on
methodNamesWithMap and return toMap/fromMap when the option is set.
Those getters are also (correctly) used for the class-level
fromMap/toMap methods emitted by _emitMapEncoderDecoder, so the
top-level functions inherited the same renaming even though they don't
operate on a Map.

Fix

makeTopLevelDependencyNames now hard-codes the _toJson/_fromJson
suffixes for the top-level function names, independent of
methodNamesWithMap. The class-level Map methods are untouched and keep
using the conditional naming, matching the fix suggested by a maintainer in
the issue thread.

Test coverage

  • test/unit/dart-method-names.test.ts (new): renders Dart both with and
    without the from-map renderer option and asserts the top-level functions
    are always named ..FromJson/..ToJson while the class-level
    factory/method are named fromMap/toMap only when the option is set
    (and fromJson/toJson otherwise, i.e. no regression to the default
    case).
  • test/languages.ts: added a from-map renderer-option variant to the Dart
    fixture's quickTestRendererOptions (using simple-object.json) so the
    end-to-end fixture also exercises code generation with this option.

Verification

  • Confirmed the bug on unfixed master with the CLI repro above.
  • Added the failing unit test first, confirmed it failed against the
    unfixed code, then applied the fix and confirmed it passes.
  • npm run build passes.
  • npm run test:unit — 165/165 tests pass.
  • Re-ran the CLI repro manually; output now matches the expected
    fromJson/toJson top-level names while class-level fromMap/toMap
    names are preserved, and confirmed no regression when --from-map is
    omitted.
  • The Dart toolchain (dart binary) is not available in this sandboxed
    environment, so the FIXTURE=dart script/test end-to-end run itself
    wasn't executed locally; the new from-map fixture variant will be
    validated by CI.

Fixes #1719

🤖 Generated with Claude Code

Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
@schani
schani merged commit 978a423 into master Jul 20, 2026
28 checks passed
@schani
schani deleted the agent/fix-issue-1719 branch July 20, 2026 22:16
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.

Dart method names fromMap() and toMap() should not apply to functions that handle Strings

1 participant