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 generated schema name conflicts #4772

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -18,6 +18,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class AbstractModelConverter implements ModelConverter {
protected final ObjectMapper _mapper;
Expand Down Expand Up @@ -65,7 +67,28 @@ protected String _typeName(JavaType type, BeanDescription beanDesc) {
return name;
}
name = _findTypeName(type, beanDesc);
// Check if the resolved type name already exists in the map
if (_resolvedTypeNames.containsValue(name)) {
// Determine if the type is from the Java standard library
final boolean isFromJava = type.getRawClass().getCanonicalName().contains("java.lang.");
// If the type is not a collection, map, primitive, or from Java standard library
if (!type.isCollectionLikeType() && !type.isMapLikeType() && !type.isPrimitive() && !isFromJava) {
// Iterate through the resolved type names to find conflicts
for (Map.Entry<JavaType, String> entry : _resolvedTypeNames.entrySet()) {
JavaType key = entry.getKey();
String value = entry.getValue();
// If a conflict is found (same name but different type)
if (value.equals(name)) {
if (key != type) {
// Generate a new name postfix to resolve the conflict
name = genNamePostfix(name);
}
}
}
}
}
_resolvedTypeNames.put(type, name);

return name;
}

Expand Down Expand Up @@ -120,4 +143,21 @@ protected boolean _isSetType(Class<?> cls) {
}
return false;
}

private String genNamePostfix(String name) {
// Regular expression to match a number at the end of the string
Pattern pattern = Pattern.compile("(\\d+)$");
Matcher matcher = pattern.matcher(name);

if (matcher.find()) {
// Extract the number, increment it, and replace it in the name
String numberStr = matcher.group(1);
int number = Integer.parseInt(numberStr);
number++;
return name.substring(0, matcher.start(1)) + number;
} else {
// If no number at the end, append "2"
return name + "2";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ protected String nameForClass(Class<?> cls, Set<Options> options) {
}

protected String getNameOfClass(Class<?> cls) {
return useFqn?cls.getName():cls.getSimpleName();
if(useFqn){
return cls.getName();
}
String simpleName = cls.getSimpleName();
if(cls.isMemberClass()){
String enclosingClsName = cls.getEnclosingClass().getSimpleName();
return String.format("%s%s", enclosingClsName, simpleName);
}
return simpleName;
}

protected String nameForGenericType(JavaType type, Set<Options> options) {
Expand Down