Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inheritance with modelPrefix #3151

Merged
merged 2 commits into from Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1756,10 +1756,10 @@ public CodegenModel fromModel(String name, Schema schema) {
m.interfaces.add(modelName);
addImport(m, modelName);
if (allDefinitions != null && refSchema != null) {
if (allParents.contains(modelName) && supportsMultipleInheritance) {
if (allParents.contains(ref) && supportsMultipleInheritance) {
// multiple inheritance
addProperties(allProperties, allRequired, refSchema);
} else if (parentName != null && parentName.equals(modelName) && supportsInheritance) {
} else if (parentName != null && parentName.equals(ref) && supportsInheritance) {
// single inheritance
addProperties(allProperties, allRequired, refSchema);
} else {
Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.openapitools.codegen;

import com.google.common.collect.Sets;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -28,10 +29,15 @@
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.languages.features.CXFServerFeatures;
import org.openapitools.codegen.utils.ModelUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -654,4 +660,48 @@ public void mapParamImportInnerObject() {

Assert.assertEquals(imports, expected);
}

@Test
public void modelDoNotContainInheritedVars() {
DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/generic.yaml", null, new ParseOptions()).getOpenAPI();
codegen.setOpenAPI(openAPI);

CodegenModel codegenModel = codegen.fromModel("Dog", openAPI.getComponents().getSchemas().get("Dog"));

Assert.assertEquals(codegenModel.vars.size(), 1);
}

@Test
public void modelWithPrefixDoNotContainInheritedVars() {
DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
codegen.setModelNamePrefix("prefix");

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/generic.yaml", null, new ParseOptions()).getOpenAPI();
codegen.setOpenAPI(openAPI);

CodegenModel codegenModel = codegen.fromModel("Dog", openAPI.getComponents().getSchemas().get("Dog"));

Assert.assertEquals(codegenModel.vars.size(), 1);
}
@Test
public void modelWithSuffuxDoNotContainInheritedVars() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo: ”Suffux” vs. ”Suffix”

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix thanks

DefaultCodegen codegen = new DefaultCodegen();
codegen.supportsInheritance = true;
codegen.setModelNameSuffix("suffix");

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/generic.yaml", null, new ParseOptions()).getOpenAPI();
codegen.setOpenAPI(openAPI);

CodegenModel codegenModel = codegen.fromModel("Dog", openAPI.getComponents().getSchemas().get("Dog"));

Assert.assertEquals(codegenModel.vars.size(), 1);
}

}