Skip to content

Conversation

@ngocnhan-tran1996
Copy link
Contributor

@ngocnhan-tran1996 ngocnhan-tran1996 commented Aug 24, 2025

This PR includes

  • Use StringUtils#hasLength
  • Remove redundant check and exception
  • Update javadoc
  • Polish some methods

Signed-off-by: Tran Ngoc Nhan <[email protected]>
Signed-off-by: Tran Ngoc Nhan <[email protected]>
Signed-off-by: Tran Ngoc Nhan <[email protected]>
Signed-off-by: Tran Ngoc Nhan <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 24, 2025
if (result.length() == 0) {
if (result.isEmpty()) {
return null;
}
Copy link
Contributor

@ronodhirSoumik ronodhirSoumik Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String result = (query == null) ? "" : artifactPattern.matcher(query).replaceFirst("");

if (result.isEmpty()) {
    return null;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, @ronodhirSoumik, though the Spring checkstyle requires a negation in ternaries. So, this is probably the result of having run checkstyleTest on the PR.

* @return
* @param artifactParameterName the artifactParameterName that is removed from the
* current URL. The result becomes the service url. Cannot be null and cannot be an
* empty String.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot be null and cannot be an empty String -> Cannot be null or empty String

return role;
}
if (defaultRolePrefix == null || defaultRolePrefix.length() == 0) {
if (defaultRolePrefix == null || defaultRolePrefix.isEmpty()) {
Copy link
Contributor

@ronodhirSoumik ronodhirSoumik Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there are several uses of str == null || str.isEmpty() I think a common util function will be helpful

boolean isEmpty(String str) {
	return str == null || str.length == 0;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use !org.springframework.util.StringUtils#hasLength

Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the great cleanup, @ngocnhan-tran1996. I've left some feedback inline.

@Override
public boolean equals(Object obj) {
if (this == obj) {
if (super.equals(obj)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please stay with this == obj as this is a bit more obvious what kind of shortcut we are doing.

}
if (!super.equals(obj) || !(obj instanceof DefaultServiceAuthenticationDetails)) {
return false;
if (obj instanceof DefaultServiceAuthenticationDetails that) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this change. In Spring Security, it's preferred to exit early for simple cases. Would you please flip the logic here to:

if (!(obj instanceof DefaultServiceAuthenticationDetails that)) {
    return false;
}
// ... continue

*/
private String getQueryString(final HttpServletRequest request, final Pattern artifactPattern) {
final String query = request.getQueryString();
if (query == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please keep checking for null here since that allows us to exit early without needing to instantiate a variable.

if (result.length() == 0) {
if (result.isEmpty()) {
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, @ronodhirSoumik, though the Spring checkstyle requires a negation in ternaries. So, this is probably the result of having run checkstyleTest on the PR.

* @param defaultRolePrefix
* @param role
* @return
* @param defaultRolePrefix the default prefix to add to roles.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's please give an example here since "role prefix" could be a bit esoteric. For example:

the default prefix to add to roles, for example {@code ROLE_}

import org.springframework.util.StringUtils;

/**
* Implementation of PasswordEncoder.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't quite say that this is an implementation as much as it is an abstract class that allows subclasses to except the password to be non-{@code null}.

Perhaps change this to:

/**
  * An abstract {@link PasswordEncoder} that implementers can use for expecting the
  * password to be non-{@code null}. Each common password API method is accompanied
  * with an abstract method with a {@code NonNull} prefix. By implementing this, the concrete
  * class is specifying what to do with the password when it is non-{@code null}, allowing this
  * class to handle the {@code null} case.
  * 
  * @author Rob Winch
  * @since 7.0
  */

public final boolean upgradeEncoding(@Nullable String encodedPassword) {
if (encodedPassword == null || encodedPassword.length() == 0) {
return false;
if (StringUtils.hasLength(encodedPassword)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please favor the simpler exits coming first:

if (!StringUtils.hasLength(encodedPassword)) {
    return false;
}
return upgradeEncodingNonNull(encodedPassword);

This also has the added benefit of making the overall change smaller.

this.strippedServletPath = strip(request.getServletPath());
String pathInfo = strip(request.getPathInfo());
if (pathInfo != null && pathInfo.length() == 0) {
if (pathInfo != null && pathInfo.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would !StringUtils.hasLength(pathInfo)) work here as well?

@jzheaux jzheaux self-assigned this Oct 20, 2025
@jzheaux jzheaux added in: core An issue in spring-security-core type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 20, 2025
Signed-off-by: Tran Ngoc Nhan <[email protected]>
Signed-off-by: Tran Ngoc Nhan <[email protected]>
Signed-off-by: Tran Ngoc Nhan <[email protected]>
Signed-off-by: Tran Ngoc Nhan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core An issue in spring-security-core type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants