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

fix: Avoid NPE in GrpcMetadataImpl #1854 #1855

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but why we use RichGrpcMetadataImpl, but above we just use GrpcMetadataImpl?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to propagate the structured error details if present, but maybe cleaner and more correct to select between GrpcMetadataImpl and RichGrpcMedataImpl on the presence of trailers instead of giving an empty metadata like I did. I'll do that instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looking at it some more, actually, I'm not so sure why we are not passing RichGrpcMetadataImpl above, that looks like the right thing to do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

312a11a should do the trick

// 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