[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
This commit is contained in:
Mattias Sehlstedt 2025-10-06 07:07:50 +02:00 committed by GitHub
parent 6f3dacafc4
commit 4352a2fcd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 86 additions and 15 deletions

View File

@ -66,7 +66,7 @@ public class OpenAPINormalizerTest {
assertEquals(schema5.getExtensions().get(X_PARENT), "abstract");
// Verify that all allOf refs gets marked as parents
Schema<?>schemaWithTwoParents = openAPI.getComponents().getSchemas().get("SchemaWithTwoParents");
Schema<?>schemaWithTwoParents = openAPI.getComponents().getSchemas().get("SchemaWithTwoAllOfRefs");
assertNull(schemaWithTwoParents.getExtensions());
Schema<?>personA = openAPI.getComponents().getSchemas().get("PersonA");
assertEquals(personA.getExtensions().get(X_PARENT), true);
@ -79,10 +79,10 @@ public class OpenAPINormalizerTest {
// to test the both REF_AS_PARENT_IN_ALLOF and REFACTOR_ALLOF_WITH_PROPERTIES_ONLY
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/allOf_extension_parent.yaml");
Schema schema = openAPI.getComponents().getSchemas().get("Child");
Schema<?> schema = openAPI.getComponents().getSchemas().get("Child");
assertNull(schema.getExtensions());
Schema schema2 = openAPI.getComponents().getSchemas().get("Ancestor");
Schema<?> schema2 = openAPI.getComponents().getSchemas().get("Ancestor");
assertNull(schema2.getExtensions());
Map<String, String> options = new HashMap<>();
@ -91,10 +91,10 @@ public class OpenAPINormalizerTest {
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
openAPINormalizer.normalize();
Schema schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
Schema<?> schema3 = openAPI.getComponents().getSchemas().get("Ancestor");
assertEquals(schema3.getExtensions().get(X_PARENT), true);
Schema schema4 = openAPI.getComponents().getSchemas().get("Child");
Schema<?> schema4 = openAPI.getComponents().getSchemas().get("Child");
assertNull(schema4.getExtensions());
}

View File

@ -22,7 +22,7 @@ import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.*;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.google.common.collect.ImmutableMap;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
@ -40,7 +40,6 @@ import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.openapitools.codegen.languages.RubyClientCodegen;
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
import org.openapitools.codegen.languages.features.CXFServerFeatures;
import org.openapitools.codegen.meta.features.SecurityFeature;
@ -49,7 +48,6 @@ import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.testutils.ConfigAssert;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.io.File;
@ -1791,13 +1789,71 @@ public class JavaClientCodegenTest {
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
validateJavaSourceFiles(files);
assertThat(files).hasSize(42);
assertThat(files).hasSize(48);
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
.content().contains("public class Child extends Person {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
.content().contains("public class Adult extends Person {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoParents.java"))
.content().contains("public class SchemaWithTwoParents {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoAllOfRefs.java"))
.content().contains("public class SchemaWithTwoAllOfRefs {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/AnotherChild.java"))
.content().contains("public class AnotherChild {");
}
@Test
public void allOfWithSeveralRefsAndRefAsParentInAllOfNormalizationIsTrue() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.addAdditionalProperty(CodegenConstants.API_PACKAGE, "xyz.abcdef.api")
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
.addAdditionalProperty(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker")
.addOpenapiNormalizer("REF_AS_PARENT_IN_ALLOF", "true")
.setInputSpec("src/test/resources/3_0/allOf_extension_parent.yaml")
.setOutputDir(output.toString().replace("\\", "/"));
DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
validateJavaSourceFiles(files);
assertThat(files).hasSize(48);
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
.content().contains("public class Child extends Person {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
.content().contains("public class Adult extends Person {");
// The class does not extend a parent since the REF_AS_PARENT_IN_ALLOF normalizer will assign it two parents
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoAllOfRefsOneIsMarkedAsParent.java"))
.content().contains("public class SchemaWithTwoAllOfRefsOneIsMarkedAsParent {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/AnotherChild.java"))
.content().contains("public class AnotherChild extends AnotherPerson {");
}
@Test
public void allOfWithSeveralRefsButOnlyOneIsMarkedAsParent() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.addAdditionalProperty(CodegenConstants.API_PACKAGE, "xyz.abcdef.api")
.addAdditionalProperty(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model")
.addAdditionalProperty(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker")
.setInputSpec("src/test/resources/3_0/allOf_extension_parent.yaml")
.setOutputDir(output.toString().replace("\\", "/"));
DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
validateJavaSourceFiles(files);
assertThat(files).hasSize(48);
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Child.java"))
.content().contains("public class Child extends Person {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/Adult.java"))
.content().contains("public class Adult extends Person {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/SchemaWithTwoAllOfRefsOneIsMarkedAsParent.java"))
.content().contains("public class SchemaWithTwoAllOfRefsOneIsMarkedAsParent extends PersonAExplicitParent {");
assertThat(output.resolve("src/main/java/xyz/abcdef/model/AnotherChild.java"))
.content().contains("public class AnotherChild {");
}
@ -3986,7 +4042,7 @@ public class JavaClientCodegenTest {
}
@Test
public void testOneOfClassWithAnnotation() throws IOException {
public void testOneOfClassWithAnnotation() {
final Map<String, File> files = generateFromContract("src/test/resources/3_0/java/oneOf-with-annotations.yaml", RESTCLIENT);
JavaFileAssert.assertThat(files.get("Fruit.java"))
.isNormalClass()
@ -3994,7 +4050,7 @@ public class JavaClientCodegenTest {
}
@Test
public void testOneOfInterfaceWithAnnotation() throws IOException {
public void testOneOfInterfaceWithAnnotation() {
final Map<String, File> files = generateFromContract("src/test/resources/3_0/java/oneOf-with-annotations.yaml", RESTCLIENT,
Map.of(USE_ONE_OF_INTERFACES, "true"));
JavaFileAssert.assertThat(files.get("Fruit.java"))

View File

@ -42,8 +42,13 @@ components:
properties:
lastName:
type: string
PersonAExplicitParent:
type: object
x-parent: "abstract"
properties:
lastName:
type: string
PersonB:
description:
type: object
properties:
firstName:
@ -76,7 +81,7 @@ components:
type: integer
format: int32
- $ref: '#/components/schemas/AnotherPerson'
SchemaWithTwoParents:
SchemaWithTwoAllOfRefs:
description: A schema that has two allOfs with refs
allOf:
- type: object
@ -86,6 +91,16 @@ components:
format: int32
- $ref: '#/components/schemas/PersonA'
- $ref: '#/components/schemas/PersonB'
SchemaWithTwoAllOfRefsOneIsMarkedAsParent:
description: A schema that has two allOfs with refs, but one of the allOfs is explicitly marked with x-parent
allOf:
- type: object
properties:
age:
type: integer
format: int32
- $ref: '#/components/schemas/PersonAExplicitParent'
- $ref: '#/components/schemas/PersonB'
AnotherPerson:
description: person object without x-parent extension
type: object