Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: autofield iterable serialization #2992

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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") {
Expand All @@ -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") {
Expand Down Expand Up @@ -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}"""
}
Expand Down
Loading