From d855c1443267ddeec1aceff53f18ce83357a7988 Mon Sep 17 00:00:00 2001 From: cdmbr Date: Mon, 22 Jan 2024 11:04:32 -0300 Subject: [PATCH] fix: autofield iterable serialization --- .../sksamuel/elastic4s/json/XContentBuilder.scala | 1 + .../elastic4s/json/XContentBuilderTest.scala | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/elastic4s-json-builder/src/main/scala/com/sksamuel/elastic4s/json/XContentBuilder.scala b/elastic4s-json-builder/src/main/scala/com/sksamuel/elastic4s/json/XContentBuilder.scala index 771111786..df49b34de 100755 --- a/elastic4s-json-builder/src/main/scala/com/sksamuel/elastic4s/json/XContentBuilder.scala +++ b/elastic4s-json-builder/src/main/scala/com/sksamuel/elastic4s/json/XContentBuilder.scala @@ -251,6 +251,7 @@ class XContentBuilder(root: JsonValue) { map.foreach { case (k, v) => autofield(k.toString, v) } endObject() case map: java.util.Map[_, _] => autofield(name, map.asScala.toMap) + case values: Iterable[_] => autoarray(name, values.toSeq) case null => obj.putNull(name) case other => field(name, other.toString) } diff --git a/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/json/XContentBuilderTest.scala b/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/json/XContentBuilderTest.scala index b4c51274e..1f3af8659 100644 --- a/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/json/XContentBuilderTest.scala +++ b/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/json/XContentBuilderTest.scala @@ -1,10 +1,11 @@ package com.sksamuel.elastic4s.json -import java.math.BigInteger - import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers +import java.math.BigInteger +import scala.collection.immutable.ListSet + class XContentBuilderTest extends AnyFunSuite with Matchers { test("simple object") { @@ -21,7 +22,7 @@ class XContentBuilderTest extends AnyFunSuite with Matchers { XContentFactory.obj() .startObject("wibble").field("foo", 1).field("boo", true).endObject() .startObject("dibble").field("goo", 2.4).string shouldBe - """{"wibble":{"foo":1,"boo":true},"dibble":{"goo":2.4}}""" + """{"wibble":{"foo":1,"boo":true},"dibble":{"goo":2.4}}""" } test("should support raw fields in objects") { @@ -68,6 +69,12 @@ class XContentBuilderTest extends AnyFunSuite with Matchers { XContentFactory.obj().autofield("biginteger", new BigInteger("98123981231982361893619")).string shouldBe """{"biginteger":98123981231982361893619}""" } + test("should support iterable fields") { + XContentFactory.obj().autofield("iterable", Set(1, 2, 3)).string shouldBe """{"iterable":[1,2,3]}""" + XContentFactory.obj().autofield("iterable", ListSet(1, 2, 3)).string shouldBe """{"iterable":[1,2,3]}""" + XContentFactory.obj().autofield("iterable", Iterable(1, 2, 3)).string shouldBe """{"iterable":[1,2,3]}""" + } + test("should support int fields") { XContentFactory.obj().field("int", 3242365).string shouldBe """{"int":3242365}""" }