Skip to content

Commit 4352a2f

Browse files
[Test] [Java] Add test example of how to handle x-parent without REF_AS_PARENT_IN_ALLOF normalization (#22058)
* Add test to illustrate example of how to handle x-parent without REF_AS_PARENT_IN_ALLOF normalization * Minor change to retrigger build
1 parent 6f3daca commit 4352a2f

File tree

3 files changed

+86
-15
lines changed

3 files changed

+86
-15
lines changed

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testOpenAPINormalizerRefAsParentInAllOf() {
6666
assertEquals(schema5.getExtensions().get(X_PARENT), "abstract");
6767

6868
// Verify that all allOf refs gets marked as parents
69-
Schema<?>schemaWithTwoParents = openAPI.getComponents().getSchemas().get("SchemaWithTwoParents");
69+
Schema<?>schemaWithTwoParents = openAPI.getComponents().getSchemas().get("SchemaWithTwoAllOfRefs");
7070
assertNull(schemaWithTwoParents.getExtensions());
7171
Schema<?>personA = openAPI.getComponents().getSchemas().get("PersonA");
7272
assertEquals(personA.getExtensions().get(X_PARENT), true);
@@ -79,10 +79,10 @@ public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithPropertie
7979
// to test the both REF_AS_PARENT_IN_ALLOF and REFACTOR_ALLOF_WITH_PROPERTIES_ONLY
8080
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_extension_parent.yaml");
8181

82-
Schema schema = openAPI.getComponents().getSchemas().get("Child");
82+
Schema<?> schema = openAPI.getComponents().getSchemas().get("Child");
8383
assertNull(schema.getExtensions());
8484

85-
Schema schema2 = openAPI.getComponents().getSchemas().get("Ancestor");
85+
Schema<?> schema2 = openAPI.getComponents().getSchemas().get("Ancestor");
8686
assertNull(schema2.getExtensions());
8787

8888
Map<String, String> options = new HashMap<>();
@@ -91,10 +91,10 @@ public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithPropertie
9191
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
9292
openAPINormalizer.normalize();
9393

94-
Schema schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
94+
Schema<?> schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
9595
assertEquals(schema3.getExtensions().get(X_PARENT), true);
9696

97-
Schema schema4 = openAPI.getComponents().getSchemas().get("Child");
97+
Schema<?> schema4 = openAPI.getComponents().getSchemas().get("Child");
9898
assertNull(schema4.getExtensions());
9999
}
100100

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.github.javaparser.ast.body.FieldDeclaration;
2323
import com.github.javaparser.ast.expr.Expression;
2424
import com.github.javaparser.ast.expr.MethodCallExpr;
25-
import com.github.javaparser.ast.visitor.*;
25+
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
2626
import com.google.common.collect.ImmutableMap;
2727
import io.swagger.parser.OpenAPIParser;
2828
import io.swagger.v3.oas.models.OpenAPI;
@@ -40,7 +40,6 @@
4040
import org.openapitools.codegen.java.assertions.JavaFileAssert;
4141
import org.openapitools.codegen.languages.AbstractJavaCodegen;
4242
import org.openapitools.codegen.languages.JavaClientCodegen;
43-
import org.openapitools.codegen.languages.RubyClientCodegen;
4443
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
4544
import org.openapitools.codegen.languages.features.CXFServerFeatures;
4645
import org.openapitools.codegen.meta.features.SecurityFeature;
@@ -49,7 +48,6 @@
4948
import org.openapitools.codegen.testutils.ConfigAssert;
5049
import org.testng.Assert;
5150
import org.testng.annotations.DataProvider;
52-
import org.testng.annotations.Parameters;
5351
import org.testng.annotations.Test;
5452

5553
import java.io.File;
@@ -1791,13 +1789,71 @@ public void testJdkHttpClientWithAndWithoutParentExtension() {
17911789
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
17921790

17931791
validateJavaSourceFiles(files);
1794-
assertThat(files).hasSize(42);
1792+
assertThat(files).hasSize(48);
17951793
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
17961794
.content().contains("public class Child extends Person {");
17971795
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
17981796
.content().contains("public class Adult extends Person {");
1799-
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoParents.java"))
1800-
.content().contains("public class SchemaWithTwoParents {");
1797+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoAllOfRefs.java"))
1798+
.content().contains("public class SchemaWithTwoAllOfRefs {");
1799+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/AnotherChild.java"))
1800+
.content().contains("public class AnotherChild {");
1801+
}
1802+
1803+
@Test
1804+
public void allOfWithSeveralRefsAndRefAsParentInAllOfNormalizationIsTrue() {
1805+
final Path output = newTempFolder();
1806+
final CodegenConfigurator configurator = new CodegenConfigurator()
1807+
.setGeneratorName(JAVA_GENERATOR)
1808+
.addAdditionalProperty(CodegenConstants.API_PACKAGE, "xyz.abcdef.api")
1809+
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
1810+
.addAdditionalProperty(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker")
1811+
.addOpenapiNormalizer("REF_AS_PARENT_IN_ALLOF", "true")
1812+
.setInputSpec("src/test/resources/3_0/allOf_extension_parent.yaml")
1813+
.setOutputDir(output.toString().replace("\\", "/"));
1814+
1815+
DefaultGenerator generator = new DefaultGenerator();
1816+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
1817+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
1818+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
1819+
1820+
validateJavaSourceFiles(files);
1821+
assertThat(files).hasSize(48);
1822+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
1823+
.content().contains("public class Child extends Person {");
1824+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
1825+
.content().contains("public class Adult extends Person {");
1826+
// The class does not extend a parent since the REF_AS_PARENT_IN_ALLOF normalizer will assign it two parents
1827+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoAllOfRefsOneIsMarkedAsParent.java"))
1828+
.content().contains("public class SchemaWithTwoAllOfRefsOneIsMarkedAsParent {");
1829+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/AnotherChild.java"))
1830+
.content().contains("public class AnotherChild extends AnotherPerson {");
1831+
}
1832+
1833+
@Test
1834+
public void allOfWithSeveralRefsButOnlyOneIsMarkedAsParent() {
1835+
final Path output = newTempFolder();
1836+
final CodegenConfigurator configurator = new CodegenConfigurator()
1837+
.setGeneratorName(JAVA_GENERATOR)
1838+
.addAdditionalProperty(CodegenConstants.API_PACKAGE, "xyz.abcdef.api")
1839+
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
1840+
.addAdditionalProperty(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker")
1841+
.setInputSpec("src/test/resources/3_0/allOf_extension_parent.yaml")
1842+
.setOutputDir(output.toString().replace("\\", "/"));
1843+
1844+
DefaultGenerator generator = new DefaultGenerator();
1845+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
1846+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
1847+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
1848+
1849+
validateJavaSourceFiles(files);
1850+
assertThat(files).hasSize(48);
1851+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
1852+
.content().contains("public class Child extends Person {");
1853+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
1854+
.content().contains("public class Adult extends Person {");
1855+
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoAllOfRefsOneIsMarkedAsParent.java"))
1856+
.content().contains("public class SchemaWithTwoAllOfRefsOneIsMarkedAsParent extends PersonAExplicitParent {");
18011857
assertThat(output.resolve("src/main/java/xyz/abcdef/model/AnotherChild.java"))
18021858
.content().contains("public class AnotherChild {");
18031859
}
@@ -3986,15 +4042,15 @@ public void oneOfWithInnerModelTest() {
39864042
}
39874043

39884044
@Test
3989-
public void testOneOfClassWithAnnotation() throws IOException {
4045+
public void testOneOfClassWithAnnotation() {
39904046
final Map<String, File> files = generateFromContract("src/test/resources/3_0/java/oneOf-with-annotations.yaml", RESTCLIENT);
39914047
JavaFileAssert.assertThat(files.get("Fruit.java"))
39924048
.isNormalClass()
39934049
.assertTypeAnnotations().containsWithName("SuppressWarnings");
39944050
}
39954051

39964052
@Test
3997-
public void testOneOfInterfaceWithAnnotation() throws IOException {
4053+
public void testOneOfInterfaceWithAnnotation() {
39984054
final Map<String, File> files = generateFromContract("src/test/resources/3_0/java/oneOf-with-annotations.yaml", RESTCLIENT,
39994055
Map.of(USE_ONE_OF_INTERFACES, "true"));
40004056
JavaFileAssert.assertThat(files.get("Fruit.java"))

modules/openapi-generator/src/test/resources/3_0/allOf_extension_parent.yaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ components:
4242
properties:
4343
lastName:
4444
type: string
45+
PersonAExplicitParent:
46+
type: object
47+
x-parent: "abstract"
48+
properties:
49+
lastName:
50+
type: string
4551
PersonB:
46-
description:
4752
type: object
4853
properties:
4954
firstName:
@@ -76,7 +81,7 @@ components:
7681
type: integer
7782
format: int32
7883
- $ref: '#/components/schemas/AnotherPerson'
79-
SchemaWithTwoParents:
84+
SchemaWithTwoAllOfRefs:
8085
description: A schema that has two allOfs with refs
8186
allOf:
8287
- type: object
@@ -86,6 +91,16 @@ components:
8691
format: int32
8792
- $ref: '#/components/schemas/PersonA'
8893
- $ref: '#/components/schemas/PersonB'
94+
SchemaWithTwoAllOfRefsOneIsMarkedAsParent:
95+
description: A schema that has two allOfs with refs, but one of the allOfs is explicitly marked with x-parent
96+
allOf:
97+
- type: object
98+
properties:
99+
age:
100+
type: integer
101+
format: int32
102+
- $ref: '#/components/schemas/PersonAExplicitParent'
103+
- $ref: '#/components/schemas/PersonB'
89104
AnotherPerson:
90105
description: person object without x-parent extension
91106
type: object

0 commit comments

Comments
 (0)