File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed 
main/scala/org/apache/spark/util 
test/scala/org/apache/spark/util Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -874,8 +874,14 @@ private[spark] object JsonProtocol extends JsonUtils {
874874      case  `stageExecutorMetrics` =>  stageExecutorMetricsFromJson(json)
875875      case  `blockUpdate` =>  blockUpdateFromJson(json)
876876      case  `resourceProfileAdded` =>  resourceProfileAddedFromJson(json)
877-       case  other =>  mapper.readValue(json.toString, Utils .classForName(other))
878-         .asInstanceOf [SparkListenerEvent ]
877+       case  other => 
878+         val  otherClass  =  Utils .classForName(other)
879+         if  (classOf [SparkListenerEvent ].isAssignableFrom(otherClass)) {
880+           mapper.readValue(json.toString, otherClass)
881+             .asInstanceOf [SparkListenerEvent ]
882+         } else  {
883+           throw  new  SparkException (s " Unknown event type:  $other" )
884+         }
879885    }
880886  }
881887
Original file line number Diff line number Diff line change @@ -874,6 +874,36 @@ class JsonProtocolSuite extends SparkFunSuite {
874874    val  jobFailedEvent  =  JsonProtocol .sparkEventFromJson(exJobFailureNoStackJson)
875875    testEvent(jobFailedEvent, exJobFailureExpectedJson)
876876  }
877+ 
878+   test(" SPARK-52381: handle class not found"  ) {
879+     val  unknownJson  = 
880+       """ {
881+         |  "Event" : "com.example.UnknownEvent", 
882+         |  "foo" : "foo" 
883+         |}"""  .stripMargin
884+     try  {
885+       JsonProtocol .sparkEventFromJson(unknownJson)
886+       fail(" Expected ClassNotFoundException for unknown event type"  )
887+     } catch  {
888+       case  e : ClassNotFoundException  => 
889+     }
890+   }
891+ 
892+   test(" SPARK-52381: only read classes that extend SparkListenerEvent"  ) {
893+     val  unknownJson  = 
894+       """ {
895+         |  "Event" : "org.apache.spark.SparkException", 
896+         |  "foo" : "foo" 
897+         |}"""  .stripMargin
898+     try  {
899+       JsonProtocol .sparkEventFromJson(unknownJson)
900+       fail(" Expected SparkException for unknown event type"  )
901+     } catch  {
902+       case  e : SparkException  => 
903+         assert(e.getMessage.startsWith(" Unknown event type"  ))
904+     }
905+   }
906+ 
877907}
878908
879909
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments