/
OpenAPIResolver.java
109 lines (87 loc) · 3.69 KB
/
OpenAPIResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package io.swagger.v3.parser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.processors.ComponentsProcessor;
import io.swagger.v3.parser.processors.OperationProcessor;
import io.swagger.v3.parser.processors.PathsProcessor;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class OpenAPIResolver {
private final OpenAPI openApi;
private final ResolverCache cache;
private final ComponentsProcessor componentsProcessor;
private final PathsProcessor pathProcessor;
private final OperationProcessor operationsProcessor;
private Settings settings = new Settings();
private Set<String> resolveValidationMessages = new HashSet<>();
public ResolverCache getCache() {
return cache;
}
public OpenAPIResolver(OpenAPI openApi) {
this(openApi, null, null, null);
}
public OpenAPIResolver(OpenAPI openApi, List<AuthorizationValue> auths) {
this(openApi, auths, null, null);
}
public OpenAPIResolver(OpenAPI openApi, List<AuthorizationValue> auths, String parentFileLocation) {
this(openApi, auths, parentFileLocation, null);
}
public OpenAPIResolver(OpenAPI openApi, List<AuthorizationValue> auths, String parentFileLocation, Settings settings) {
this(openApi, auths, parentFileLocation, settings, new ParseOptions());
}
public OpenAPIResolver(OpenAPI openApi, List<AuthorizationValue> auths, String parentFileLocation, Settings settings, ParseOptions parseOptions) {
this.openApi = openApi;
this.settings = settings != null ? settings : new Settings();
this.cache = new ResolverCache(openApi, auths, parentFileLocation, resolveValidationMessages, parseOptions);
componentsProcessor = new ComponentsProcessor(openApi,this.cache);
pathProcessor = new PathsProcessor(cache, openApi,this.settings);
operationsProcessor = new OperationProcessor(cache, openApi);
}
public void resolve(SwaggerParseResult result) {
OpenAPI resolved = resolve();
if (resolved == null) {
return;
}
result.setOpenAPI(resolved);
result.getMessages().addAll(resolveValidationMessages);
}
public OpenAPI resolve() {
if (openApi == null) {
return null;
}
pathProcessor.processPaths();
componentsProcessor.processComponents();
if(openApi.getPaths() != null) {
for(String pathname : openApi.getPaths().keySet()) {
PathItem pathItem = openApi.getPaths().get(pathname);
if(pathItem.readOperations() != null) {
for(Operation operation : pathItem.readOperations()) {
operationsProcessor.processOperation(operation);
}
}
}
}
return openApi;
}
public static class Settings {
private boolean addParametersToEachOperation = true;
/**
* If true, resource parameters are added to each operation
*/
public boolean addParametersToEachOperation() {
return this.addParametersToEachOperation;
}
/**
* If true, resource parameters are added to each operation
*/
public Settings addParametersToEachOperation(final boolean addParametersToEachOperation) {
this.addParametersToEachOperation = addParametersToEachOperation;
return this;
}
}
}