-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the ability to accept payloads with underscore via config (#5304)
Co-authored-by: Simon Dumas <[email protected]>
- Loading branch information
Showing
10 changed files
with
40 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 9 additions & 52 deletions
61
delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/resources/NexusSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,18 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.resources | ||
|
||
import io.circe.Decoder.Result | ||
import io.circe.{Decoder, DecodingFailure, HCursor, Json} | ||
import pureconfig.ConfigReader | ||
import pureconfig.error.CannotConvert | ||
import io.circe.syntax.EncoderOps | ||
import io.circe.{Decoder, Json} | ||
|
||
final case class NexusSource(value: Json) extends AnyVal | ||
|
||
object NexusSource { | ||
|
||
sealed trait DecodingOption | ||
|
||
object DecodingOption { | ||
final case object Strict extends DecodingOption | ||
|
||
final case object Lenient extends DecodingOption | ||
|
||
implicit val decodingOptionConfigReader: ConfigReader[DecodingOption] = | ||
ConfigReader.fromString { | ||
case "strict" => Right(Strict) | ||
case "lenient" => Right(Lenient) | ||
case other => | ||
Left( | ||
CannotConvert( | ||
other, | ||
"DecodingOption", | ||
s"values can only be 'strict' or 'lenient'" | ||
) | ||
) | ||
|
||
} | ||
} | ||
|
||
private val strictDecoder = new Decoder[NexusSource] { | ||
private val decoder = implicitly[Decoder[Json]] | ||
|
||
override def apply(c: HCursor): Result[NexusSource] = { | ||
decoder(c).flatMap { json => | ||
val underscoreFields = json.asObject.toList.flatMap(_.keys).filter(_.startsWith("_")) | ||
Either.cond( | ||
underscoreFields.isEmpty, | ||
NexusSource(json), | ||
DecodingFailure( | ||
s"Field(s) starting with _ found in payload: ${underscoreFields.mkString(", ")}", | ||
c.history | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private val lenientDecoder = implicitly[Decoder[Json]].map(NexusSource(_)) | ||
|
||
implicit def nexusSourceDecoder(implicit decodingOption: DecodingOption): Decoder[NexusSource] = { | ||
decodingOption match { | ||
case DecodingOption.Lenient => lenientDecoder | ||
case DecodingOption.Strict => strictDecoder | ||
} | ||
implicit val nexusSourceDecoder: Decoder[NexusSource] = Decoder.decodeJsonObject.emap { obj => | ||
val underscoreFields = obj.keys.filter(_.startsWith("_")) | ||
Either.cond( | ||
underscoreFields.isEmpty, | ||
NexusSource(obj.asJson), | ||
s"Field(s) starting with _ found in payload: ${underscoreFields.mkString(", ")}" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.