Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update JSON Parsing to Optionally Allow Numeric Booleans #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/main/scala/scalapb/json4s/JsonFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.google.protobuf.timestamp.Timestamp
import scalapb.json4s.JsonFormat.GenericCompanion
import scalapb._
import org.json4s.JsonAST._
import org.json4s.{Reader, Writer, MappingException}
import org.json4s.{MappingException, Reader, Writer}

import scala.collection.mutable
import scala.collection.concurrent.TrieMap
Expand Down Expand Up @@ -464,6 +464,7 @@ object Parser {
private final case class ParserConfig(
isIgnoringUnknownFields: Boolean,
isIgnoringOverlappingOneofFields: Boolean,
areNumericBooleanValuesAllowed: Boolean,
mapEntriesAsKeyValuePairs: Boolean,
formatRegistry: FormatRegistry,
typeRegistry: TypeRegistry
Expand All @@ -476,6 +477,7 @@ class Parser private (config: Parser.ParserConfig) {
Parser.ParserConfig(
isIgnoringUnknownFields = false,
isIgnoringOverlappingOneofFields = false,
areNumericBooleanValuesAllowed = false,
mapEntriesAsKeyValuePairs = false,
JsonFormat.DefaultRegistry,
TypeRegistry.empty
Expand All @@ -495,6 +497,7 @@ class Parser private (config: Parser.ParserConfig) {
Parser.ParserConfig(
isIgnoringUnknownFields = false,
isIgnoringOverlappingOneofFields = false,
areNumericBooleanValuesAllowed = false,
mapEntriesAsKeyValuePairs = false,
formatRegistry,
typeRegistry
Expand All @@ -507,6 +510,9 @@ class Parser private (config: Parser.ParserConfig) {
def ignoringOverlappingOneofFields: Parser =
new Parser(config.copy(isIgnoringOverlappingOneofFields = true))

def allowNumericBooleanValues: Parser =
new Parser(config.copy(areNumericBooleanValuesAllowed = true))

def mapEntriesAsKeyValuePairs: Parser =
new Parser(config.copy(mapEntriesAsKeyValuePairs = true))

Expand Down Expand Up @@ -714,7 +720,8 @@ class Parser private (config: Parser.ParserConfig) {
value,
throw new JsonFormatException(
s"Unexpected value ($value) for field ${fd.name} of ${fd.containingMessage.name}"
)
),
allowNumericBooleans = config.areNumericBooleanValuesAllowed
)
}
}
Expand Down Expand Up @@ -902,7 +909,8 @@ object JsonFormat {
def parsePrimitive(
protoType: FieldDescriptorProto.Type,
value: JValue,
onError: => PValue
onError: => PValue,
allowNumericBooleans: Boolean = false
): PValue =
(protoType, value) match {
case (Type.TYPE_UINT32 | Type.TYPE_FIXED32, JInt(x)) =>
Expand Down Expand Up @@ -966,6 +974,13 @@ object JsonFormat {
case (Type.TYPE_BOOL, JBool(b)) => PBoolean(b)
case (Type.TYPE_BOOL, JString("true")) => PBoolean(true)
case (Type.TYPE_BOOL, JString("false")) => PBoolean(false)
case (Type.TYPE_BOOL, JInt(i)) => {
(allowNumericBooleans, i.toInt) match {
case (true, 0) => PBoolean(false)
case (true, 1) => PBoolean(true)
case (_, _) => onError
}
}
case (Type.TYPE_STRING, JString(s)) => PString(s)
case (Type.TYPE_BYTES, JString(s)) =>
PByteString(
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/scalapb/json4s/JsonFormatSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,29 @@ class JsonFormatSpec
assertRejects("optionalDouble", (minDouble.multiply(moreThanOne).toString))
}

"parser" should "parse numeric boolean values when enabled" in {
val parser = new Parser().allowNumericBooleanValues
def validateRejects(json: String): Assertion = {
a[JsonFormatException] mustBe thrownBy {
parser.fromJsonString[MyTest](json)
}
}

parser.fromJsonString[MyTest]("""{"optBool":1}""") must be(
MyTest(optBool = Some(true))
)
parser.fromJsonString[MyTest]("""{"optBool":0}""") must be(
MyTest(optBool = Some(false))
)

// Only 0 and 1 as integers should be parsed
validateRejects("""{"optBool":2}""")
validateRejects("""{"optBool":-1}""")
validateRejects("""{"optBool":"0"}""")
validateRejects("""{"optBool":"1"}""")
}


val anyEnabledJavaTypeRegistry = JavaTypeRegistry
.newBuilder()
.add(TestProto.companion.javaDescriptor)
Expand Down