Skip to content

Commit 1755794

Browse files
authored
Merge pull request #509 from swagger-api/ticket-9794
refs swagger-api/swagger-codegen#9794 - remove conflict enum from child
2 parents 3079e7d + c5d6fe7 commit 1755794

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/main/java/io/swagger/codegen/v3/generators/java/AbstractJavaCodegen.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.List;
3636
import java.util.ListIterator;
3737
import java.util.Map;
38+
import java.util.Objects;
3839
import java.util.regex.Pattern;
3940

4041
import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
@@ -1182,7 +1183,12 @@ private static CodegenModel reconcileInlineEnums(CodegenModel codegenModel, Code
11821183
while (iterator.hasNext()) {
11831184
CodegenProperty codegenProperty = iterator.next();
11841185
isEnum = getBooleanValue(codegenProperty, IS_ENUM_EXT_NAME);
1185-
if (isEnum && codegenProperty.equals(parentModelCodegenPropery)) {
1186+
// we don't check for the full set of properties as they could be overridden
1187+
// e.g. in the child; if we used codegenProperty.equals, the result in this
1188+
// case would be `false` resulting on 2 different enums created on parent and
1189+
// child classes, used in same method. This means that the child class will use
1190+
// the enum defined in the parent, loosing any overridden property
1191+
if (isEnum && isSameEnum(codegenProperty, parentModelCodegenPropery)) {
11861192
// We found an enum in the child class that is
11871193
// a duplicate of the one in the parent, so remove it.
11881194
iterator.remove();
@@ -1199,11 +1205,48 @@ private static CodegenModel reconcileInlineEnums(CodegenModel codegenModel, Code
11991205
count += 1;
12001206
codegenProperty.getVendorExtensions().put(CodegenConstants.HAS_MORE_EXT_NAME, (count < numVars) ? true : false);
12011207
}
1208+
1209+
if (!codegenProperties.isEmpty()) {
1210+
codegenModel.getVendorExtensions().put(CodegenConstants.HAS_VARS_EXT_NAME, true);
1211+
codegenModel.getVendorExtensions().put(CodegenConstants.HAS_ENUMS_EXT_NAME, false);
1212+
} else {
1213+
codegenModel.emptyVars = true;
1214+
codegenModel.getVendorExtensions().put(CodegenConstants.HAS_VARS_EXT_NAME, false);
1215+
codegenModel.getVendorExtensions().put(CodegenConstants.HAS_ENUMS_EXT_NAME, false);
1216+
}
1217+
1218+
12021219
codegenModel.vars = codegenProperties;
12031220
}
12041221
return codegenModel;
1222+
1223+
12051224
}
12061225

1226+
protected static boolean isSameEnum(CodegenProperty actual, CodegenProperty other) {
1227+
if (actual == null && other == null) {
1228+
return true;
1229+
}
1230+
if ((actual.name == null) ? (other.name != null) : !actual.name.equals(other.name)) {
1231+
return false;
1232+
}
1233+
if ((actual.baseName == null) ? (other.baseName != null) : !actual.baseName.equals(other.baseName)) {
1234+
return false;
1235+
}
1236+
if ((actual.datatype == null) ? (other.datatype != null) : !actual.datatype.equals(other.datatype)) {
1237+
return false;
1238+
}
1239+
if ((actual.datatypeWithEnum == null) ? (other.datatypeWithEnum != null) : !actual.datatypeWithEnum.equals(other.datatypeWithEnum)) {
1240+
return false;
1241+
}
1242+
if ((actual.baseType == null) ? (other.baseType != null) : !actual.baseType.equals(other.baseType)) {
1243+
return false;
1244+
}
1245+
if (!Objects.equals(actual.enumName, other.enumName)) {
1246+
return false;
1247+
}
1248+
return true;
1249+
}
12071250
private static String sanitizePackageName(String packageName) {
12081251
packageName = packageName.trim(); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
12091252
packageName = packageName.replaceAll("[^a-zA-Z0-9_\\.]", "_");

0 commit comments

Comments
 (0)