Skip to content

@ContextValue does not work for Mono argument #355

Closed
@myurish

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());
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions