-
Notifications
You must be signed in to change notification settings - Fork 6k
/
CodegenTest.java
512 lines (429 loc) · 25.3 KB
/
CodegenTest.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package io.swagger.codegen;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.parser.SwaggerParser;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
public class CodegenTest {
@Test(description = "handle simple composition")
public void propertiesInComposedModelTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfProperties.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("RedisResource");
CodegenModel composed = codegen.fromModel("RedisResource", model, swagger.getDefinitions());
Assert.assertEquals(composed.vars.size(), 3);
Assert.assertEquals(composed.vars.get(0).baseName, "modelOneProp");
Assert.assertEquals(composed.vars.get(1).baseName, "properties");
Assert.assertEquals(composed.vars.get(2).baseName, "zones");
Assert.assertNotNull(composed.parent);
}
@Test(description = "test sanitizeTag")
public void sanitizeTagTest() {
final DefaultCodegen codegen = new DefaultCodegen();
Assert.assertEquals(codegen.sanitizeTag("foo"), "Foo");
Assert.assertEquals(codegen.sanitizeTag("$foo!"), "Foo");
Assert.assertEquals(codegen.sanitizeTag("foo bar"), "FooBar");
Assert.assertEquals(codegen.sanitizeTag("foo_bar"), "FooBar");
Assert.assertEquals(codegen.sanitizeTag("foo1 bar2"), "Foo1Bar2");
Assert.assertEquals(codegen.sanitizeTag("foo bar 1"), "FooBar1");
Assert.assertEquals(codegen.sanitizeTag("1foo"), "Class1foo");
}
@Test(description = "test underscore")
public void underscoreNamesTest() {
final DefaultCodegen codegen = new DefaultCodegen();
Assert.assertEquals(codegen.underscore("foo"), "foo");
Assert.assertEquals(codegen.underscore("foo-bar"), "foo_bar");
Assert.assertEquals(codegen.underscore("foo bar"), "foo_bar");
Assert.assertEquals(codegen.underscore("FooBar"), "foo_bar");
Assert.assertEquals(codegen.underscore("FooBarBaz"), "foo_bar_baz");
}
@Test(description = "test camelize")
public void camelizeNamesTest() {
final DefaultCodegen codegen = new DefaultCodegen();
Assert.assertEquals(codegen.camelize("foo"), "Foo");
Assert.assertEquals(codegen.camelize(".foo"), "Foo");
Assert.assertEquals(codegen.camelize(".foo.bar"), "FooBar");
Assert.assertEquals(codegen.camelize("foo$bar"), "Foo$bar");
Assert.assertEquals(codegen.camelize("foo_$bar"), "Foo$bar");
Assert.assertEquals(codegen.camelize("foo_bar"), "FooBar");
Assert.assertEquals(codegen.camelize("foo_bar_baz"), "FooBarBaz");
Assert.assertEquals(codegen.camelize("foo/bar.baz"), "FooBarBaz");
Assert.assertEquals(codegen.camelize("/foo/bar/baz.qux/corge"), "FooBarBazQuxCorge");
Assert.assertEquals(codegen.camelize("foo-bar"), "FooBar");
Assert.assertEquals(codegen.camelize("foo-bar-xyzzy"), "FooBarXyzzy");
}
@Test(description = "read a file upload param from a 2.0 spec")
public void fileUploadParamTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/pet/{petId}/uploadImage";
final Operation p = model.getPaths().get(path).getPost();
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions());
Assert.assertEquals(op.operationId, "uploadFile");
Assert.assertEquals(op.httpMethod, "POST");
Assert.assertTrue(op.hasConsumes);
Assert.assertEquals(op.consumes.size(), 1);
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "multipart/form-data");
Assert.assertTrue(op.hasProduces);
Assert.assertEquals(op.allParams.size(), 3);
Assert.assertEquals(op.formParams.size(), 2);
final CodegenParameter file = op.formParams.get(1);
Assert.assertTrue(file.isFormParam);
Assert.assertEquals(file.dataType, "File");
Assert.assertFalse(file.required);
Assert.assertTrue(file.isFile);
Assert.assertFalse(file.hasMore);
}
@Test(description = "read formParam values from a 2.0 spec")
public void formParamTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/pet/{petId}";
final Operation p = model.getPaths().get(path).getPost();
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions());
Assert.assertEquals(op.operationId, "updatePetWithForm");
Assert.assertEquals(op.httpMethod, "POST");
Assert.assertTrue(op.hasConsumes);
Assert.assertEquals(op.consumes.size(), 1);
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/x-www-form-urlencoded");
Assert.assertTrue(op.hasProduces);
Assert.assertEquals(op.produces.size(), 2);
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/json");
Assert.assertEquals(op.produces.get(0).get("hasMore"), "true");
Assert.assertEquals(op.produces.get(1).get("mediaType"), "application/xml");
Assert.assertEquals(op.pathParams.size(), 1);
final CodegenParameter idParam = op.pathParams.get(0);
Assert.assertTrue(idParam.isPathParam);
Assert.assertEquals(idParam.dataType, "String");
Assert.assertTrue(idParam.required);
Assert.assertFalse(idParam.hasMore);
Assert.assertEquals(op.allParams.size(), 3);
Assert.assertEquals(op.formParams.size(), 2);
final CodegenParameter nameParam = op.formParams.get(0);
Assert.assertTrue(nameParam.isFormParam);
Assert.assertTrue(nameParam.notFile);
Assert.assertEquals(nameParam.dataType, "String");
Assert.assertFalse(nameParam.required);
Assert.assertTrue(nameParam.hasMore);
final CodegenParameter statusParam = op.formParams.get(1);
Assert.assertTrue(statusParam.isFormParam);
Assert.assertTrue(statusParam.notFile);
Assert.assertEquals(statusParam.dataType, "String");
Assert.assertFalse(statusParam.required);
Assert.assertFalse(statusParam.hasMore);
}
@Test(description = "handle enum array in query parameter test")
public void enumArrayQueryParameterTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/pet/findByStatus";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertEquals(op.queryParams.size(), 1);
final CodegenParameter statusParam = op.queryParams.get(0);
Assert.assertEquals(statusParam.items.datatypeWithEnum, "StatusEnum");
Assert.assertNotNull(statusParam.items);
Assert.assertTrue(statusParam.items.isEnum);
Assert.assertEquals(statusParam.items._enum.size(), 3);
}
@Test(description = "handle enum in query parameter test")
public void enumQueryParameterTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/pet/findByStatus";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertEquals(op.queryParams.size(), 1);
final CodegenParameter statusParam = op.queryParams.get(0);
Assert.assertEquals(statusParam.datatypeWithEnum, "List");
Assert.assertEquals(statusParam.baseType, "String");
Assert.assertTrue(statusParam.isEnum);
Assert.assertEquals(((List)statusParam.allowableValues.get("values")).size(), 3);
}
@Test(description = "handle required parameters from a 2.0 spec as required when figuring out Swagger types")
public void requiredParametersTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/requiredTest.json");
final DefaultCodegen codegen = new DefaultCodegen() {
@Override
public String getSwaggerType(Property p) {
if (p != null && !p.getRequired()) {
return "Optional<" + super.getSwaggerType(p) + ">";
}
return super.getSwaggerType(p);
}
};
final String path = "/tests/requiredParams";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
final List<CodegenParameter> formParams = op.formParams;
Assert.assertEquals(formParams.size(), 2);
Assert.assertEquals(formParams.get(0).dataType, "Long");
Assert.assertEquals(formParams.get(1).dataType, "Optional<string>");
Assert.assertEquals(op.returnType, "Long");
}
@Test(description = "select main response from a 2.0 spec using the lowest 2XX code")
public void responseSelectionTest1() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/responseSelectionTest.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/withTwoHundredAndDefault";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertEquals(op.returnType, "String");
}
@Test(description = "select main response from a 2.0 spec using the default keyword when no 2XX code")
public void responseSelectionTest2() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/responseSelectionTest.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/withoutTwoHundredButDefault";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertEquals(op.returnType, "String");
}
@Test(description = "return byte array when response format is byte")
public void binaryDataTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/binaryDataTest.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/binaryResponse";
final Operation p = model.getPaths().get(path).getPost();
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions());
Assert.assertEquals(op.returnType, "byte[]");
Assert.assertEquals(op.bodyParam.dataType, "byte[]");
Assert.assertTrue(op.bodyParam.isBinary);
Assert.assertTrue(op.responses.get(0).isBinary);
}
@Test(description = "return file when response format is file")
public void fileResponeseTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/fileResponseTest.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/fileResponse";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertEquals(op.returnType, "File");
Assert.assertTrue(op.responses.get(0).isFile);
Assert.assertTrue(op.isResponseFile);
}
@Test(description = "discriminator is present")
public void discriminatorTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/discriminatorTest.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/pets";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertEquals(op.discriminator, "className");
}
@Test(description = "handle simple composition")
public void simpleCompositionTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("SimpleComposition");
CodegenModel composed = codegen.fromModel("SimpleComposition", model, swagger.getDefinitions());
Assert.assertEquals(composed.vars.size(), 2);
Assert.assertEquals(composed.vars.get(0).baseName, "modelTwoProp");
Assert.assertEquals(composed.vars.get(1).baseName, "simpleCompositionProp");
Assert.assertNotNull(composed.parent);
}
@Test(description = "handle multi level composition")
public void multiCompositionTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("CompositionOfSimpleComposition");
CodegenModel composed = codegen.fromModel("CompositionOfSimpleComposition", model, swagger.getDefinitions());
Assert.assertEquals(composed.vars.size(), 2);
Assert.assertEquals(composed.vars.get(0).baseName, "modelThreeProp");
Assert.assertEquals(composed.vars.get(1).baseName, "compositionOfSimpleCompositionProp");
Assert.assertNotNull(composed.parent);
}
@Test(description = "handle simple inheritance")
public void simpleInheritanceTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("ChildOfSimpleParent");
CodegenModel child = codegen.fromModel("ChildOfSimpleParent", model, swagger.getDefinitions());
Assert.assertEquals(child.vars.size(), 3);
Assert.assertEquals(child.vars.get(0).baseName, "disc");
Assert.assertEquals(child.vars.get(1).baseName, "simpleParentProp");
Assert.assertEquals(child.vars.get(2).baseName, "childOfSimpleParentProp");
Assert.assertEquals(child.parent, "ModelOne");
}
@Test(description = "handle multi level inheritance")
public void multiInheritanceTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("ChildOfChildOfSimpleParent");
CodegenModel child = codegen.fromModel("ChildOfChildOfSimpleParent", model, swagger.getDefinitions());
Assert.assertEquals(child.vars.size(), 1);
Assert.assertEquals(child.vars.get(0).baseName, "childOfChildOfSimpleParentProp");
Assert.assertEquals(child.parent, "ChildOfSimpleParent");
}
@Test(description = "copy properties in multi level inheritance if supportsInheritance is false")
public void noSupportsInheritanceTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
final Model model = swagger.getDefinitions().get("ChildOfChildOfSimpleParent");
CodegenModel child = codegen.fromModel("ChildOfChildOfSimpleParent", model, swagger.getDefinitions());
Assert.assertEquals(child.vars.size(), 5);
Assert.assertEquals(child.vars.get(0).baseName, "modelOneProp");
Assert.assertEquals(child.vars.get(1).baseName, "disc");
Assert.assertEquals(child.vars.get(2).baseName, "simpleParentProp");
Assert.assertEquals(child.vars.get(3).baseName, "childOfSimpleParentProp");
Assert.assertEquals(child.vars.get(4).baseName, "childOfChildOfSimpleParentProp");
Assert.assertEquals(child.parent, "ChildOfSimpleParent");
}
@Test(description = "don't copy interfaces properties if supportsMixins is true")
public void supportsMixinsTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
codegen.supportsMixins = true;
final Model model = swagger.getDefinitions().get("ChildOfChildOfSimpleParent");
CodegenModel child = codegen.fromModel("ChildOfChildOfSimpleParent", model, swagger.getDefinitions());
Assert.assertEquals(child.vars.size(), 1);
Assert.assertEquals(child.vars.get(0).baseName, "childOfChildOfSimpleParentProp");
Assert.assertEquals(child.allVars.size(), 5);
Assert.assertEquals(child.allVars.get(0).baseName, "modelOneProp");
Assert.assertEquals(child.allVars.get(1).baseName, "disc");
Assert.assertEquals(child.allVars.get(2).baseName, "simpleParentProp");
Assert.assertEquals(child.allVars.get(3).baseName, "childOfSimpleParentProp");
Assert.assertEquals(child.allVars.get(4).baseName, "childOfChildOfSimpleParentProp");
Assert.assertEquals(child.parent, "ChildOfSimpleParent");
}
@Test(description = "handle inheritance from composed model")
public void inheritanceOfComposedModelTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("ChildOfComposedParent");
CodegenModel child = codegen.fromModel("ChildOfComposedParent", model, swagger.getDefinitions());
Assert.assertEquals(child.vars.size(), 1);
Assert.assertEquals(child.vars.get(0).baseName, "childOfComposedParentProp");
Assert.assertEquals(child.parent, "ComposedParent");
}
@Test(description = "handle multi level inheritance from composed model")
public void multiInheritanceOfComposedModelTest() {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfTest.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
final Model model = swagger.getDefinitions().get("ChildOfChildOfComposedParent");
CodegenModel child = codegen.fromModel("ChildOfChildOfComposedParent", model, swagger.getDefinitions());
Assert.assertEquals(child.vars.size(), 1);
Assert.assertEquals(child.vars.get(0).baseName, "childOfChildOfComposedParentProp");
Assert.assertEquals(child.parent, "ChildOfComposedParent");
}
@Test(description = "use relative $ref for definitions of parameters")
public void relativeDefinitionsInParameterTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/photo/getPhotos";
final Operation p = model.getPaths().get(path).getPost();
CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions(), model);
Assert.assertNotNull(op);
Assert.assertNotNull(op.imports);
Assert.assertTrue(op.imports.contains("Photo"));
Assert.assertTrue(op.imports.contains("PhotosRequest"));
}
@Test(description = "use relative $ref for definitions of response")
public void relativeDefinitionsInResponseTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/photo/{id}";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertNotNull(op);
Assert.assertNotNull(op.imports);
Assert.assertTrue(op.imports.contains("Photo"));
Assert.assertTrue(op.imports.contains("integer"));
}
@Test(description = "use relative $ref for definitions of response")
public void relativeDefinitionsMapInResponseTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/relative-ref/nested/directory/main/relative-refs.yml");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/photo/thumbnails";
final Operation p = model.getPaths().get(path).getPost();
CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions(), model);
Assert.assertNotNull(op);
Assert.assertNotNull(op.imports);
Assert.assertTrue(op.imports.contains("Photo"));
Assert.assertTrue(op.imports.contains("PhotoThumbnailsRequest"));
}
@Test(description = "use operation consumes and produces")
public void localConsumesAndProducesTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/globalConsumesAndProduces.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/localConsumesAndProduces";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertTrue(op.hasConsumes);
Assert.assertEquals(op.consumes.size(), 1);
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/json");
Assert.assertTrue(op.hasProduces);
Assert.assertEquals(op.produces.size(), 1);
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/json");
}
@Test(description = "use spec consumes and produces")
public void globalConsumesAndProducesTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/globalConsumesAndProduces.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/globalConsumesAndProduces";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertTrue(op.hasConsumes);
Assert.assertEquals(op.consumes.size(), 1);
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/global_consumes");
Assert.assertTrue(op.hasProduces);
Assert.assertEquals(op.produces.size(), 1);
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/global_produces");
}
@Test(description = "use operation consumes and produces (reset in operation with empty array)")
public void localResetConsumesAndProducesTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/globalConsumesAndProduces.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/localResetConsumesAndProduces";
final Operation p = model.getPaths().get(path).getGet();
CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions(), model);
Assert.assertNotNull(op);
Assert.assertFalse(op.hasConsumes);
Assert.assertNull(op.consumes);
Assert.assertFalse(op.hasProduces);
Assert.assertNull(op.produces);
}
private static Swagger parseAndPrepareSwagger(String path) {
Swagger swagger = new SwaggerParser().read(path);
// resolve inline models
new InlineModelResolver().flatten(swagger);
return swagger;
}
@Test(description = "isDeprecated is present")
public void deprecatedParamTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/pet/findByTags";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
Assert.assertTrue(op.isDeprecated);
}
@Test(description = "https://github.com/swagger-api/swagger-codegen/issues/7980")
public void testPattern() throws Exception {
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/petstore.yaml");
ModelImpl currency = (ModelImpl) swagger.getDefinitions().get("Currency");
Assert.assertNotNull(currency);
Assert.assertEquals(currency.getPattern(), "^[A-Z]{3,3}$");
ModelImpl amount = (ModelImpl) swagger.getDefinitions().get("Amount");
final DefaultCodegen codegen = new DefaultCodegen();
final CodegenModel codegenModel = codegen.fromModel("Amount", amount, swagger.getDefinitions());
for (CodegenProperty codegenProperty : codegenModel.vars) {
if ("currency".equalsIgnoreCase(codegenProperty.name)) {
Assert.assertEquals(codegenProperty.pattern, "/^[A-Z]{3,3}$/");
break;
}
}
}
}