package pl.openrest.dto.validation.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;

import pl.openrest.dto.mapper.BeforeCreateMappingHandler;
import pl.openrest.dto.mapper.BeforeUpdateMappingHandler;
import pl.openrest.dto.mapper.MappingManager;
import pl.openrest.dto.validation.DtoFieldExpressionValidator;
import pl.openrest.dto.validation.handler.ValidationContextHandler;
import pl.openrest.dto.validation.handler.ValidatorInvoker;

@Configuration
public class OpenRestDtoValidationConfiguration {

    @Autowired
    private Validator validator;

    @Autowired(required = false)
    private DtoFieldExpressionValidator dtoFieldExpressionValidator;

    @Bean
    public ValidatorInvoker validatorInvoker() {
        return new ValidatorInvoker();
    }

    @Autowired
    public void addDtoValidatorInvokerHandler(MappingManager mappingManager) {
        ValidatorInvoker validatorInvoker = validatorInvoker();
        ValidationContextHandler contextHandler = new ValidationContextHandler();
        mappingManager.addHandler((BeforeCreateMappingHandler) contextHandler);
        mappingManager.addHandler((BeforeUpdateMappingHandler) contextHandler);
        mappingManager.addHandler((BeforeCreateMappingHandler) validatorInvoker);
        mappingManager.addHandler((BeforeUpdateMappingHandler) validatorInvoker);
    }

    @Autowired
    private void addValidators(ValidatorInvoker invoker) {
        invoker.addValidator(validator);
        if (dtoFieldExpressionValidator != null) {
            invoker.addValidator(dtoFieldExpressionValidator);
        }
    }

}

@@ -213,16 +213,132 @@
 ration;%0A
+import org.springframework.context.annotation.Scope;%0Aimport org.springframework.context.annotation.ScopedProxyMode;%0A
 import o
@@ -604,16 +604,77 @@
 idator;%0A
+import pl.openrest.dto.validation.handler.ValidationContext;%0A
 import p
@@ -1207,16 +1207,67 @@
 gManager
+, ValidationContextHandler validationContextHandler
 ) %7B%0A    
@@ -1324,90 +1324,8 @@
 nvok
-er();%0A        ValidationContextHandler contextHandler = new ValidationContextHandl
 er()
@@ -1381,33 +1381,43 @@
 MappingHandler) 
-c
+validationC
 ontextHandler);%0A
@@ -1479,17 +1479,27 @@
 andler) 
-c
+validationC
 ontextHa
@@ -1923,10 +1923,319 @@
 %0A    %7D%0A%0A
+    @Bean%0A    @Scope(value = %22request%22, proxyMode = ScopedProxyMode.TARGET_CLASS)%0A    public ValidationContext validationContext() %7B%0A        return new ValidationContext();%0A    %7D%0A%0A    @Bean%0A    public ValidationContextHandler validationContextHandler() %7B%0A        return new ValidationContextHandler();%0A    %7D%0A%0A
 %7D%0A

Add request scoped ValidationContext and ValidationContextHandler