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

added option to check duplicated model names. #10529

Merged
merged 3 commits into from
Oct 31, 2020
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 @@ -11,6 +11,8 @@
import java.util.regex.Pattern;

import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
import io.swagger.models.RefModel;
import io.swagger.models.properties.RefProperty;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -58,6 +60,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
public static final String SUPPORT_JAVA6 = "supportJava6";
public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
public static final String ERROR_ON_UNKNOWN_ENUM = "errorOnUnknownEnum";
public static final String CHECK_DUPLICATED_MODEL_NAME = "checkDuplicatedModelName";

protected String dateLibrary = "threetenbp";
protected boolean supportAsync = false;
Expand Down Expand Up @@ -187,6 +190,7 @@ public AbstractJavaCodegen() {
cliOptions.add(java8Mode);

cliOptions.add(CliOption.newBoolean(DISABLE_HTML_ESCAPING, "Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)"));
cliOptions.add(CliOption.newBoolean(CHECK_DUPLICATED_MODEL_NAME, "Check if there are duplicated model names (ignoring case)"));
}

@Override
Expand Down Expand Up @@ -1042,6 +1046,10 @@ public void preprocessSwagger(Swagger swagger) {
if (swagger == null || swagger.getPaths() == null){
return;
}
boolean checkDuplicatedModelName = Boolean.parseBoolean(additionalProperties.get(CHECK_DUPLICATED_MODEL_NAME) != null ? additionalProperties.get(CHECK_DUPLICATED_MODEL_NAME).toString() : "");
if (checkDuplicatedModelName) {
this.checkDuplicatedModelNameIgnoringCase(swagger);
}
for (String pathname : swagger.getPaths().keySet()) {
Path path = swagger.getPath(pathname);
if (path.getOperations() == null){
Expand Down Expand Up @@ -1099,6 +1107,84 @@ private static String getAccept(Operation operation) {
protected boolean needToImport(String type) {
return super.needToImport(type) && type.indexOf(".") < 0;
}

protected void checkDuplicatedModelNameIgnoringCase(Swagger swagger) {
final Map<String, Model> definitions = swagger.getDefinitions();
final Map<String, Map<String, Model>> definitionsRepeated = new HashMap<>();

for (String definitionKey : definitions.keySet()) {
final Model model = definitions.get(definitionKey);
final String lowerKeyDefinition = definitionKey.toLowerCase();

if (definitionsRepeated.containsKey(lowerKeyDefinition)) {
Map<String, Model> modelMap = definitionsRepeated.get(lowerKeyDefinition);
if (modelMap == null) {
modelMap = new HashMap<>();
definitionsRepeated.put(lowerKeyDefinition, modelMap);
}
modelMap.put(definitionKey, model);
} else {
definitionsRepeated.put(lowerKeyDefinition, null);
}
}
for (String lowerKeyDefinition : definitionsRepeated.keySet()) {
final Map<String, Model> modelMap = definitionsRepeated.get(lowerKeyDefinition);
if (modelMap == null) {
continue;
}
int index = 1;
for (String name : modelMap.keySet()) {
final Model model = modelMap.get(name);
final String newModelName = name + index;
definitions.put(newModelName, model);
replaceDuplicatedInPaths(swagger.getPaths(), name, newModelName);
replaceDuplicatedInModelProperties(definitions, name, newModelName);
definitions.remove(name);
index++;
}
}
}

protected void replaceDuplicatedInPaths(Map<String, Path> paths, String modelName, String newModelName) {
if (paths == null || paths.isEmpty()) {
return;
}
paths.values().stream()
.flatMap(path -> path.getOperations().stream())
.flatMap(operation -> operation.getParameters().stream())
.filter(parameter -> parameter instanceof BodyParameter
&& ((BodyParameter)parameter).getSchema() != null
&& ((BodyParameter)parameter).getSchema() instanceof RefModel
)
.forEach(parameter -> {
final RefModel refModel = (RefModel) ((BodyParameter)parameter).getSchema();
if (refModel.getSimpleRef().equals(modelName)) {
refModel.set$ref(refModel.get$ref().replace(modelName, newModelName));
}
});
paths.values().stream()
.flatMap(path -> path.getOperations().stream())
.flatMap(operation -> operation.getResponses().values().stream())
.filter(response -> response.getResponseSchema() != null && response.getResponseSchema() instanceof RefModel)
.forEach(response -> {
final RefModel refModel = (RefModel) response.getResponseSchema();
if (refModel.getSimpleRef().equals(modelName)) {
refModel.set$ref(refModel.get$ref().replace(modelName, newModelName));
}
});
}

protected void replaceDuplicatedInModelProperties(Map<String, Model> definitions, String modelName, String newModelName) {
definitions.values().stream()
.flatMap(model -> model.getProperties().values().stream())
.filter(property -> property instanceof RefProperty)
.forEach(property -> {
final RefProperty refProperty = (RefProperty) property;
if (refProperty.getSimpleRef().equals(modelName)) {
refProperty.set$ref(refProperty.get$ref().replace(modelName, newModelName));
}
});
}
/*
@Override
public String findCommonPrefixOfVars(List<String> vars) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public JavaOptionsProvider() {
.put(JavaClientCodegen.DISABLE_HTML_ESCAPING, "false")
.put("hideGenerationTimestamp", "true")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(JavaClientCodegen.CHECK_DUPLICATED_MODEL_NAME, "false")
//.put("supportJava6", "true")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true")
.put(JavaClientCodegen.JAVA8_MODE, JAVA8_MODE_VALUE)
.put(JavaClientCodegen.WITH_XML, WITH_XML_VALUE)
//.put(JavaClientCodegen.DATE_LIBRARY, "joda")
.put("hideGenerationTimestamp", "true")
.put(JavaClientCodegen.DISABLE_HTML_ESCAPING, "false")
.put(JavaCXFServerCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION)
.put("serverPort", "2345")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(JavaJerseyServerCodegen.USE_TAGS, USE_TAGS);
.put(JavaJerseyServerCodegen.USE_TAGS, USE_TAGS)
.put(JavaClientCodegen.CHECK_DUPLICATED_MODEL_NAME, "false");

return builder.build();
}
Expand Down