Skip to content

Sealed base with explicitSubTypes emits subtype toJson/fromJson calls when generateJson is off (generated code does not compile) #159

Closed

Description

@arrrrny

Symptom

A sealed base with explicitSubTypes generates a polymorphic changeTo dispatch that calls Subtype().toJson() / Subtype.fromJson(...) for every declared subtype — even when neither the base nor the subtypes enable generateJson. The generated part therefore fails to analyze/compile:

sealed.dart: undefined_instance_member_read — The member 'toJson' isn't defined for the type 'Circle'.
sealed.dart: undefined_instance_member_read — The member 'toJson' isn't defined for the type 'Square'.
sealed.dart: undefined_static_member_read — The class 'Circle' doesn't have a static member or constructor named 'fromJson'.

Reproduction

import 'package:zorphy_annotation/zorphy_annotation.dart';

part 'sealed.zorphy.dart';

@Zorphy(explicitSubTypes: [$Circle, $Square])
abstract class $$Shape {
  double get area;
}

@Zorphy()
abstract class $Circle implements $$Shape {
  double get radius;
}

@Zorphy()
abstract class $Square implements $$Shape {
  double get side;
}

Generate with the 2.5.0 pipeline (resolved ClassElement -> ClassAnalyzer -> Orchestrator.generate) and analyze the output: the diagnostics above appear.

Why it was found now

The 2.5.0 analyze-clean release gate (zorphy/test/release/analyze_clean_gate_test.dart) re-analyzes generated output under the pinned analyzer (14.7.0). Previous releases only compared goldens, which never re-compiled the emitted source.

Expected

The subtype toJson/fromJson dispatch must only be emitted when the relevant JSON surface exists (base and/or subtypes with generateJson: true).

Impact

Any sealed hierarchy that does not use JSON generation produces uncompilable code. 2.5.0 keeps byte-identical output (this is a lib/ behavior change), so the gate documents the current behavior as a characterization test and this issue tracks the fix.

Activity

  1. arrrrny commented on Nov 16, 2026

    @arrrrny
    OwnerAuthor

    ✅ Forklift: shipped — #161

  2. arrrrny commented on Nov 16, 2026

    @arrrrny
    OwnerAuthor

    Note for whoever fixes this: PR #160 (2.5.0) adds zorphy/test/release/analyze_clean_gate_test.dart with a characterization test for exactly this shape — it asserts the current broken output (contains('toJson()') plus the undefined_instance_member_read / undefined_static_member_read diagnostics) so the fix cannot land silently. When you fix it, that test fails by design: move the _sealedNoJsonFixture into the strict zero-diagnostic group and delete the characterization test. The reason 2.5.0 only characterizes instead of fixing is that 2.5.0 must keep generated output byte-identical (a lib/ behavior change belongs in its own PR).

  3. added a commit that references this issue on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions