Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ServerWebInputException is throw each time Http request with mandatory request parameters is handled. [SPR-17338] #21872

Closed
spring-projects-issues opened this issue Oct 4, 2018 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 4, 2018

Dmytro Mrachkovskyi opened SPR-17338 and commented

ServerWebInputException is throw each time Http request with mandatory request parameters is handled in AbstractNamedValueArgumentResolver. Even though this does not brake request handling flow, exception (and stack trace)  is generated each time and it has performance impact. 

Root-cause: 

In

 

org.springframework.web.reactive.result.method.annotation.AbstractNamedValueArgumentResolver#resolveArgument 

 return this.resolveName(resolvedName.toString(), nestedParameter, exchange)
.flatMap((arg) -> mappingFunction())
.switchIfEmpty(this.getDefaultValue(namedValueInfo, parameter, bindingContext, model, exchange));

switchIfEmpty takes default value as an argument. As for mandatory arguments there is no default value ServerWebInputException will be thrown in getDefaultValue() method and wrapped into Mono.error(ex). This error mono will never be used until mandatory request parameter exists, but Exception it self is crated when there is no need in it.

Fix. Default value should be evaluated only if request parameter is empty: 

package org.springframework.web.reactive.result.method.annotation;

public abstract class AbstractNamedValueArgumentResolver extends HandlerMethodArgumentResolverSupport {

...

private Mono<Object> getDefaultValue(NamedValueInfo namedValueInfo, MethodParameter parameter,
        BindingContext bindingContext, Model model, ServerWebExchange exchange) {
        return Mono.fromSupplier(() -> {
            Object value = null;
            if (namedValueInfo.defaultValue != null) {
                value = resolveStringValue(namedValueInfo.defaultValue);
            }
            else if (namedValueInfo.required && !parameter.isOptional()) {
                handleMissingValue(namedValueInfo.name, parameter, exchange);
            }
            value = handleNullValue(namedValueInfo.name, value, parameter.getNestedParameterType());
            value = applyConversion(value, namedValueInfo, parameter, bindingContext, exchange);
            handleResolvedValue(value, namedValueInfo.name, parameter, model, exchange);
            return value;
        });
}

Affects: 5.0.9, 5.1 GA

Referenced from: commits 983bce1, 8422976

Backported to: 5.0.10

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 4, 2018

Dmytro Mrachkovskyi commented

Pull request

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 4, 2018

Rossen Stoyanchev commented

Indeed this is eagerly creating an Exception instance only to supply a default value that might not be needed in the end.

@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.1.1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants