From a8c355ef16ed9cf0b926db8b79d2efb199a1cf08 Mon Sep 17 00:00:00 2001 From: Daniel Slapman Date: Thu, 23 Mar 2023 15:20:41 +0100 Subject: [PATCH] Improve behavior of JsonOptic.set (fixes #13) --- .../scala/ru/tinkoff/tcb/utils/circe/optics/package.scala | 4 +++- .../ru/tinkoff/tcb/utils/circe/optics/OpticSpec.scala | 7 +++++++ backend/project/Settings.scala | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/circe-utils/src/main/scala/ru/tinkoff/tcb/utils/circe/optics/package.scala b/backend/circe-utils/src/main/scala/ru/tinkoff/tcb/utils/circe/optics/package.scala index e3eaddc3..b0901d01 100644 --- a/backend/circe-utils/src/main/scala/ru/tinkoff/tcb/utils/circe/optics/package.scala +++ b/backend/circe-utils/src/main/scala/ru/tinkoff/tcb/utils/circe/optics/package.scala @@ -32,7 +32,9 @@ package object optics { } } .getOrElse(construct(part, mod(None))) - case Traverse => json => json.withArray(arr => Json.fromValues(arr.map(j => mod(Some(j))))) + case Traverse => + json => + json.asArray.map(arr => Json.fromValues(arr.map(j => mod(Some(j))))).getOrElse(construct(part, mod(None))) } } diff --git a/backend/circe-utils/src/test/scala/ru/tinkoff/tcb/utils/circe/optics/OpticSpec.scala b/backend/circe-utils/src/test/scala/ru/tinkoff/tcb/utils/circe/optics/OpticSpec.scala index e42e3bdf..c813de3a 100644 --- a/backend/circe-utils/src/test/scala/ru/tinkoff/tcb/utils/circe/optics/OpticSpec.scala +++ b/backend/circe-utils/src/test/scala/ru/tinkoff/tcb/utils/circe/optics/OpticSpec.scala @@ -245,6 +245,13 @@ class OpticSpec extends RefSpec with Matchers { result shouldBe json"""{"a": [{"b": 4}, {"b": 4}, {"b": 4}]}""" } + + def `should properly re-create complete structure`: Assertion = { + val target = Json.obj() + val result = optic.set(Json.fromInt(4))(target) + + result shouldBe json"""{"a": [{"b": 4}]}""" + } } object `json getter` { diff --git a/backend/project/Settings.scala b/backend/project/Settings.scala index 9036b502..5a66986b 100644 --- a/backend/project/Settings.scala +++ b/backend/project/Settings.scala @@ -26,7 +26,7 @@ object Settings { val common = Seq( organization := "ru.tinkoff", - version := "3.7.0", + version := "3.7.1", scalaVersion := "2.13.10", Compile / packageDoc / publishArtifact := false, Compile / packageSrc / publishArtifact := false,