google / google/built_value.dart

StandardJsonPlugin: Serialisation fails for `BuiltSet`s with `specifiedType = FullType.unspecified`

Open
#1,102 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

I'm trying to serialise a nested data structure, which is of type `BuiltMap`. Some of the values are of type `BuiltSet`. When I'm trying to serialise them with the `StandardJsonPlugin`, I'm getting the following error:
```
_CastError (type '_InternalLinkedHashMap' is not a subtype of type 'String' in type cast)

StandardJsonPlugin._toMapWithDiscriminator (~/.flutter/.pub-cache/hosted/pub.dartlang.org/built_value-8.1.3/lib/standard_json_plugin.dart:132)
StandardJsonPlugin.afterSerialize (~/.flutter/.pub-cache/hosted/pub.dartlang.org/built_value-8.1.3/lib/standard_json_plugin.dart:49)
BuiltJsonSerializers.serialize (~/.flutter/.pub-cache/hosted/pub.dartlang.org/built_value-8.1.3/lib/src/built_json_serializers.dart:71)
```

# Reproduction

I was able to reproduce the error with the following snippet:

```dart
@SerializersFor([])
final Serializers serializers = (_$serializers.toBuilder()
..addPlugin(StandardJsonPlugin(valueKey: "#")))
.build();

serializers.serialize(BuiltSet(["a", "b", "c"])); // this statement throws
```

I think, the reason for this is missing support for BuiltSet in `StandardJsonPlugin._toMapWithDiscriminator`. This function has cases for `type == "list"` and `type == "map"`, but not for `type == "set"`. I think it could be implemented analogously to the "list"-case.

https://github.com/google/built_value.dart/blob/e80b134d51823dae28c4a6608bd1ccafb3d52c49/built_value/lib/standard_json_plugin.dart#L98

I'd be happy to open a PR or help fixing this bug once confirmed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.