1
1
package com .algolia .codegen ;
2
2
3
+ import io .swagger .v3 .oas .models .Operation ;
4
+ import io .swagger .v3 .oas .models .media .Schema ;
5
+ import io .swagger .v3 .oas .models .servers .Server ;
6
+ import java .io .FileInputStream ;
7
+ import java .net .URL ;
8
+ import java .util .*;
3
9
import org .openapitools .codegen .*;
4
10
import org .openapitools .codegen .languages .JavaClientCodegen ;
5
11
import org .openapitools .codegen .utils .ModelUtils ;
6
12
import org .yaml .snakeyaml .Yaml ;
7
13
8
- import java .util .*;
9
- import java .io .FileInputStream ;
10
- import java .net .URL ;
11
-
12
- import io .swagger .v3 .oas .models .Operation ;
13
- import io .swagger .v3 .oas .models .media .Schema ;
14
- import io .swagger .v3 .oas .models .servers .Server ;
15
-
16
14
@ SuppressWarnings ("unchecked" )
17
15
public class AlgoliaJavaGenerator extends JavaClientCodegen {
16
+
18
17
/**
19
- * Configures a friendly name for the generator. This will be used by the
20
- * generator
21
- * to select the library with the -g flag.
18
+ * Configures a friendly name for the generator. This will be used by the generator to select the
19
+ * library with the -g flag.
22
20
*
23
21
* @return the friendly name for the generator
24
22
*/
@@ -27,15 +25,17 @@ public String getName() {
27
25
return "algolia-java" ;
28
26
}
29
27
30
- /**
31
- * Inject server info into the client to generate the right URL
32
- */
28
+ /** Inject server info into the client to generate the right URL */
33
29
private void generateServer (Map <String , Object > client ) {
34
30
String clientName = (String ) client .get ("pathPrefix" );
35
31
Yaml yaml = new Yaml ();
36
32
try {
37
- Map <String , Object > spec = yaml .load (new FileInputStream ("specs/" + clientName + "/spec.yml" ));
38
- List <Map <String , Object >> servers = (List <Map <String , Object >>) spec .get ("servers" );
33
+ Map <String , Object > spec = yaml .load (
34
+ new FileInputStream ("specs/" + clientName + "/spec.yml" )
35
+ );
36
+ List <Map <String , Object >> servers = (List <Map <String , Object >>) spec .get (
37
+ "servers"
38
+ );
39
39
40
40
boolean hasRegionalHost = false ;
41
41
boolean fallbackToAliasHost = false ;
@@ -47,19 +47,28 @@ private void generateServer(Map<String, Object> client) {
47
47
48
48
for (Map <String , Object > server : servers ) {
49
49
if (!server .containsKey ("url" )) {
50
- throw new GenerationException ("Invalid server, does not contains 'url'" );
50
+ throw new GenerationException (
51
+ "Invalid server, does not contains 'url'"
52
+ );
51
53
}
52
54
53
55
if (!server .containsKey ("variables" )) {
54
56
continue ;
55
57
}
56
58
57
- Map <String , Map <String , Object >> variables = (Map <String , Map <String , Object >>) server .get ("variables" );
59
+ Map <String , Map <String , Object >> variables = (Map <String , Map <String , Object >>) server .get (
60
+ "variables"
61
+ );
58
62
59
- if (!variables .containsKey ("region" ) || !variables .get ("region" ).containsKey ("enum" )) {
63
+ if (
64
+ !variables .containsKey ("region" ) ||
65
+ !variables .get ("region" ).containsKey ("enum" )
66
+ ) {
60
67
continue ;
61
68
}
62
- ArrayList <String > enums = (ArrayList <String >) variables .get ("region" ).get ("enum" );
69
+ ArrayList <String > enums = (ArrayList <String >) variables
70
+ .get ("region" )
71
+ .get ("enum" );
63
72
hasRegionalHost = true ;
64
73
65
74
URL url = new URL ((String ) server .get ("url" ));
@@ -95,18 +104,30 @@ private void generateServer(Map<String, Object> client) {
95
104
}
96
105
97
106
@ Override
98
- public CodegenOperation fromOperation (String path , String httpMethod , Operation operation , List <Server > servers ) {
99
- return Utils .specifyCustomRequest (super .fromOperation (path , httpMethod , operation , servers ));
107
+ public CodegenOperation fromOperation (
108
+ String path ,
109
+ String httpMethod ,
110
+ Operation operation ,
111
+ List <Server > servers
112
+ ) {
113
+ return Utils .specifyCustomRequest (
114
+ super .fromOperation (path , httpMethod , operation , servers )
115
+ );
100
116
}
101
117
102
- /**
103
- * Provides an opportunity to inspect and modify operation data before the code
104
- * is generated.
105
- */
118
+ /** Provides an opportunity to inspect and modify operation data before the code is generated. */
106
119
@ Override
107
- public Map <String , Object > postProcessOperationsWithModels (Map <String , Object > objs , List <Object > allModels ) {
108
- Map <String , Object > results = super .postProcessOperationsWithModels (objs , allModels );
109
- Map <String , Object > client = (Map <String , Object >) results .get ("operations" );
120
+ public Map <String , Object > postProcessOperationsWithModels (
121
+ Map <String , Object > objs ,
122
+ List <Object > allModels
123
+ ) {
124
+ Map <String , Object > results = super .postProcessOperationsWithModels (
125
+ objs ,
126
+ allModels
127
+ );
128
+ Map <String , Object > client = (Map <String , Object >) results .get (
129
+ "operations"
130
+ );
110
131
111
132
generateServer (client );
112
133
@@ -118,7 +139,11 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
118
139
Map <String , Object > models = super .postProcessAllModels (objs );
119
140
120
141
for (Object modelContainer : models .values ()) {
121
- CodegenModel model = ((Map <String , List <Map <String , CodegenModel >>>) modelContainer ).get ("models" ).get (0 )
142
+ CodegenModel model =
143
+ ((Map <String , List <Map <String , CodegenModel >>>) modelContainer ).get (
144
+ "models"
145
+ )
146
+ .get (0 )
122
147
.get ("model" );
123
148
if (!model .oneOf .isEmpty ()) {
124
149
List <HashMap <String , String >> listOneOf = new ArrayList ();
@@ -127,7 +152,10 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
127
152
HashMap <String , String > hashMapOneOf = new HashMap ();
128
153
129
154
hashMapOneOf .put ("type" , iterateModel );
130
- hashMapOneOf .put ("name" , iterateModel .replace ("<" , "" ).replace (">" , "" ));
155
+ hashMapOneOf .put (
156
+ "name" ,
157
+ iterateModel .replace ("<" , "" ).replace (">" , "" )
158
+ );
131
159
132
160
listOneOf .add (hashMapOneOf );
133
161
}
@@ -141,23 +169,33 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
141
169
}
142
170
143
171
@ Override
144
- public Map <String , Object > postProcessSupportingFileData (Map <String , Object > objs ) {
172
+ public Map <String , Object > postProcessSupportingFileData (
173
+ Map <String , Object > objs
174
+ ) {
145
175
Map <String , Object > bundle = super .postProcessSupportingFileData (objs );
146
- List <Map <String , Object >> apis = ((Map <String , List <Map <String , Object >>>) bundle .get ("apiInfo" )).get ("apis" );
176
+ List <Map <String , Object >> apis =
177
+ ((Map <String , List <Map <String , Object >>>) bundle .get ("apiInfo" )).get (
178
+ "apis"
179
+ );
147
180
for (Map <String , Object > api : apis ) {
148
- List <CodegenOperation > operations = ((Map <String , List <CodegenOperation >>) api .get ("operations" ))
149
- .get ("operation" );
181
+ List <CodegenOperation > operations =
182
+ ((Map <String , List <CodegenOperation >>) api .get ("operations" )).get (
183
+ "operation"
184
+ );
150
185
151
186
for (CodegenOperation ope : operations ) {
152
- ope .returnType = ope .returnType .replace ("Map<" , "HashMap<" ).replace ("List<" , "ArrayList<" );
187
+ ope .returnType =
188
+ ope .returnType
189
+ .replace ("Map<" , "HashMap<" )
190
+ .replace ("List<" , "ArrayList<" );
153
191
}
154
192
}
155
193
return bundle ;
156
194
}
157
195
158
196
/**
159
- * Returns human-friendly help for the generator. Provide the consumer with help
160
- * tips, parameters here
197
+ * Returns human-friendly help for the generator. Provide the consumer with help tips, parameters
198
+ * here
161
199
*
162
200
* @return A string value for the help message
163
201
*/
@@ -170,9 +208,13 @@ public String getHelp() {
170
208
public void processOpts () {
171
209
super .processOpts ();
172
210
173
- supportingFiles .add (new SupportingFile ("EchoResponse.mustache" ,
211
+ supportingFiles .add (
212
+ new SupportingFile (
213
+ "EchoResponse.mustache" ,
174
214
"algoliasearch-core/com/algolia/utils/echo" ,
175
- "EchoResponse.java" ));
215
+ "EchoResponse.java"
216
+ )
217
+ );
176
218
177
219
// Prevent all useless file to generate
178
220
apiTestTemplateFiles .clear ();
0 commit comments