Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testOpenAPINormalizerRefAsParentInAllOf() {
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);
Expand All @@ -79,10 +79,10 @@ public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithPropertie
// 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<>();
Expand All @@ -91,10 +91,10 @@ public void testOpenAPINormalizerRefAsParentInAllOfAndRefactorAllOfWithPropertie
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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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;
Expand All @@ -40,7 +40,6 @@
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;
Expand All @@ -49,7 +48,6 @@
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;
Expand Down Expand Up @@ -1791,13 +1789,71 @@ public void testJdkHttpClientWithAndWithoutParentExtension() {
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 {");
}
Expand Down Expand Up @@ -3986,15 +4042,15 @@ public void oneOfWithInnerModelTest() {
}

@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()
.assertTypeAnnotations().containsWithName("SuppressWarnings");
}

@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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading