|
11 | 11 | import java.util.regex.Pattern;
|
12 | 12 |
|
13 | 13 | import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
|
| 14 | +import io.swagger.models.RefModel; |
| 15 | +import io.swagger.models.properties.RefProperty; |
14 | 16 | import org.apache.commons.lang3.BooleanUtils;
|
15 | 17 | import org.apache.commons.lang3.StringUtils;
|
16 | 18 |
|
@@ -58,6 +60,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
58 | 60 | public static final String SUPPORT_JAVA6 = "supportJava6";
|
59 | 61 | public static final String DISABLE_HTML_ESCAPING = "disableHtmlEscaping";
|
60 | 62 | public static final String ERROR_ON_UNKNOWN_ENUM = "errorOnUnknownEnum";
|
| 63 | + public static final String CHECK_DUPLICATED_MODEL_NAME = "checkDuplicatedModelName"; |
61 | 64 |
|
62 | 65 | protected String dateLibrary = "threetenbp";
|
63 | 66 | protected boolean supportAsync = false;
|
@@ -187,6 +190,7 @@ public AbstractJavaCodegen() {
|
187 | 190 | cliOptions.add(java8Mode);
|
188 | 191 |
|
189 | 192 | cliOptions.add(CliOption.newBoolean(DISABLE_HTML_ESCAPING, "Disable HTML escaping of JSON strings when using gson (needed to avoid problems with byte[] fields)"));
|
| 193 | + cliOptions.add(CliOption.newBoolean(CHECK_DUPLICATED_MODEL_NAME, "Check if there are duplicated model names (ignoring case)")); |
190 | 194 | }
|
191 | 195 |
|
192 | 196 | @Override
|
@@ -1042,6 +1046,10 @@ public void preprocessSwagger(Swagger swagger) {
|
1042 | 1046 | if (swagger == null || swagger.getPaths() == null){
|
1043 | 1047 | return;
|
1044 | 1048 | }
|
| 1049 | + boolean checkDuplicatedModelName = Boolean.parseBoolean(additionalProperties.get(CHECK_DUPLICATED_MODEL_NAME) != null ? additionalProperties.get(CHECK_DUPLICATED_MODEL_NAME).toString() : ""); |
| 1050 | + if (checkDuplicatedModelName) { |
| 1051 | + this.checkDuplicatedModelNameIgnoringCase(swagger); |
| 1052 | + } |
1045 | 1053 | for (String pathname : swagger.getPaths().keySet()) {
|
1046 | 1054 | Path path = swagger.getPath(pathname);
|
1047 | 1055 | if (path.getOperations() == null){
|
@@ -1099,6 +1107,84 @@ private static String getAccept(Operation operation) {
|
1099 | 1107 | protected boolean needToImport(String type) {
|
1100 | 1108 | return super.needToImport(type) && type.indexOf(".") < 0;
|
1101 | 1109 | }
|
| 1110 | + |
| 1111 | + protected void checkDuplicatedModelNameIgnoringCase(Swagger swagger) { |
| 1112 | + final Map<String, Model> definitions = swagger.getDefinitions(); |
| 1113 | + final Map<String, Map<String, Model>> definitionsRepeated = new HashMap<>(); |
| 1114 | + |
| 1115 | + for (String definitionKey : definitions.keySet()) { |
| 1116 | + final Model model = definitions.get(definitionKey); |
| 1117 | + final String lowerKeyDefinition = definitionKey.toLowerCase(); |
| 1118 | + |
| 1119 | + if (definitionsRepeated.containsKey(lowerKeyDefinition)) { |
| 1120 | + Map<String, Model> modelMap = definitionsRepeated.get(lowerKeyDefinition); |
| 1121 | + if (modelMap == null) { |
| 1122 | + modelMap = new HashMap<>(); |
| 1123 | + definitionsRepeated.put(lowerKeyDefinition, modelMap); |
| 1124 | + } |
| 1125 | + modelMap.put(definitionKey, model); |
| 1126 | + } else { |
| 1127 | + definitionsRepeated.put(lowerKeyDefinition, null); |
| 1128 | + } |
| 1129 | + } |
| 1130 | + for (String lowerKeyDefinition : definitionsRepeated.keySet()) { |
| 1131 | + final Map<String, Model> modelMap = definitionsRepeated.get(lowerKeyDefinition); |
| 1132 | + if (modelMap == null) { |
| 1133 | + continue; |
| 1134 | + } |
| 1135 | + int index = 1; |
| 1136 | + for (String name : modelMap.keySet()) { |
| 1137 | + final Model model = modelMap.get(name); |
| 1138 | + final String newModelName = name + index; |
| 1139 | + definitions.put(newModelName, model); |
| 1140 | + replaceDuplicatedInPaths(swagger.getPaths(), name, newModelName); |
| 1141 | + replaceDuplicatedInModelProperties(definitions, name, newModelName); |
| 1142 | + definitions.remove(name); |
| 1143 | + index++; |
| 1144 | + } |
| 1145 | + } |
| 1146 | + } |
| 1147 | + |
| 1148 | + protected void replaceDuplicatedInPaths(Map<String, Path> paths, String modelName, String newModelName) { |
| 1149 | + if (paths == null || paths.isEmpty()) { |
| 1150 | + return; |
| 1151 | + } |
| 1152 | + paths.values().stream() |
| 1153 | + .flatMap(path -> path.getOperations().stream()) |
| 1154 | + .flatMap(operation -> operation.getParameters().stream()) |
| 1155 | + .filter(parameter -> parameter instanceof BodyParameter |
| 1156 | + && ((BodyParameter)parameter).getSchema() != null |
| 1157 | + && ((BodyParameter)parameter).getSchema() instanceof RefModel |
| 1158 | + ) |
| 1159 | + .forEach(parameter -> { |
| 1160 | + final RefModel refModel = (RefModel) ((BodyParameter)parameter).getSchema(); |
| 1161 | + if (refModel.getSimpleRef().equals(modelName)) { |
| 1162 | + refModel.set$ref(refModel.get$ref().replace(modelName, newModelName)); |
| 1163 | + } |
| 1164 | + }); |
| 1165 | + paths.values().stream() |
| 1166 | + .flatMap(path -> path.getOperations().stream()) |
| 1167 | + .flatMap(operation -> operation.getResponses().values().stream()) |
| 1168 | + .filter(response -> response.getResponseSchema() != null && response.getResponseSchema() instanceof RefModel) |
| 1169 | + .forEach(response -> { |
| 1170 | + final RefModel refModel = (RefModel) response.getResponseSchema(); |
| 1171 | + if (refModel.getSimpleRef().equals(modelName)) { |
| 1172 | + refModel.set$ref(refModel.get$ref().replace(modelName, newModelName)); |
| 1173 | + } |
| 1174 | + }); |
| 1175 | + } |
| 1176 | + |
| 1177 | + protected void replaceDuplicatedInModelProperties(Map<String, Model> definitions, String modelName, String newModelName) { |
| 1178 | + definitions.values().stream() |
| 1179 | + .flatMap(model -> model.getProperties().values().stream()) |
| 1180 | + .filter(property -> property instanceof RefProperty) |
| 1181 | + .forEach(property -> { |
| 1182 | + final RefProperty refProperty = (RefProperty) property; |
| 1183 | + if (refProperty.getSimpleRef().equals(modelName)) { |
| 1184 | + refProperty.set$ref(refProperty.get$ref().replace(modelName, newModelName)); |
| 1185 | + } |
| 1186 | + }); |
| 1187 | + } |
1102 | 1188 | /*
|
1103 | 1189 | @Override
|
1104 | 1190 | public String findCommonPrefixOfVars(List<String> vars) {
|
|
0 commit comments