dgs-framework icon indicating copy to clipboard operation
dgs-framework copied to clipboard

bug: [webflux] ServerRequest.bodyToMono returns empty

Open kdby-io opened this issue 3 years ago • 1 comments

Expected behavior

ServerRequest.bodyToMono returns its body.

Actual behavior

ServeRequest.bodyToMono returns Mono.empty().

Steps to reproduce

  1. Use graphql-dgs-example-java-webflux example.
  2. Modify MyContextBuilder.build() as below
    public Mono<MyContext> build(@Nullable Map<String, ?> extensions, @Nullable HttpHeaders headers, @Nullable ServerRequest serverRequest) {
        assert serverRequest != null;
        return Mono.just(new MyContext())
                .doOnNext(body -> System.out.println("before bodyToMono"))
                .flatMap(c -> serverRequest.bodyToMono(String.class))
                .doOnNext(System.out::println)
                .doOnNext(body -> System.out.println("after bodyToMono"))
                .map(body -> new MyContext());
    }
  1. Run its server
  2. Request with { usingContext } query in localhost:8080/graphiql
  3. See only before bodyToMono is printed

kdby-io avatar Sep 19 '22 07:09 kdby-io

Thanks for reporting!

srinivasankavitha avatar Sep 19 '22 16:09 srinivasankavitha

The request body is already consumed in DefaultDgsWebfluxHttpHandler by the time your context builder is run, so the bodyToMono call is returning an empty Mono. I think this is expected behavior, as it's necessary for the body to get consumed by the HTTP handler, and unfortunately I don't think there is much that DGS itself can do about it. I think you'd need to implement a WebFilter and cache the request somehow.

kilink avatar Jan 28 '24 02:01 kilink