Skip to content

Conversation

devunt
Copy link

@devunt devunt commented Sep 1, 2025

Description

This PR fixes an issue where JsonObject fields incorrectly generate calls to .replace() method when tristate_optionals is enabled.

Problem

When using Ferry with JsonObject type (from built_value package) and tristate_optionals: true option enabled, the generated serializer code contains calls to .replace() method on JsonObject? fields. However, JsonObject doesn't have a replace method, causing compilation errors.

Solution

Added special handling for JsonObject type in the tristate optionals code generator to ensure it's treated as a simple value type (using direct assignment) rather than a built collection type (which would use .replace() method).

Changes

  • Modified /codegen/gql_code_builder/lib/src/tristate_optionals.dart to detect JsonObject type and exclude it from being treated as a builder type

Testing

Tested with a real-world Flutter project using:

  • tristate_optionals: true
  • JsonObject type override for JSON scalar
  • Verified that generated code now compiles successfully

Related Issues

Before/After

Before (causes compilation error):

case 'params':
  var _$fieldValue = serializers.deserialize(value,
      specifiedType: const FullType(_i1.JsonObject)) as _i1.JsonObject;
  builder.params.replace(_$fieldValue);  // ❌ Error: replace() not defined
  break;

After (works correctly):

case 'params':
  var _$fieldValue = serializers.deserialize(value,
      specifiedType: const FullType(_i1.JsonObject)) as _i1.JsonObject;
  builder.params = _$fieldValue;  // ✅ Direct assignment
  break;

…optionals

When tristate_optionals is enabled, the code generator incorrectly treats JsonObject from built_value package as a built collection that has a replace() method. However, JsonObject is a simple value type without replace() method.

This fix adds special handling for JsonObject to ensure it's treated as a simple value type, using direct assignment instead of replace() method.

Fixes gql-dart/ferry#649
@devunt devunt force-pushed the fix-jsonobject-tristate-replace branch from 363c9ac to 484317f Compare September 1, 2025 04:43
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.

Error: The method 'replace' isn't defined for type 'JsonObject?' when tristate_optionals is enabled
1 participant