From ddf7345b0d76c04d38d68cdb1547e9a84c33fca4 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 3 Sep 2020 14:03:49 +0200 Subject: [PATCH] MdYamlCodec: Load maps properly Our MdYamlCodec runs inside an isolate, and send the result back. The YamlMap data structure doesn't seem to be serializable over an isolate and gives us an error. Therefore, we should make sure it is a normal map. --- lib/core/md_yaml_doc_codec.dart | 17 ++++++++++++++--- test/md_yaml_codec_test.dart | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/core/md_yaml_doc_codec.dart b/lib/core/md_yaml_doc_codec.dart index 22bb31cc..05a3549e 100644 --- a/lib/core/md_yaml_doc_codec.dart +++ b/lib/core/md_yaml_doc_codec.dart @@ -91,9 +91,7 @@ class MarkdownYAMLCodec { if (yamlMap is! Map) { return map; } - yamlMap.forEach((key, value) { - map[key] = value; - }); + map = _convertMap(yamlMap); } catch (err) { Log.d('MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}'); } @@ -125,3 +123,16 @@ class MarkdownYAMLCodec { return "---\n" + yaml + "---\n"; } } + +LinkedHashMap _convertMap(YamlMap yamlMap) { + LinkedHashMap map = LinkedHashMap(); + + yamlMap.forEach((key, value) { + if (value is YamlMap) { + value = _convertMap(value); + } + map[key] = value; + }); + + return map; +} diff --git a/test/md_yaml_codec_test.dart b/test/md_yaml_codec_test.dart index d01d321b..1b750094 100644 --- a/test/md_yaml_codec_test.dart +++ b/test/md_yaml_codec_test.dart @@ -1,6 +1,7 @@ import 'dart:collection'; import 'package:test/test.dart'; +import 'package:yaml/yaml.dart'; import 'package:gitjournal/core/md_yaml_doc.dart'; import 'package:gitjournal/core/md_yaml_doc_codec.dart'; @@ -208,5 +209,24 @@ foo: bar // FIXME: Add another test for yaml header at the bottom without a newline // FIXME: Add another test for yaml header at the bottom with lots of new lines after // FIXME: Add another test for yaml header at the bottom with lots of new lines with spaces after + + test('Should not have any YamlMaps', () { + // YamlMaps cannot be sent over an isolate + + var str = """--- +thumbnail: { + name: "adrian_sommeling.jpg", + alt: "Padre e hijo se cubren con un paraguas de una tormenta que hace volar al niño", + style: top +} +tags: ["opinión", "autores"] +--- +"""; + + var serializer = MarkdownYAMLCodec(); + var doc = serializer.decode(str); + + expect(doc.props['thumbnail'].runtimeType, isNot(YamlMap)); + }); }); }