From 91651816c2cc4fae305b81420095a84689df75e9 Mon Sep 17 00:00:00 2001 From: Dan Kotovski Date: Tue, 17 Dec 2024 19:50:20 +0200 Subject: [PATCH 1/3] Removed unnecessary error generation in the JsUndefined class when the asOpt function was used --- .../src/main/scala/play/api/libs/json/JsLookup.scala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/play-json/shared/src/main/scala/play/api/libs/json/JsLookup.scala b/play-json/shared/src/main/scala/play/api/libs/json/JsLookup.scala index 152b59afa..a5f7420ba 100644 --- a/play-json/shared/src/main/scala/play/api/libs/json/JsLookup.scala +++ b/play-json/shared/src/main/scala/play/api/libs/json/JsLookup.scala @@ -195,9 +195,10 @@ case class JsDefined(value: JsValue) extends AnyVal with JsLookupResult * Represent a missing Json value. */ final class JsUndefined(err: => String) extends JsLookupResult { - def error = err - def validationError = JsonValidationError(error) - override def toString = s"JsUndefined($err)" + def error = err + def validationError = JsonValidationError(error) + override def toString = s"JsUndefined($err)" + override def asOpt[T](implicit fjs: Reads[T]): Option[T] = None } object JsUndefined { From ec302fdb7ec2c3e41cf0a6a272258be652a64be8 Mon Sep 17 00:00:00 2001 From: Dan Kotovski Date: Thu, 16 Jan 2025 14:57:44 +0200 Subject: [PATCH 2/3] Added test for JsUndefined.asOpt function --- .../play/api/libs/json/JsLookupSpec.scala | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala diff --git a/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala b/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala new file mode 100644 index 000000000..f11f330e6 --- /dev/null +++ b/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala @@ -0,0 +1,27 @@ +/* + * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. + */ + +package play.api.libs.json + +import play.api.libs.json.Json._ + +import org.scalatest.matchers.must.Matchers +import org.scalatest.wordspec.AnyWordSpec + +class JsLookupSpec extends AnyWordSpec with Matchers { + "JsLookupResult" should { + val obj = Json.obj( + "field" -> 123 + ) + val result = obj \ "missingField" + + "return JsUndefined when a key is missing" in { + result mustBe a[JsUndefined] + } + + "return None when calling asOpt on JsUndefined" in { + result.asOpt[Int] mustEqual None + } + } +} From 5e7eec6a5bdd25dd9ab064e0c7e243a6fbbd2183 Mon Sep 17 00:00:00 2001 From: Dan Kotovski Date: Tue, 21 Jan 2025 13:46:50 +0200 Subject: [PATCH 3/3] Formatting --- .../shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala b/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala index f11f330e6..e0a368552 100644 --- a/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala +++ b/play-json/shared/src/test/scala/play/api/libs/json/JsLookupSpec.scala @@ -21,7 +21,7 @@ class JsLookupSpec extends AnyWordSpec with Matchers { } "return None when calling asOpt on JsUndefined" in { - result.asOpt[Int] mustEqual None + result.asOpt[Int].mustEqual(None) } } }