Skip to content

fix the kotlin suspend function validation issure 23499. #29840

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -120,7 +120,7 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter

public LocalValidatorFactoryBean() {
if (KotlinDetector.isKotlinReflectPresent()) {
this.parameterNameDiscoverer = new KotlinReflectionParameterNameDiscoverer();
this.parameterNameDiscoverer = new KotlinReflectionParameterNameDiscoverer(true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
* @see DefaultParameterNameDiscoverer
*/
public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDiscoverer {
private boolean includeContinuationParameter;

public KotlinReflectionParameterNameDiscoverer() {
this(false);
}

public KotlinReflectionParameterNameDiscoverer(boolean includeContinuationParameter) {
this.includeContinuationParameter = includeContinuationParameter;
}

@Override
@Nullable
Expand All @@ -49,7 +58,7 @@ public String[] getParameterNames(Method method) {

try {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return (function != null ? getParameterNames(function.getParameters()) : null);
return (function != null ? getParameterNames(function.getParameters(), includeContinuationParameter && function.isSuspend()) : null);
}
catch (UnsupportedOperationException ex) {
return null;
Expand All @@ -65,21 +74,21 @@ public String[] getParameterNames(Constructor<?> ctor) {

try {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor);
return (function != null ? getParameterNames(function.getParameters()) : null);
return (function != null ? getParameterNames(function.getParameters(), false) : null);
}
catch (UnsupportedOperationException ex) {
return null;
}
}

@Nullable
private String[] getParameterNames(List<KParameter> parameters) {
private String[] getParameterNames(List<KParameter> parameters, boolean addContinuationName) {
List<KParameter> filteredParameters = parameters
.stream()
// Extension receivers of extension methods must be included as they appear as normal method parameters in Java
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind()) || KParameter.Kind.EXTENSION_RECEIVER.equals(p.getKind()))
.toList();
String[] parameterNames = new String[filteredParameters.size()];
String[] parameterNames = new String[filteredParameters.size() + (addContinuationName ? 1 : 0)];
for (int i = 0; i < filteredParameters.size(); i++) {
KParameter parameter = filteredParameters.get(i);
// extension receivers are not explicitly named, but require a name for Java interoperability
Expand All @@ -90,7 +99,9 @@ private String[] getParameterNames(List<KParameter> parameters) {
}
parameterNames[i] = name;
}
if (addContinuationName) {
parameterNames[filteredParameters.size()] = "continuation";
}
return parameterNames;
}

}