From bfe7a2b4213dd70a35ab9e2ac4aceafe56c06c8f Mon Sep 17 00:00:00 2001 From: Dan Printzell Date: Tue, 12 Aug 2025 13:03:22 +0200 Subject: [PATCH] fix(gql_code_builder): isBuilder detection Signed-off-by: Dan Printzell --- .../lib/src/tristate_optionals.dart | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/codegen/gql_code_builder/lib/src/tristate_optionals.dart b/codegen/gql_code_builder/lib/src/tristate_optionals.dart index b4e0a77f..2684882f 100644 --- a/codegen/gql_code_builder/lib/src/tristate_optionals.dart +++ b/codegen/gql_code_builder/lib/src/tristate_optionals.dart @@ -186,11 +186,8 @@ String _generateFieldDeserializers( final typeDefNode = getTypeDefinitionNode(schemaSource.document, originalSymbolName); - //TODO this feels flaky, find a better way - final isBuilder = type.url != null && - !isWrappedValue && - (typeDefNode is! ScalarTypeDefinitionNode && - typeDefNode is! EnumTypeDefinitionNode); + // Check if this is a built type that needs .replace() method + final isBuilder = !isWrappedValue && _isBuiltType(type, typeDefNode); const fieldNameVariableName = "_\$fieldValue"; @@ -295,3 +292,26 @@ bool _isValue(Reference ref) { return ref.symbol == valueTypeRef.symbol && ref.url == valueTypeRef.url; } + +bool _isBuiltType(TypeReference type, TypeDefinitionNode? typeDefNode) { + // Built collection types (BuiltList, BuiltMap, etc.) + final bool isFromBuiltCollectionPackage = + type.url?.contains("built_collection") ?? false; + if (isFromBuiltCollectionPackage) { + return true; + } + + // Built value types (generated classes that implement Built) + if (typeDefNode != null && + (typeDefNode is InputObjectTypeDefinitionNode || + typeDefNode is ScalarTypeDefinitionNode)) { + return true; + } + + // Types with Builder suffix pattern + if (type.symbol.endsWith("Builder")) { + return true; + } + + return false; +}