-
Notifications
You must be signed in to change notification settings - Fork 0
/
techscore-client-codegen.groovy
137 lines (125 loc) · 6.04 KB
/
techscore-client-codegen.groovy
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@Grab(group = 'org.openapitools', module = 'openapi-generator-cli', version = '4.2.2')
import org.openapitools.codegen.*
import org.openapitools.codegen.languages.*
import io.swagger.v3.oas.models.security.*;
class TechscoreJavaClientCodegen extends JavaClientCodegen {
static main(String[] args) {
OpenAPIGenerator.main(args)
}
TechscoreJavaClientCodegen() {
super()
}
String name = "techscore-codegen"
@Override
public void processOpts() {
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
super.processOpts()
if (WEBCLIENT.equals(getLibrary())) {
// add WebClientConfig
supportingFiles.add(new SupportingFile("WebClientConfig.mustache", invokerFolder, "WebClientConfig.java"))
// add application.yml
supportingFiles.add(new SupportingFile("application.yml.mustache", projectFolder + '/resources', "application.yml"))
}
}
@Override
@SuppressWarnings("static-method")
public List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> securitySchemeMap) {
if (securitySchemeMap == null) {
return Collections.emptyList();
}
List<CodegenSecurity> codegenSecurities = new ArrayList<CodegenSecurity>(securitySchemeMap.size());
for (String key : securitySchemeMap.keySet()) {
final SecurityScheme securityScheme = securitySchemeMap.get(key);
CodegenSecurity cs = CodegenModelFactory.newInstance(CodegenModelType.SECURITY);
cs.name = key;
cs.type = securityScheme.getType().toString();
cs.isCode = cs.isPassword = cs.isApplication = cs.isImplicit = false;
cs.isBasicBasic = cs.isBasicBearer = false;
cs.scheme = securityScheme.getScheme();
if (SecurityScheme.Type.APIKEY.equals(securityScheme.getType())) {
cs.isBasic = cs.isOAuth = false;
cs.isApiKey = true;
cs.keyParamName = securityScheme.getName();
cs.isKeyInHeader = securityScheme.getIn() == SecurityScheme.In.HEADER;
cs.isKeyInQuery = securityScheme.getIn() == SecurityScheme.In.QUERY;
cs.isKeyInCookie = securityScheme.getIn() == SecurityScheme.In.COOKIE; //it assumes a validation step prior to generation. (cookie-auth supported from OpenAPI 3.0.0)
} else if (SecurityScheme.Type.HTTP.equals(securityScheme.getType())) {
cs.isKeyInHeader = cs.isKeyInQuery = cs.isKeyInCookie = cs.isApiKey = cs.isOAuth = false;
cs.isBasic = true;
if ("basic".equals(securityScheme.getScheme())) {
cs.isBasicBasic = true;
} else if ("bearer".equals(securityScheme.getScheme())) {
cs.isBasicBearer = true;
cs.bearerFormat = securityScheme.getBearerFormat();
}
} else if (SecurityScheme.Type.OAUTH2.equals(securityScheme.getType())) {
cs.isKeyInHeader = cs.isKeyInQuery = cs.isKeyInCookie = cs.isApiKey = cs.isBasic = false;
cs.isOAuth = true;
final OAuthFlows flows = securityScheme.getFlows();
if (securityScheme.getFlows() == null) {
throw new RuntimeException("missing oauth flow in " + cs.name);
}
if (flows.getPassword() != null) {
setOauth2Info(cs, flows.getPassword());
cs.isPassword = true;
cs.flow = "password";
} else if (flows.getImplicit() != null) {
setOauth2Info(cs, flows.getImplicit());
cs.isImplicit = true;
cs.flow = "implicit";
} else if (flows.getClientCredentials() != null) {
setOauth2Info(cs, flows.getClientCredentials());
cs.isApplication = true;
cs.flow = "application";
} else if (flows.getAuthorizationCode() != null) {
setOauth2Info(cs, flows.getAuthorizationCode());
cs.isCode = true;
cs.flow = "accessCode";
} else {
throw new RuntimeException("Could not identify any oauth2 flow in " + cs.name);
}
}
codegenSecurities.add(cs);
}
// sort auth methods to maintain the same order
Collections.sort(codegenSecurities, new Comparator<CodegenSecurity>() {
@Override
public int compare(CodegenSecurity one, CodegenSecurity another) {
return ObjectUtils.compare(one.name, another.name);
}
});
// set 'hasMore'
Iterator<CodegenSecurity> it = codegenSecurities.iterator();
while (it.hasNext()) {
final CodegenSecurity security = it.next();
security.hasMore = it.hasNext();
}
return codegenSecurities;
}
private void setOauth2Info(CodegenSecurity codegenSecurity, OAuthFlow flow) {
codegenSecurity.authorizationUrl = flow.getAuthorizationUrl();
codegenSecurity.tokenUrl = flow.getTokenUrl();
if (flow.getScopes() != null && !flow.getScopes().isEmpty()) {
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
int count = 0, numScopes = flow.getScopes().size();
for (Map.Entry<String, String> scopeEntry : flow.getScopes().entrySet()) {
Map<String, Object> scope = new HashMap<String, Object>();
scope.put("scope", scopeEntry.getKey());
scope.put("description", escapeText(scopeEntry.getValue()));
count += 1;
if (count < numScopes) {
scope.put("hasMore", "true");
} else {
scope.put("hasMore", null);
}
scopes.add(scope);
}
codegenSecurity.scopes = scopes;
}
if (flow.getExtensions() != null) {
Map<String, Object> extensions = flow.getExtensions();
codegenSecurity.vendorExtensions = extensions;
}
}
}