Skip to content

@ContextValue does not work for Mono argument #355

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
myurish opened this issue Apr 18, 2022 · 0 comments
Closed

@ContextValue does not work for Mono argument #355

myurish opened this issue Apr 18, 2022 · 0 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@myurish
Copy link

myurish commented Apr 18, 2022

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());
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 18, 2022
@rstoyanchev rstoyanchev added this to the 1.0.0 milestone May 5, 2022
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 5, 2022
@rstoyanchev rstoyanchev 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 May 5, 2022
@rstoyanchev rstoyanchev self-assigned this May 6, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants