Skip to content

Commit

Permalink
fix: Avoid NPE in GrpcMetadataImpl #1854 (#1855)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren authored Oct 17, 2023
1 parent cdd9e63 commit ccc7ce2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions runtime/src/main/scala/akka/grpc/GrpcServiceException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package akka.grpc
import io.grpc.{ Status, StatusRuntimeException }
import akka.annotation.ApiMayChange
import akka.grpc.scaladsl.{ Metadata, MetadataBuilder }
import akka.grpc.internal.{ GrpcMetadataImpl, JavaMetadataImpl, RichGrpcMetadataImpl }
import akka.grpc.internal.{ JavaMetadataImpl, RichGrpcMetadataImpl }
import com.google.protobuf.any.Any
import io.grpc.protobuf.StatusProto

Expand Down Expand Up @@ -39,7 +39,9 @@ object GrpcServiceException {

val statusRuntimeException = StatusProto.toStatusRuntimeException(status.build)

new GrpcServiceException(statusRuntimeException.getStatus, new GrpcMetadataImpl(statusRuntimeException.getTrailers))
new GrpcServiceException(
statusRuntimeException.getStatus,
new RichGrpcMetadataImpl(statusRuntimeException.getStatus, statusRuntimeException.getTrailers))
}

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

def apply(ex: StatusRuntimeException): GrpcServiceException = {
new GrpcServiceException(ex.getStatus, new RichGrpcMetadataImpl(ex.getStatus, ex.getTrailers))
ex.getTrailers match {
case null =>
new GrpcServiceException(ex.getStatus)
case trailers =>
new GrpcServiceException(ex.getStatus, new RichGrpcMetadataImpl(ex.getStatus, trailers))
}

}
}

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 ccc7ce2

Please sign in to comment.