From 3aa85bf96cc45f4a0458ac31fbe1cc4f7f143b9d Mon Sep 17 00:00:00 2001 From: Mathieu Ancelin Date: Fri, 15 Mar 2024 17:42:28 +0100 Subject: [PATCH] Return jsnull if null read --- otoroshi/app/env/Env.scala | 3 +++ otoroshi/app/utils/jsonpath.scala | 8 +++++++- otoroshi/conf/application.conf | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/otoroshi/app/env/Env.scala b/otoroshi/app/env/Env.scala index b9caf5a81..aab468ded 100644 --- a/otoroshi/app/env/Env.scala +++ b/otoroshi/app/env/Env.scala @@ -193,6 +193,9 @@ class Env( private lazy val halloweenStop = DateTime.now().withMonthOfYear(10).withDayOfMonth(31).plusDays(1).withMillisOfDay(1) + lazy val jsonPathNullReadIsJsNull = + configuration.getOptionalWithFileSupport[Boolean]("otoroshi.options.jsonPathNullReadIsJsNull").getOrElse(false) + lazy val dynamicBodySizeCompute = configuration.getOptionalWithFileSupport[Boolean]("otoroshi.options.dynamicBodySizeCompute").getOrElse(true) diff --git a/otoroshi/app/utils/jsonpath.scala b/otoroshi/app/utils/jsonpath.scala index 9603ccde8..74cef0581 100644 --- a/otoroshi/app/utils/jsonpath.scala +++ b/otoroshi/app/utils/jsonpath.scala @@ -86,6 +86,8 @@ object JsonPathUtils { // } } + private lazy val jsonPathNullReadIsJsNull = OtoroshiEnvHolder.get().jsonPathNullReadIsJsNull + def getAtPolyF(payload: String, path: String): Either[JsonPathReadError, JsValue] = { //val env = OtoroshiEnvHolder.get() //env.metrics.withTimer("JsonPathUtils.getAtPolyF") { @@ -95,7 +97,11 @@ object JsonPathUtils { if (read != null) { Right(Writes.jsonNodeWrites.writes(read)) } else { - Left(JsonPathReadError("null read", path, payload, None)) + if (jsonPathNullReadIsJsNull) { + Right(JsNull) + } else { + Left(JsonPathReadError("null read", path, payload, None)) + } } } match { case Failure(e) => Left(JsonPathReadError("error while trying to read", path, payload, e.some)) diff --git a/otoroshi/conf/application.conf b/otoroshi/conf/application.conf index 9cef2ae64..90cabf54a 100644 --- a/otoroshi/conf/application.conf +++ b/otoroshi/conf/application.conf @@ -1033,6 +1033,8 @@ otoroshi { } } options { + jsonPathNullReadIsJsNull = false + jsonPathNullReadIsJsNull = ${?OTOROSHI_OPTIONS_JSONPATHNULLREADISJSNULL} bypassUserRightsCheck = false bypassUserRightsCheck = ${?OTOROSHI_OPTIONS_BYPASSUSERRIGHTSCHECK} emptyContentLengthIsChunked = true