Skip to content

Commit

Permalink
Add a handling for the null case when flattening over the map of sett…
Browse files Browse the repository at this point in the history
…ings, for example when initial_recovery = null after copying or recovering an index (#3308)

* Add a handling for the null case when flattening over the map of settings, for example when initial_recovery = null after copying or recovering an index

* add tests for null edge case of the Maps class flatten function

* formatting new MapsTest file
  • Loading branch information
q-martindurchov authored Feb 14, 2025
1 parent c64d2ea commit 9c7e011
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ object Maps {
case (key, value: java.util.Map[String, V]) => flatten(value.asScala.toMap[String, V]).map { case (k, v) =>
s"$key$separator$k" -> v
}
case (key, null) => Map(key -> null.asInstanceOf[V])
case (key, value: V) => Map(key -> value)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sksamuel.elastic4s

import com.sksamuel.elastic4s.ext.Maps
import org.scalatest
import org.scalatest.matchers.should.Matchers

class MapsTest extends scalatest.flatspec.AnyFlatSpec with Matchers {
"Maps flatten function" should "support null values" in {
val jsonMap: Map[String, Any] = Map(
"settings" -> Map(
"index" -> Map(
"routing" -> Map(
"allocation" -> Map(
"include" -> Map(
"_tier_preference" -> "data_content"
),
"initial_recovery" -> Map(
"_id" -> null
)
)
),
"number_of_shards" -> "1",
"routing_partition_size" -> "1",
"blocks" -> Map(
"write" -> "true"
),
"provided_name" -> "test-two",
"resize" -> Map(
"source" -> Map(
"name" -> "test-index",
"uuid" -> "1234"
)
),
"creation_date" -> "1234",
"number_of_replicas" -> "1",
"uuid" -> "1234",
"version" -> Map(
"created" -> "1234"
)
)
)
)
Maps.flatten(jsonMap) shouldBe Map(
"settings.index.resize.source.name" -> "test-index",
"settings.index.resize.source.uuid" -> "1234",
"settings.index.provided_name" -> "test-two",
"settings.index.blocks.write" -> "true",
"settings.index.routing_partition_size" -> "1",
"settings.index.routing.allocation.include._tier_preference" -> "data_content",
"settings.index.routing.allocation.initial_recovery._id" -> null,
"settings.index.number_of_replicas" -> "1",
"settings.index.version.created" -> "1234",
"settings.index.number_of_shards" -> "1",
"settings.index.creation_date" -> "1234",
"settings.index.uuid" -> "1234"
)
}
}

0 comments on commit 9c7e011

Please sign in to comment.