|
1
|
|
|
package easytests.config; |
|
2
|
|
|
|
|
3
|
|
|
import com.atlassian.oai.validator.springmvc.SwaggerValidationFilter; |
|
4
|
|
|
import com.atlassian.oai.validator.springmvc.SwaggerValidationInterceptor; |
|
5
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
6
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
|
7
|
|
|
import java.io.IOException; |
|
8
|
|
|
import javax.servlet.Filter; |
|
9
|
|
|
import org.apache.commons.io.FileUtils; |
|
10
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
11
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
12
|
|
|
import org.springframework.context.annotation.Bean; |
|
13
|
|
|
import org.springframework.context.annotation.Configuration; |
|
14
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
15
|
|
|
import org.springframework.core.io.Resource; |
|
16
|
|
|
import org.springframework.core.io.support.EncodedResource; |
|
17
|
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
|
18
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
|
19
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author malinink |
|
24
|
|
|
*/ |
|
25
|
|
|
@Configuration |
|
26
|
|
|
@EnableWebMvc |
|
27
|
|
|
public class SwaggerRequestValidationConfig implements WebMvcConfigurer { |
|
28
|
|
|
|
|
29
|
|
|
private final SwaggerValidationInterceptor swaggerValidationInterceptor; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param yamlResource the {@link Resource} to your Swagger schema |
|
33
|
|
|
*/ |
|
34
|
|
|
@Autowired |
|
35
|
|
|
public SwaggerRequestValidationConfig( |
|
36
|
|
|
@Value("classpath:api/api.yaml") final Resource yamlResource |
|
37
|
|
|
) throws IOException { |
|
38
|
|
|
final String json = (new ObjectMapper()).writeValueAsString( |
|
39
|
|
|
(new ObjectMapper(new YAMLFactory())).readValue( |
|
40
|
|
|
FileUtils.readFileToString(yamlResource.getFile()), |
|
41
|
|
|
Object.class |
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
final EncodedResource swaggerResource = new EncodedResource(new ByteArrayResource(json.getBytes()), "UTF-8"); |
|
46
|
|
|
this.swaggerValidationInterceptor = new SwaggerValidationInterceptor(swaggerResource); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
@Bean |
|
50
|
|
|
public Filter swaggerValidationFilter() { |
|
51
|
|
|
return new SwaggerValidationFilter(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
@Override |
|
55
|
|
|
public void addInterceptors(final InterceptorRegistry registry) { |
|
56
|
|
|
registry.addInterceptor(this.swaggerValidationInterceptor); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|