Skip to content

Commit

Permalink
Escape special characters in building a JSON (#77)
Browse files Browse the repository at this point in the history
* add json escaper

* fix format

* fix format

* change test name

* update test

* Bump to 0.2.1-SNAPSHO
  • Loading branch information
biandratti committed Sep 10, 2021
1 parent a8fe5b7 commit 6d8998b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,40 @@ class ConfigParserSpec
}
}

it should "deal with escape characters according to rfc7159 (section-7)" in {
val conf = ConfigFactory.parseString("""{"quotation_mark":"",
|"reverse_solidus":"",
|"solidus":"",
|"backspace":"",
|"form_feed":"",
|"line_feed":"",
|"carriage_return":"",
|"tab":"",
|"java_home":"C:\\Program Files\\java\\jdk-8\\bin"}""".stripMargin)

val orig = ConfigParser.toJson(conf.root())
val json = ConfigParser.withCamelCase(orig)

val env = Map(
"QUOTATION_MARK" -> "\"",
"REVERSE_SOLIDUS" -> "\\",
"SOLIDUS" -> "/",
"BACKSPACE" -> "\b",
"FORM_FEED" -> "\f",
"LINE_FEED" -> "\n",
"CARRIAGE_RETURN" -> "\r",
"TAB" -> "\t",
"JAVA_HOME" -> "C:\\Program Files\\java\\jdk-11\\bin"
)

val result = ConfigParser.withEnvVarOverrides(json, "", env)

inside(result) { case Right(json) =>
json.noSpaces shouldBe
"""{"backspace":"\b","carriageReturn":"\r","formFeed":"\f","javaHome":"C:\\Program Files\\java\\jdk-11\\bin","lineFeed":"\n","quotationMark":"\"","reverseSolidus":"\\","solidus":"/","tab":"\t"}""".stripMargin
}
}

it should "validate that data types are not altered" in {
val conf = ConfigFactory.load("override.conf")
val orig = ConfigParser.toJson(conf.root())
Expand Down
31 changes: 19 additions & 12 deletions metronome/config/src/io/iohk/metronome/config/ConfigParser.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.iohk.metronome.config

import cats.implicits._
import com.typesafe.config.{ConfigObject, ConfigRenderOptions}
import io.circe.{Json, JsonObject, ParsingFailure, Decoder, DecodingFailure}
import com.typesafe.config.{ConfigObject, ConfigRenderOptions, ConfigUtil}
import io.circe.{Decoder, DecodingFailure, Json, JsonObject, ParsingFailure}
import io.circe.parser.{parse => parseJson}
import scala.util.{Try, Success, Failure}

import scala.util.{Failure, Success, Try}

object ConfigParser {
protected[config] type ParsingResult = Either[ParsingFailure, Json]
Expand Down Expand Up @@ -142,16 +143,22 @@ object ConfigParser {
env
.get(path)
.map { value =>
val maybeJson = parseJson(value) orElse parseJson(s""""$value"""")

maybeJson.flatMap { json =>
if (validate(json)) {
Right(json)
} else {
val msg = s"Invalid value for $path: $value"
Left(ParsingFailure(value, new IllegalArgumentException(msg)))
val maybeJson =
parseJson(value) orElse parseJson(ConfigUtil.quoteString(value))

maybeJson.left
.map { err =>
val msg = s"Could not parse value for $path: $value"
ParsingFailure(msg, err)
}
.flatMap { json =>
if (validate(json)) {
Right(json)
} else {
val msg = s"Invalid value for $path: $value"
Left(ParsingFailure(value, new IllegalArgumentException(msg)))
}
}
}
}
.getOrElse(Right(default))

Expand Down
2 changes: 1 addition & 1 deletion versionFile/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0-SNAPSHOT
0.2.1-SNAPSHOT

0 comments on commit 6d8998b

Please sign in to comment.