From 958cc7800feb9103d950425ea5ee1186f2cb6626 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 23 Feb 2023 20:20:01 +0100 Subject: [PATCH] Can serialize record with List --- .../compiler/test/context/JacksonTest.java | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/engine/runtime/src/test/java/org/enso/compiler/test/context/JacksonTest.java b/engine/runtime/src/test/java/org/enso/compiler/test/context/JacksonTest.java index 82bf44dc9c4f..19b24e5db07e 100644 --- a/engine/runtime/src/test/java/org/enso/compiler/test/context/JacksonTest.java +++ b/engine/runtime/src/test/java/org/enso/compiler/test/context/JacksonTest.java @@ -1,9 +1,12 @@ package org.enso.compiler.test.context; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.scala.DefaultScalaModule; +import java.util.List; import org.enso.polyglot.Suggestion; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.junit.Test; import scala.Option; @@ -14,11 +17,11 @@ public class JacksonTest { @Test public void testSerdeOfSuggestion() throws Exception { Object shape = new Suggestion.Module( - "SampleModule", - Option.apply("doc"), - Option.apply("html"), - Option.empty(), - Option.empty() + "SampleModule", + Option.apply("doc"), + Option.apply("html"), + Option.empty(), + Option.empty() ); final ObjectMapper m = new ObjectMapper().registerModule(new DefaultScalaModule()); String result = m @@ -34,10 +37,10 @@ public void testSerdeOfSuggestion() throws Exception { @Test public void testArraySerdeOfSuggestion() throws Exception { Object shape = new Suggestion[]{new Suggestion.Module( - "SampleModule", - Option.apply("doc"), - Option.apply("html"), - Option.empty(), + "SampleModule", + Option.apply("doc"), + Option.apply("html"), + Option.empty(), Option.empty() )}; final ObjectMapper m = new ObjectMapper().registerModule(new DefaultScalaModule()); @@ -51,6 +54,38 @@ public void testArraySerdeOfSuggestion() throws Exception { if (suggestion instanceof Suggestion.Module module) { assertEquals("SampleModule", module.name()); assertEquals("doc", module.documentation().get()); + } else { + fail("Expecting Suggestion.Module: " + suggestion); + } + } + + @Test + public void testRecordSerdeOfSuggestion() throws Exception { + Object shape = new SuggestionCache(11, List.of(new Suggestion.Module( + "SampleModule", + Option.apply("doc"), + Option.apply("html"), + Option.empty(), + Option.empty() + ))); + final ObjectMapper m = new ObjectMapper().registerModule(new DefaultScalaModule()); + String result = m + .writerWithDefaultPrettyPrinter() + .writeValueAsString(shape); + + var cache = (SuggestionCache) m.readerFor(SuggestionCache.class).readValue(result); + assertEquals("One suggestion", 1, cache.suggestions.size()); + if (cache.suggestions().get(0) instanceof Suggestion.Module module) { + assertEquals("SampleModule", module.name()); + assertEquals("doc", module.documentation().get()); + } else { + fail("Expecting Suggestion.Module: " + cache); } } + + public record SuggestionCache( + @JsonProperty("version") int version, + @JsonProperty("suggestions") List suggestions + ) { + } }