Skip to content

Commit

Permalink
fix: Avoid NPE in GrpcMetadataImpl #1854
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren committed Oct 11, 2023
1 parent 9ce044e commit d3120a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions runtime/src/main/scala/akka/grpc/GrpcServiceException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ object GrpcServiceException {

val statusRuntimeException = StatusProto.toStatusRuntimeException(status.build)

new GrpcServiceException(statusRuntimeException.getStatus, new GrpcMetadataImpl(statusRuntimeException.getTrailers))
new GrpcServiceException(
statusRuntimeException.getStatus,
new GrpcMetadataImpl(
// might not be present
Option(statusRuntimeException.getTrailers).getOrElse(new io.grpc.Metadata())))
}

private def toJavaProto(scalaPbSource: com.google.protobuf.any.Any): com.google.protobuf.Any = {
Expand All @@ -50,7 +54,12 @@ object GrpcServiceException {
}

def apply(ex: StatusRuntimeException): GrpcServiceException = {
new GrpcServiceException(ex.getStatus, new RichGrpcMetadataImpl(ex.getStatus, ex.getTrailers))
new GrpcServiceException(
ex.getStatus,
new RichGrpcMetadataImpl(
ex.getStatus,
// might not be present
Option(ex.getTrailers).getOrElse(new io.grpc.Metadata())))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import scalapb.{ GeneratedMessage, GeneratedMessageCompanion }
*/
@InternalApi
class GrpcMetadataImpl(delegate: io.grpc.Metadata) extends Metadata {
require(delegate != null, "Metadata delegate must be present")
private lazy val map = delegate.keys.iterator.asScala.map(key => key -> getEntries(key)).toMap

override def getText(key: String): Option[String] =
Expand Down

0 comments on commit d3120a1

Please sign in to comment.