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 potential NPE when no source and no body #4932

Closed
wants to merge 1 commit into from

Conversation

dadoonet
Copy link
Member

In recent changes, we added missing support for source parameter in some REST APIs:

        BytesReference content = null;
        if (request.hasContent()) {
            content = request.content();
        } else {
            String source = request.param("source");
            if (source != null) {
                content = new BytesArray(source);
            }
        }

It's definitely better to have:

        BytesReference content = request.content();
        if (!request.hasContent()) {
            String source = request.param("source");
            if (source != null) {
                content = new BytesArray(source);
            }
        }

That said, it could be nice to have a single method to manage it for various REST actions.

Closes #4924.

In recent changes, we added missing support for `source` parameter in some REST APIs:

* elastic#4892 : mget
* elastic#4900 : mpercolate
* elastic#4901 : msearch
* elastic#4902 : mtermvectors
* elastic#4903 : percolate

```java
        BytesReference content = null;
        if (request.hasContent()) {
            content = request.content();
        } else {
            String source = request.param("source");
            if (source != null) {
                content = new BytesArray(source);
            }
        }
```

It's definitely better to have:

```java
        BytesReference content = request.content();
        if (!request.hasContent()) {
            String source = request.param("source");
            if (source != null) {
                content = new BytesArray(source);
            }
        }
```

That said, it could be nice to have a single method to manage it for various REST actions.

Closes elastic#4924.
* @return rest content
*/
public static BytesReference getRestContent(RestRequest request) {
if (request == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm when can this be null? sorry I looked at it earlier but the request should never be null no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree. I'm just paranoiac! :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

YEAH so I'd put an assert in there just for kicks

@s1monw
Copy link
Contributor

s1monw commented Jan 28, 2014

aside of the comment this LGTM and is good to go

@dadoonet dadoonet closed this Jan 28, 2014
@dadoonet dadoonet deleted the issue/4924-source-rest branch January 28, 2014 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix potential NPE when no source and no body
3 participants