Skip to content

Commit

Permalink
Fix NPE while DocService scans gRPC services (line#1715)
Browse files Browse the repository at this point in the history
Motivation:

While `GrpcDocServicePlugin` scans the gRPC services to retrieve their
`ServiceDescriptor`s, a `NullPointerException` can occur in some
services.

Modifications:

- Skip the service whose `getServiceDescriptor()` and
  `getSchemaDesciptor()` return `null`.
- Skip the service whose `getSchemaDescriptor()` returns a descriptor
  that's not a `ProtoFileDescriptorSupplier`.

Result:

- No more NPE
- Fixes line#1705
  • Loading branch information
trustin committed Apr 15, 2019
1 parent 75dcb7f commit 07d5b7a
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,27 @@ public ServiceSpecification generateSpecification(Set<ServiceConfig> serviceConf
}
}
final Set<MediaType> supportedMediaTypes = supportedMediaTypesBuilder.build();
for (ServerServiceDefinition service : grpcService.services()) {
final String serviceName = service.getServiceDescriptor().getName();
map.computeIfAbsent(
serviceName,
s -> {
final FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier)
service.getServiceDescriptor().getSchemaDescriptor()).getFileDescriptor();
final ServiceDescriptor serviceDescriptor =
fileDescriptor.getServices().stream()
.filter(sd -> sd.getFullName().equals(
serviceName))
.findFirst()
.orElseThrow(IllegalStateException::new);
return new ServiceEntryBuilder(serviceDescriptor);
});
}

// Find the ServiceDescriptors of all services and put them all into the 'map'
// after wrapping with ServiceEntryBuilder.
grpcService.services().stream()
.map(ServerServiceDefinition::getServiceDescriptor)
.filter(Objects::nonNull)
.filter(desc -> desc.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier)
.forEach(desc -> {
final String serviceName = desc.getName();
map.computeIfAbsent(serviceName, s -> {
final ProtoFileDescriptorSupplier fileDescSupplier =
(ProtoFileDescriptorSupplier) desc.getSchemaDescriptor();
final FileDescriptor fileDesc = fileDescSupplier.getFileDescriptor();
final ServiceDescriptor serviceDesc =
fileDesc.getServices().stream()
.filter(sd -> sd.getFullName().equals(serviceName))
.findFirst()
.orElseThrow(IllegalStateException::new);
return new ServiceEntryBuilder(serviceDesc);
});
});

final String pathPrefix;
if (serviceConfig.pathMapping().prefix().isPresent()) {
Expand Down

0 comments on commit 07d5b7a

Please sign in to comment.