forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultCodegenTest.java
313 lines (256 loc) · 15.4 KB
/
DefaultCodegenTest.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DefaultCodegenTest {
@Test
public void testCamelize() throws Exception {
Assert.assertEquals(DefaultCodegen.camelize("abcd"), "Abcd");
Assert.assertEquals(DefaultCodegen.camelize("some-value"), "SomeValue");
Assert.assertEquals(DefaultCodegen.camelize("some_value"), "SomeValue");
Assert.assertEquals(DefaultCodegen.camelize("$type"), "$Type");
Assert.assertEquals(DefaultCodegen.camelize("abcd", true), "abcd");
Assert.assertEquals(DefaultCodegen.camelize("some-value", true), "someValue");
Assert.assertEquals(DefaultCodegen.camelize("some_value", true), "someValue");
Assert.assertEquals(DefaultCodegen.camelize("Abcd", true), "abcd");
Assert.assertEquals(DefaultCodegen.camelize("$type", true), "$type");
Assert.assertEquals(DefaultCodegen.camelize("123", true), "123");
Assert.assertEquals(DefaultCodegen.camelize("$123", true), "$123");
}
@Test
public void testHasBodyParameter() throws Exception {
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
Operation pingOperation = new Operation()
.responses(
new ApiResponses().addApiResponse("204", new ApiResponse()
.description("Ok response")));
Operation createOperation = new Operation()
.requestBody(new RequestBody()
.content(new Content().addMediaType("application/json",
new MediaType().schema(refSchema))))
.responses(
new ApiResponses().addApiResponse("201", new ApiResponse()
.description("Created response")));
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
openAPI.getComponents().addSchemas("Pet", new ObjectSchema());
final DefaultCodegen codegen = new DefaultCodegen();
Assert.assertEquals(codegen.hasBodyParameter(openAPI, pingOperation), false);
Assert.assertEquals(codegen.hasBodyParameter(openAPI, createOperation), true);
}
@Test
public void testGetConsumesInfoAndGetProducesInfo() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
openAPI.getComponents().addSchemas("Pet", new ObjectSchema());
openAPI.getComponents().addRequestBodies("MyRequestBody", new RequestBody()
.content(new Content().addMediaType("application/json",
new MediaType().schema(refSchema))));
openAPI.getComponents().addResponses("MyResponse", new ApiResponse()
.description("Ok response")
.content(new Content().addMediaType("application/xml",
new MediaType().schema(refSchema))));
Operation createOperation = new Operation()
.requestBody(new RequestBody()
.content(new Content()
.addMediaType("application/json", new MediaType().schema(refSchema))
.addMediaType("application/xml", new MediaType().schema(refSchema))
))
.responses(
new ApiResponses().addApiResponse("201", new ApiResponse()
.description("Created response")));
Set<String> createConsumesInfo = DefaultCodegen.getConsumesInfo(openAPI, createOperation);
Assert.assertEquals(createConsumesInfo.size(), 2);
Assert.assertTrue(createConsumesInfo.contains("application/json"), "contains 'application/json'");
Assert.assertTrue(createConsumesInfo.contains("application/xml"), "contains 'application/xml'");
Set<String> createProducesInfo = DefaultCodegen.getProducesInfo(openAPI, createOperation);
Assert.assertEquals(createProducesInfo.size(), 0);
CodegenOperation coCreate = codegen.fromOperation("somepath", "post", createOperation, openAPI.getComponents().getSchemas(), openAPI);
Assert.assertTrue(coCreate.hasConsumes);
Assert.assertEquals(coCreate.consumes.size(), 2);
Assert.assertFalse(coCreate.hasProduces);
Operation updateOperationWithRef = new Operation()
.requestBody(new RequestBody().$ref("#/components/requestBodies/MyRequestBody"))
.responses(new ApiResponses().addApiResponse("201", new ApiResponse().$ref("#/components/responses/MyResponse")));
Set<String> updateConsumesInfo = DefaultCodegen.getConsumesInfo(openAPI, updateOperationWithRef);
Assert.assertEquals(updateConsumesInfo.size(), 1);
Assert.assertTrue(updateConsumesInfo.contains("application/json"), "contains 'application/json'");
Set<String> updateProducesInfo = DefaultCodegen.getProducesInfo(openAPI, updateOperationWithRef);
Assert.assertEquals(updateProducesInfo.size(), 1);
Assert.assertTrue(updateProducesInfo.contains("application/xml"), "contains 'application/xml'");
CodegenOperation coUpdate = codegen.fromOperation("somepath", "post", updateOperationWithRef, openAPI.getComponents().getSchemas(), openAPI);
Assert.assertTrue(coUpdate.hasConsumes);
Assert.assertEquals(coUpdate.consumes.size(), 1);
Assert.assertEquals(coUpdate.consumes.get(0).get("mediaType"), "application/json");
Assert.assertTrue(coUpdate.hasProduces);
Assert.assertEquals(coUpdate.produces.size(), 1);
Assert.assertEquals(coUpdate.produces.get(0).get("mediaType"), "application/xml");
}
@Test
public void testInitialConfigValues() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
}
@Test
public void testSettersForConfigValues() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setHideGenerationTimestamp(false);
codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false );
}
@Test
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, false);
codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
}
@Test
public void testArraySchemaIsNotIncluedInAliases() throws Exception {
Map<String, Schema> schemas = new HashMap<String, Schema>() {
{
put("ArraySchemaTest", new ArraySchema());
}
};
Method method = DefaultCodegen.class.getDeclaredMethod("getAllAliases", Map.class);
method.setAccessible(true);
Map<String, String> aliases = (Map<String, String>)method.invoke(null, schemas);
Assert.assertEquals(aliases.size(), 0);
}
@Test
public void testFormParameterHasDefaultValue() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/fake").getGet().getRequestBody());
CodegenParameter codegenParameter = codegen.fromFormProperty("enum_form_string", (Schema) requestBodySchema.getProperties().get("enum_form_string"), new HashSet<String>());
Assert.assertEquals(codegenParameter.defaultValue, "-efg");
}
@Test
public void testEnsureNoDuplicateProduces() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/two-responses.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/test").getGet();
CodegenOperation co = codegen.fromOperation("/test", "get", operation, ModelUtils.getSchemas(openAPI), openAPI);
Assert.assertEquals(co.produces.size(), 1);
Assert.assertEquals(co.produces.get(0).get("mediaType"), "application/json");
}
@Test
public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/composed-oneof.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/state").getPost();
Schema schema = ModelUtils.getSchemaFromRequestBody(operation.getRequestBody());
String type = codegen.getSchemaType(schema);
Assert.assertNotNull(type);
}
@Test
public void updateCodegenPropertyEnum() {
final DefaultCodegen codegen = new DefaultCodegen();
CodegenProperty array = codegenPropertyWithArrayOfIntegerValues();
codegen.updateCodegenPropertyEnum(array);
List<Map<String, Object>> enumVars = (List<Map<String, Object>>) array.getItems().getAllowableValues().get("enumVars");
Assert.assertNotNull(enumVars);
Map<String, Object> testedEnumVar = enumVars.get(0);
Assert.assertNotNull(testedEnumVar);
Assert.assertEquals(testedEnumVar.getOrDefault("name", ""),"_1");
Assert.assertEquals(testedEnumVar.getOrDefault("value", ""), "\"1\"");
Assert.assertEquals(testedEnumVar.getOrDefault("isString", ""), false);
}
@Test
public void testExample1() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/example1/singular").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));
Assert.assertEquals(codegenParameter.example, "example1 value");
Operation operation2 = openAPI.getPaths().get("/example1/plural").getGet();
CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0));
Assert.assertEquals(codegenParameter2.example, "An example1 value");
}
@Test
public void testExample2() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/example2/singular").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));
Assert.assertEquals(codegenParameter.example, "example2 value");
}
@Test
public void testExample3() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/example3/singular").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));
Assert.assertEquals(codegenParameter.example, "example3: parameter value");
Operation operation2 = openAPI.getPaths().get("/example3/plural").getGet();
CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter2, operation2.getParameters().get(0));
Assert.assertEquals(codegenParameter2.example, "example3: parameter value");
}
@Test
public void testExample4() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/examples.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/example4/singular").getPost();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getRequestBody());
Assert.assertEquals(codegenParameter.example, "example4 value");
Operation operation2 = openAPI.getPaths().get("/example4/plural").getPost();
CodegenParameter codegenParameter2 = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter2, operation2.getRequestBody());
Assert.assertEquals(codegenParameter2.example, "An example4 value");
}
private CodegenProperty codegenPropertyWithArrayOfIntegerValues() {
CodegenProperty array = new CodegenProperty();
final CodegenProperty items = new CodegenProperty();
final HashMap<String, Object> allowableValues = new HashMap<>();
allowableValues.put("values", Collections.singletonList(1));
items.setAllowableValues(allowableValues);
items.dataType = "Integer";
array.setItems(items);
array.dataType = "Array";
return array;
}
}