Skip to content

Commit 989049b

Browse files
committed
Interface with custom discriminator test
1 parent 9c2eff9 commit 989049b

File tree

3 files changed

+183
-5
lines changed

3 files changed

+183
-5
lines changed

src/test/java/io/swagger/codegen/v3/generators/GeneratorRunner.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.io.InputStream;
1414
import java.nio.file.Files;
1515
import java.util.List;
16+
import java.util.function.Consumer;
17+
import java.util.function.Supplier;
1618

1719
/**
1820
*
@@ -28,14 +30,22 @@ public static List<File> runGenerator(
2830
boolean v2Spec,
2931
boolean yaml,
3032
boolean flattenInlineComposedSchema,
31-
String outFolder
33+
String outFolder,
34+
Consumer<Options> optionsCustomizer
3235
) throws Exception {
3336

3437
String path = outFolder;
3538
if (StringUtils.isBlank(path)) {
3639
path = getTmpFolder().getAbsolutePath();
3740
}
3841
GenerationRequest request = new GenerationRequest();
42+
43+
Options option = new Options()
44+
.flattenInlineComposedSchema(flattenInlineComposedSchema)
45+
.outputDir(path);
46+
47+
optionsCustomizer.accept(option);
48+
3949
request
4050
.codegenVersion(codegenVersion) // use V2 to target Swagger/OpenAPI 2.x Codegen version
4151
.type(GenerationRequest.Type.CLIENT)
@@ -44,9 +54,7 @@ public static List<File> runGenerator(
4454
yaml, // YAML file, use false for JSON
4555
v2Spec)) // OpenAPI 3.x - use true for Swagger/OpenAPI 2.x definitions
4656
.options(
47-
new Options()
48-
.flattenInlineComposedSchema(flattenInlineComposedSchema)
49-
.outputDir(path)
57+
option
5058
);
5159

5260
List<File> files = new GeneratorService().generationRequest(request).generate();

src/test/java/io/swagger/codegen/v3/generators/java/GeneratorResultTestJava.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import org.testng.annotations.Test;
77

88
import java.io.File;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
911
import java.util.List;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
1014

1115
public class GeneratorResultTestJava {
1216

@@ -29,11 +33,59 @@ public void testJavaGenerator_OneOf() throws Exception {
2933
v2Spec,
3034
yaml,
3135
flattenInlineComposedSchema,
32-
outFolder);
36+
outFolder, options -> {});
3337

3438
Assert.assertFalse(files.isEmpty());
3539
for (File f: files) {
3640
// TODO test stuff
3741
}
3842
}
43+
44+
@Test
45+
public void interfaceWithCustomDiscriminator() throws Exception {
46+
47+
String name = "java";
48+
String specPath = "3_0_0/sample_interface_with_discriminator.json";
49+
GenerationRequest.CodegenVersion codegenVersion = GenerationRequest.CodegenVersion.V3;
50+
boolean v2Spec = false; // 3.0 spec
51+
boolean yaml = false;
52+
boolean flattenInlineComposedSchema = true;
53+
String outFolder = null; // temporary folder
54+
55+
56+
List<File> files = GeneratorRunner.runGenerator(
57+
name,
58+
specPath,
59+
codegenVersion,
60+
v2Spec,
61+
yaml,
62+
flattenInlineComposedSchema,
63+
outFolder,
64+
options -> options.setLibrary("resttemplate"));
65+
66+
67+
68+
File interfaceFile = files.stream().filter(f -> f.getName().equals("Item.java")).findAny().orElseThrow(() -> new RuntimeException("No interface generated"));
69+
70+
String interfaceContent = new String(Files.readAllBytes(Paths.get(interfaceFile.toURI())));
71+
72+
Pattern typeInfoPattern = Pattern.compile( "(.*)(@JsonTypeInfo\\()(.*)(}\\))(.*)", Pattern.DOTALL);
73+
74+
Matcher matcher = typeInfoPattern.matcher(interfaceContent);
75+
76+
Assert.assertTrue(matcher.matches(), "No JsonTypeInfo generated into the interface file");
77+
78+
String generatedTypeinfoLines = matcher.group(2)+matcher.group(3)+matcher.group(4);
79+
80+
Assert.assertEquals( generatedTypeinfoLines,"@JsonTypeInfo(\n" +
81+
" use = JsonTypeInfo.Id.NAME,\n" +
82+
" include = JsonTypeInfo.As.PROPERTY,\n" +
83+
" property = \"aCustomProperty\")\n" +
84+
"@JsonSubTypes({\n" +
85+
" @JsonSubTypes.Type(value = ClassA.class, name = \"typeA\"),\n" +
86+
" @JsonSubTypes.Type(value = ClassB.class, name = \"typeB\"),\n" +
87+
" @JsonSubTypes.Type(value = ClassC.class, name = \"typeC\")\n" +
88+
"})", "Wrong json subtypes generated");
89+
90+
}
3991
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "Title",
5+
"description": "Title",
6+
"version": "1.0.0"
7+
},
8+
"paths": {
9+
"/sampleObjectResponse": {
10+
"get": {
11+
"tags": [
12+
"Sample"
13+
],
14+
"responses": {
15+
"200": {
16+
"description": "Returns requested data",
17+
"content": {
18+
"application/json": {
19+
"schema": {
20+
"$ref": "#/components/schemas/sampleResponse"
21+
}
22+
}
23+
}
24+
}
25+
26+
}
27+
}
28+
29+
}
30+
},
31+
"components": {
32+
"schemas": {
33+
"sampleResponse": {
34+
"type": "array",
35+
"items": {
36+
"$ref": "#/components/schemas/item"
37+
}
38+
},
39+
"item": {
40+
"discriminator": {
41+
"propertyName": "aCustomProperty",
42+
"mapping": {
43+
"typeA": "#/components/schemas/classA",
44+
"typeB": "#/components/schemas/classB",
45+
"typeC": "#/components/schemas/classC"
46+
}
47+
},
48+
"oneOf": [
49+
{
50+
"$ref": "#/components/schemas/classA"
51+
},
52+
{
53+
"$ref": "#/components/schemas/classB"
54+
},
55+
{
56+
"$ref": "#/components/schemas/classC"
57+
}
58+
]
59+
},
60+
"classA": {
61+
"type": "object",
62+
"properties": {
63+
"aaa": {
64+
"type": "string"
65+
}
66+
},
67+
"allOf": [
68+
{
69+
"$ref": "#/components/schemas/baseClass"
70+
}
71+
]
72+
},
73+
"classB": {
74+
"type": "object",
75+
"properties": {
76+
"bbb": {
77+
"type": "string"
78+
}
79+
},
80+
"allOf": [
81+
{
82+
"$ref": "#/components/schemas/baseClass"
83+
}
84+
]
85+
},
86+
"classC": {
87+
"type": "object",
88+
"properties": {
89+
"ccc": {
90+
"type": "string"
91+
}
92+
},
93+
"allOf": [
94+
{
95+
"$ref": "#/components/schemas/baseClass"
96+
}
97+
]
98+
},
99+
"baseClass": {
100+
101+
"properties": {
102+
"id": {
103+
"type": "integer"
104+
},
105+
"name": {
106+
"type": "string"
107+
}
108+
}
109+
}
110+
}
111+
},
112+
"tags": [
113+
{
114+
"name": "Sample",
115+
"description": "Sample"
116+
}
117+
]
118+
}

0 commit comments

Comments
 (0)