Skip to content

@ContextValue does not work for Mono argument #355

Closed

Description

@myurish

When an @SchemaMapping annotated controller method has a parameter that is annotated with @ContextValue and whose type is Mono, it will be called with null when the corresponding value in the GraphQLContext is an empty Mono.

When the GraphQLContext contains a mapping from value to Mono.empty(), the below code will throw an NPE:

@QueryMapping("SomeField")
fun someField(@ContextValue value: Mono<SomeContextType>) = value.map { ... }

I believe the cause is in InvocableHandlerMethodSupport.toArgsMono(Object[] args). This code replaces empty Mono instances in the generated arguments list with null if they are empty. This works fine for unwrapping Mono values stored in the GraphQLContext for an annotated controller with a @ContextValue parameter whose type is the Mono's element type, such as a Mono<String> value in the context to a String (or null) parameter value, but is an issue with a Mono parameter type since it gets invoked with null.

Edit:

This is the code that replaced the empty Mono with null regardless of the @ContextValue parameter type:

protected Mono<Object[]> toArgsMono(Object[] args) {
	List<Mono<Object>> monoList = Arrays.stream(args)
			.map(arg -> {
				Mono<Object> argMono = (arg instanceof Mono ? (Mono<Object>) arg : Mono.justOrEmpty(arg));
				return argMono.defaultIfEmpty(NO_VALUE);
			})
			.collect(Collectors.toList());
		return Mono.zip(monoList,
			values -> Stream.of(values).map(value -> value != NO_VALUE ? value : null).toArray());
}

Activity

  1. added this to the 1.0.0 milestone on May 5, 2022
  2. added
    type: bug
    and removed
    status: waiting-for-triage
    on May 5, 2022
  3. changed the title [-]Annotated controller method @ContextValue parameter called with null for empty Mono in GraphQLContext despite parameter type being Mono[/-] [+]@ContextValue does not work for Mono argument[/+] on May 5, 2022
  4. self-assigned this
    on May 6, 2022
  5. added a commit that references this issue on Feb 13, 2024
    07293ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions