Skip to content

Commit 1ada2a5

Browse files
authored
Merge pull request #677 from swagger-api/test-result
add generator result test utils - add Java generator result test draft for composed schemas
2 parents ce63066 + 01207f4 commit 1ada2a5

File tree

8 files changed

+425
-0
lines changed

8 files changed

+425
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package io.swagger.codegen.v3.generators;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import io.swagger.codegen.v3.service.GenerationRequest;
5+
import io.swagger.codegen.v3.service.GeneratorService;
6+
import io.swagger.codegen.v3.service.Options;
7+
import io.swagger.util.Json;
8+
import io.swagger.util.Yaml;
9+
import org.apache.commons.io.IOUtils;
10+
import org.apache.commons.lang3.StringUtils;
11+
12+
import java.io.File;
13+
import java.io.InputStream;
14+
import java.util.List;
15+
16+
/**
17+
*
18+
* helper methods to test specific generator result (set of files)
19+
*
20+
*/
21+
public abstract class GeneratorRunner {
22+
23+
public static List<File> runGenerator(
24+
String name,
25+
String specPath,
26+
GenerationRequest.CodegenVersion codegenVersion,
27+
boolean v2Spec,
28+
boolean yaml,
29+
boolean flattenInlineComposedSchema,
30+
String outFolder
31+
) throws Exception {
32+
33+
String path = outFolder;
34+
if (StringUtils.isBlank(path)) {
35+
path = getTmpFolder().getAbsolutePath();
36+
}
37+
GenerationRequest request = new GenerationRequest();
38+
request
39+
.codegenVersion(codegenVersion) // use V2 to target Swagger/OpenAPI 2.x Codegen version
40+
.type(GenerationRequest.Type.CLIENT)
41+
.lang(name)
42+
.spec(loadSpecAsNode( specPath,
43+
yaml, // YAML file, use false for JSON
44+
v2Spec)) // OpenAPI 3.x - use true for Swagger/OpenAPI 2.x definitions
45+
.options(
46+
new Options()
47+
.flattenInlineComposedSchema(flattenInlineComposedSchema)
48+
.outputDir(path)
49+
);
50+
51+
List<File> files = new GeneratorService().generationRequest(request).generate();
52+
return files;
53+
}
54+
55+
public static File getOutFolder(String path, boolean delete) {
56+
try {
57+
File outputFolder = new File(path);
58+
if (delete) {
59+
// TODO delete..
60+
}
61+
return outputFolder;
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
return null;
65+
}
66+
}
67+
68+
public static File getTmpFolder() {
69+
try {
70+
File outputFolder = File.createTempFile("codegentest-", "-tmp");
71+
outputFolder.delete();
72+
outputFolder.mkdir();
73+
outputFolder.deleteOnExit();
74+
return outputFolder;
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
return null;
78+
}
79+
}
80+
81+
public static JsonNode loadSpecAsNode(final String file, boolean yaml, boolean v2) {
82+
InputStream in = null;
83+
84+
try {
85+
in = GeneratorRunner.class.getClassLoader().getResourceAsStream(file);
86+
if (yaml) {
87+
if (v2) {
88+
return Yaml.mapper().readTree(in);
89+
} else {
90+
return io.swagger.v3.core.util.Yaml.mapper().readTree(in);
91+
}
92+
}
93+
if (v2) {
94+
return Json.mapper().readTree(in);
95+
}
96+
return io.swagger.v3.core.util.Json.mapper().readTree(in);
97+
} catch (Exception e) {
98+
throw new RuntimeException("could not load file " + file);
99+
} finally {
100+
IOUtils.closeQuietly(in);
101+
}
102+
}
103+
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.swagger.codegen.v3.generators.java;
2+
3+
import io.swagger.codegen.v3.generators.GeneratorRunner;
4+
import io.swagger.codegen.v3.service.GenerationRequest;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.File;
9+
import java.util.List;
10+
11+
public class GeneratorResultTestJava {
12+
13+
14+
@Test
15+
public void testJavaGenerator_OneOf() throws Exception {
16+
17+
String name = "java";
18+
String specPath = "3_0_0/composedFlatten/allof_inline_ref.yaml";
19+
GenerationRequest.CodegenVersion codegenVersion = GenerationRequest.CodegenVersion.V3;
20+
boolean v2Spec = false; // 3.0 spec
21+
boolean yaml = true;
22+
boolean flattenInlineComposedSchema = true;
23+
String outFolder = null; // temporary folder
24+
25+
List<File> files = GeneratorRunner.runGenerator(
26+
name,
27+
specPath,
28+
codegenVersion,
29+
v2Spec,
30+
yaml,
31+
flattenInlineComposedSchema,
32+
outFolder);
33+
34+
Assert.assertFalse(files.isEmpty());
35+
for (File f: files) {
36+
// TODO test stuff
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.2
2+
info:
3+
title: CC-20717 test - OAS3
4+
version: 1.0.0
5+
paths:
6+
/something:
7+
get:
8+
responses:
9+
200:
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Bar'
15+
components:
16+
schemas:
17+
Foo:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
Bar:
23+
type: object
24+
properties:
25+
foo1:
26+
description: An instance of Foo
27+
allOf:
28+
- $ref: '#/components/schemas/Foo'
29+
foo2:
30+
$ref: '#/components/schemas/Foo'
31+
OneOfTest:
32+
oneOf:
33+
- $ref: "#/components/schemas/Foo"
34+
- type: object
35+
properties:
36+
oneOfInlineBar:
37+
type: string
38+
SchemaTest:
39+
allOf:
40+
- type: object
41+
properties:
42+
schemaTestInline1Foo:
43+
type: string
44+
schemaTestInline1Ref:
45+
type: array
46+
items:
47+
$ref: '#/components/schemas/Bar'
48+
- $ref: '#/components/schemas/OneOfTest'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.2
2+
info:
3+
title: CC-20717 test - OAS3
4+
version: 1.0.0
5+
paths:
6+
/something:
7+
get:
8+
responses:
9+
200:
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Bar'
15+
components:
16+
schemas:
17+
Foo:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
Bar:
23+
type: object
24+
properties:
25+
foo1:
26+
description: An instance of Foo
27+
allOf:
28+
- $ref: '#/components/schemas/Foo'
29+
foo2:
30+
$ref: '#/components/schemas/Foo'
31+
OneOfTest:
32+
oneOf:
33+
- $ref: "#/components/schemas/Foo"
34+
- type: object
35+
properties:
36+
oneOfInlineBar:
37+
type: string
38+
SchemaTest:
39+
allOf:
40+
- type: object
41+
properties:
42+
schemaTestInline1Foo:
43+
type: string
44+
schemaTestInline1Ref:
45+
type: array
46+
items:
47+
$ref: '#/components/schemas/Bar'
48+
- $ref: '#/components/schemas/Foo'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.2
2+
info:
3+
title: CC-20717 test - OAS3
4+
version: 1.0.0
5+
paths:
6+
/something:
7+
get:
8+
responses:
9+
200:
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Bar'
15+
components:
16+
schemas:
17+
Foo:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
Bar:
23+
type: object
24+
properties:
25+
foo1:
26+
description: An instance of Foo
27+
allOf:
28+
- $ref: '#/components/schemas/Foo'
29+
foo2:
30+
$ref: '#/components/schemas/Foo'
31+
OneOfTest:
32+
oneOf:
33+
- $ref: "#/components/schemas/Foo"
34+
- type: object
35+
properties:
36+
foo:
37+
type: string
38+
SchemaTest:
39+
allOf:
40+
- $ref: '#/components/schemas/OneOfTest'
41+
- type: object
42+
properties:
43+
schemaTestInline1Foo:
44+
type: string
45+
schemaTestInline1Ref:
46+
type: array
47+
items:
48+
$ref: '#/components/schemas/Bar'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
openapi: 3.0.2
2+
info:
3+
title: CC-20717 test - OAS3
4+
version: 1.0.0
5+
paths:
6+
/something:
7+
get:
8+
responses:
9+
200:
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Bar'
15+
components:
16+
schemas:
17+
Foo:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
Bar:
23+
type: object
24+
properties:
25+
foo1:
26+
description: An instance of Foo
27+
allOf:
28+
- $ref: '#/components/schemas/Foo'
29+
foo2:
30+
$ref: '#/components/schemas/Foo'
31+
OneOfTest:
32+
oneOf:
33+
- $ref: "#/components/schemas/Foo"
34+
- type: object
35+
properties:
36+
foo:
37+
type: string
38+
SchemaTest:
39+
allOf:
40+
- $ref: '#/components/schemas/OneOfTest'
41+
- type: object
42+
properties:
43+
schemaTestInline1Foo:
44+
type: string
45+
schemaTestInline1Ref:
46+
type: array
47+
items:
48+
$ref: '#/components/schemas/Bar'
49+
- type: object
50+
properties:
51+
schemaTestInline2Foo:
52+
type: string
53+
schemaTestInline2Ref:
54+
type: array
55+
items:
56+
$ref: '#/components/schemas/Bar'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
openapi: 3.0.2
2+
info:
3+
title: CC-20717 test - OAS3
4+
version: 1.0.0
5+
paths:
6+
/something:
7+
get:
8+
responses:
9+
200:
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Bar'
15+
components:
16+
schemas:
17+
Foo:
18+
type: object
19+
properties:
20+
foo:
21+
type: string
22+
Bar:
23+
type: object
24+
properties:
25+
foo1:
26+
description: An instance of Foo
27+
allOf:
28+
- $ref: '#/components/schemas/Foo'
29+
foo2:
30+
$ref: '#/components/schemas/Foo'
31+
OneOfTest:
32+
oneOf:
33+
- $ref: "#/components/schemas/Foo"
34+
- type: object
35+
properties:
36+
foo:
37+
type: string
38+
SchemaTest:
39+
allOf:
40+
- $ref: '#/components/schemas/OneOfTest'
41+
- $ref: '#/components/schemas/Bar'

0 commit comments

Comments
 (0)