aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorJosh Rosen <joshrosen@databricks.com>2015-02-16 18:08:02 -0800
committerAndrew Or <andrew@databricks.com>2015-02-16 18:08:02 -0800
commit58a82a7882d7a8a7e4064278c4bf28607d9a42ba (patch)
tree77d158553aedfaf13e2722e6fa1ca5129477d3bc /core
parent16687651f05bde8ff2e2fcef100383168958bf7f (diff)
downloadspark-58a82a7882d7a8a7e4064278c4bf28607d9a42ba.tar.gz
spark-58a82a7882d7a8a7e4064278c4bf28607d9a42ba.tar.bz2
spark-58a82a7882d7a8a7e4064278c4bf28607d9a42ba.zip
[SPARK-5849] Handle more types of invalid JSON requests in SubmitRestProtocolMessage.parseAction
This patch improves SubmitRestProtocol's handling of invalid JSON requests in cases where those requests were parsable as JSON but not as JSON objects (e.g. they could be parsed as ararys or strings). I replaced an unchecked cast with pattern-matching and added a new test case. Author: Josh Rosen <joshrosen@databricks.com> Closes #4637 from JoshRosen/rest-protocol-cast and squashes the following commits: b3f282b [Josh Rosen] [SPARK-5849] Handle more types of invalid JSON in SubmitRestProtocolMessage.parseAction
Diffstat (limited to 'core')
-rw-r--r--core/src/main/scala/org/apache/spark/deploy/rest/SubmitRestProtocolMessage.scala16
-rw-r--r--core/src/test/scala/org/apache/spark/deploy/rest/StandaloneRestSubmitSuite.scala4
2 files changed, 12 insertions, 8 deletions
diff --git a/core/src/main/scala/org/apache/spark/deploy/rest/SubmitRestProtocolMessage.scala b/core/src/main/scala/org/apache/spark/deploy/rest/SubmitRestProtocolMessage.scala
index b877898231..8f36635674 100644
--- a/core/src/main/scala/org/apache/spark/deploy/rest/SubmitRestProtocolMessage.scala
+++ b/core/src/main/scala/org/apache/spark/deploy/rest/SubmitRestProtocolMessage.scala
@@ -17,8 +17,6 @@
package org.apache.spark.deploy.rest
-import scala.util.Try
-
import com.fasterxml.jackson.annotation._
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
import com.fasterxml.jackson.annotation.JsonInclude.Include
@@ -111,12 +109,14 @@ private[spark] object SubmitRestProtocolMessage {
* If the action field is not found, throw a [[SubmitRestMissingFieldException]].
*/
def parseAction(json: String): String = {
- parse(json).asInstanceOf[JObject].obj
- .find { case (f, _) => f == "action" }
- .map { case (_, v) => v.asInstanceOf[JString].s }
- .getOrElse {
- throw new SubmitRestMissingFieldException(s"Action field not found in JSON:\n$json")
- }
+ val value: Option[String] = parse(json) match {
+ case JObject(fields) =>
+ fields.collectFirst { case ("action", v) => v }.collect { case JString(s) => s }
+ case _ => None
+ }
+ value.getOrElse {
+ throw new SubmitRestMissingFieldException(s"Action field not found in JSON:\n$json")
+ }
}
/**
diff --git a/core/src/test/scala/org/apache/spark/deploy/rest/StandaloneRestSubmitSuite.scala b/core/src/test/scala/org/apache/spark/deploy/rest/StandaloneRestSubmitSuite.scala
index a345e06ecb..2fa90e3bd1 100644
--- a/core/src/test/scala/org/apache/spark/deploy/rest/StandaloneRestSubmitSuite.scala
+++ b/core/src/test/scala/org/apache/spark/deploy/rest/StandaloneRestSubmitSuite.scala
@@ -245,6 +245,7 @@ class StandaloneRestSubmitSuite extends FunSuite with BeforeAndAfterEach {
val goodJson = constructSubmitRequest(masterUrl).toJson
val badJson1 = goodJson.replaceAll("action", "fraction") // invalid JSON
val badJson2 = goodJson.substring(goodJson.size / 2) // malformed JSON
+ val notJson = "\"hello, world\""
val (response1, code1) = sendHttpRequestWithResponse(submitRequestPath, "POST") // missing JSON
val (response2, code2) = sendHttpRequestWithResponse(submitRequestPath, "POST", badJson1)
val (response3, code3) = sendHttpRequestWithResponse(submitRequestPath, "POST", badJson2)
@@ -252,6 +253,7 @@ class StandaloneRestSubmitSuite extends FunSuite with BeforeAndAfterEach {
val (response5, code5) = sendHttpRequestWithResponse(s"$killRequestPath/", "POST")
val (response6, code6) = sendHttpRequestWithResponse(statusRequestPath, "GET") // missing ID
val (response7, code7) = sendHttpRequestWithResponse(s"$statusRequestPath/", "GET")
+ val (response8, code8) = sendHttpRequestWithResponse(submitRequestPath, "POST", notJson)
// these should all fail as error responses
getErrorResponse(response1)
getErrorResponse(response2)
@@ -260,6 +262,7 @@ class StandaloneRestSubmitSuite extends FunSuite with BeforeAndAfterEach {
getErrorResponse(response5)
getErrorResponse(response6)
getErrorResponse(response7)
+ getErrorResponse(response8)
assert(code1 === HttpServletResponse.SC_BAD_REQUEST)
assert(code2 === HttpServletResponse.SC_BAD_REQUEST)
assert(code3 === HttpServletResponse.SC_BAD_REQUEST)
@@ -267,6 +270,7 @@ class StandaloneRestSubmitSuite extends FunSuite with BeforeAndAfterEach {
assert(code5 === HttpServletResponse.SC_BAD_REQUEST)
assert(code6 === HttpServletResponse.SC_BAD_REQUEST)
assert(code7 === HttpServletResponse.SC_BAD_REQUEST)
+ assert(code8 === HttpServletResponse.SC_BAD_REQUEST)
}
test("bad request paths") {