enif-lee / grpc-dotnet-validator

Simple request message validator for grpc.aspnet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validation does not work for Services which returns stream

WSebo opened this issue · comments

commented

Hello, everything works fine, but when I am testing getting many items from service using stream, the validator does not work.
Service looks like:
rpc GetManyItems (ItemsGetManyQuery) returns (stream ItemResponse);

commented

Hello. I fixed it by overriding ServerStreamingServerHandler in ValidationInterceptor.

public override async Task ServerStreamingServerHandler<TRequest, TResponse>(
            TRequest request,
            IServerStreamWriter<TResponse> responseStream,
            ServerCallContext context,
            ServerStreamingServerMethod<TRequest, TResponse> continuation)
        {
            if (_locator.TryGetValidator<TRequest>(out var validator))
            {
                var results = await validator.ValidateAsync(request);
                if (!results.IsValid)
                {
                    var message = await _handler.HandleAsync(results.Errors);
                    throw new RpcException(new Status(StatusCode.InvalidArgument, message));
                }
            }

            await continuation(request, responseStream, context);
        }

@WSebo Ok, Got it, I will publish new version with fixing this issue.