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

Fixes two NPE conditions in DefaultEntityResolver return statement #1400

Merged

Conversation

Nick-Wunderdog
Copy link
Contributor

@Nick-Wunderdog Nick-Wunderdog commented May 4, 2023

Fixes:

  1. initializes the Optional as empty, not as null object.
  2. added [optional] exception handler for AccessDeniedException for throwing it.
  3. added [optional] (slf4j) logging

First change fixes the two NullPointerExeception cases in method last return statement.
a) when invoker == null
b) AccessDeniedException or any Expection other than ConverterNotFoundException is thrown.

2.&3. are not necessary, but I think improve the code.

@CLAassistant
Copy link

CLAassistant commented May 4, 2023

CLA assistant check
All committers have signed the CLA.

@@ -163,7 +163,7 @@ public Object findOne(Repositories repositories, StoreInfo info, String reposito
public Object findOne(Repositories repositories, StoreInfo info, Class<?> domainObjClass, String repository, Serializable id)
throws HttpRequestMethodNotSupportedException {

Optional<Object> domainObj = null;
Optional<Object> domainObj = Optional.empty();
Copy link
Contributor Author

@Nick-Wunderdog Nick-Wunderdog May 4, 2023

Choose a reason for hiding this comment

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

Fixes the two cases of NPE in return statement:

  1. if AccessDeniedException is thrown.
  2. if (invoker == null)

@@ -55,7 +55,7 @@
import internal.org.springframework.content.rest.utils.StoreUtils;

public class DefaultEntityResolver implements EntityResolver {

private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DefaultEntityResolver.class);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Logging would be nice, but not mandatory.

domainObj = findOneByReflection(repositories, domainObjClass, id);
} catch (AccessDeniedException ace) {
Copy link
Contributor Author

@Nick-Wunderdog Nick-Wunderdog May 4, 2023

Choose a reason for hiding this comment

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

Catching the ACE explicitly is be good for code clarity. Regardless if you want to or not to throw ACE. The If you throw it the Servlet context calling this class should be able to process ACE to 403 HTTP response.

@@ -172,13 +175,17 @@ public Object findOne(Repositories repositories, StoreInfo info, Class<?> domain
invoker = resolveRootResourceInformation(info, repository, id, new ModelAndViewContainer(), new FakeWebBinderFactory());
if (invoker != null) {
domainObj = invoker.invokeFindById(id);
} else {
logger.warn("Could not resolve RootResourceInformation");
Copy link
Contributor Author

@Nick-Wunderdog Nick-Wunderdog May 4, 2023

Choose a reason for hiding this comment

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

  1. domainObj NPE condition in the original code.

} catch (Exception e) {

e.printStackTrace();
logger.error("invoking Repository findById(id) method threw exception", e.getCause());
Copy link
Contributor Author

@Nick-Wunderdog Nick-Wunderdog May 4, 2023

Choose a reason for hiding this comment

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

  1. domainObj NPE condition in the original code.

@Nick-Wunderdog Nick-Wunderdog changed the title Fixes for AccessDeniedException causing NPE in DefaultEntityResolver Fixes two NPE conditions in DefaultEntityResolver return statement May 4, 2023
@paulcwarren paulcwarren merged commit 91db703 into paulcwarren:main May 6, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants