Skip to content

Commit

Permalink
Fix DeleteRequest / GetRequest / UpdateRequest / ExplainRequest valid…
Browse files Browse the repository at this point in the history
…ation for null and/or empty id/type (#35314)

Closes #35297

(cherry picked from commit a467a81)
  • Loading branch information
vladimirdolzhenko committed Nov 7, 2018
1 parent edd8277 commit 4ecb79b
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 12 deletions.
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.support.replication.ReplicatedWriteRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.uid.Versions;
Expand Down Expand Up @@ -83,10 +84,10 @@ public DeleteRequest(String index, String type, String id) {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validate();
if (type == null) {
if (Strings.isEmpty(type)) {
validationException = addValidationError("type is missing", validationException);
}
if (id == null) {
if (Strings.isEmpty(id)) {
validationException = addValidationError("id is missing", validationException);
}
if (!versionType.validateVersionForWrites(version)) {
Expand Down
Expand Up @@ -34,6 +34,8 @@

import java.io.IOException;

import static org.elasticsearch.action.ValidateActions.addValidationError;

/**
* Explain request encapsulating the explain query and document identifier to get an explanation for.
*/
Expand Down Expand Up @@ -152,11 +154,11 @@ public ExplainRequest filteringAlias(AliasFilter filteringAlias) {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validateNonNullIndex();
if (type == null) {
validationException = ValidateActions.addValidationError("type is missing", validationException);
if (Strings.isEmpty(type)) {
validationException = addValidationError("type is missing", validationException);
}
if (id == null) {
validationException = ValidateActions.addValidationError("id is missing", validationException);
if (Strings.isEmpty(id)) {
validationException = addValidationError("id is missing", validationException);
}
if (query == null) {
validationException = ValidateActions.addValidationError("query is missing", validationException);
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.single.shard.SingleShardRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.uid.Versions;
Expand All @@ -32,6 +33,8 @@

import java.io.IOException;

import static org.elasticsearch.action.ValidateActions.addValidationError;

/**
* A request to get a document (its source) from an index based on its type (optional) and id. Best created using
* {@link org.elasticsearch.client.Requests#getRequest(String)}.
Expand Down Expand Up @@ -91,11 +94,11 @@ public GetRequest(String index, String type, String id) {
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validateNonNullIndex();
if (type == null) {
validationException = ValidateActions.addValidationError("type is missing", validationException);
if (Strings.isEmpty(type)) {
validationException = addValidationError("type is missing", validationException);
}
if (id == null) {
validationException = ValidateActions.addValidationError("id is missing", validationException);
if (Strings.isEmpty(id)) {
validationException = addValidationError("id is missing", validationException);
}
if (!versionType.validateVersionForReads(version)) {
validationException = ValidateActions.addValidationError("illegal version value [" + version + "] for version type [" + versionType.name() + "]",
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.action.support.replication.ReplicationRequest;
import org.elasticsearch.action.support.single.instance.InstanceShardOperationRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.uid.Versions;
Expand Down Expand Up @@ -106,10 +107,10 @@ public ActionRequestValidationException validate() {
if(upsertRequest != null && upsertRequest.version() != Versions.MATCH_ANY) {
validationException = addValidationError("can't provide version in upsert request", validationException);
}
if (type == null) {
if (Strings.isEmpty(type)) {
validationException = addValidationError("type is missing", validationException);
}
if (id == null) {
if (Strings.isEmpty(id)) {
validationException = addValidationError("id is missing", validationException);
}

Expand Down
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.delete;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;

public class DeleteRequestTests extends ESTestCase {

public void testValidation() {
{
final DeleteRequest request = new DeleteRequest("index4", "_doc", "0");
final ActionRequestValidationException validate = request.validate();

assertThat(validate, nullValue());
}

{
final DeleteRequest request = new DeleteRequest("index4", randomBoolean() ? "" : null, randomBoolean() ? "" : null);
final ActionRequestValidationException validate = request.validate();

assertThat(validate, not(nullValue()));
assertThat(validate.validationErrors(), hasItems("type is missing", "id is missing"));
}
}
}
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.action.explain;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
Expand All @@ -35,6 +36,10 @@
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;

public class ExplainRequestTests extends ESTestCase {
private NamedWriteableRegistry namedWriteableRegistry;

Expand Down Expand Up @@ -70,4 +75,24 @@ public void testSerialize() throws IOException {
}
}
}

public void testValidation() {
{
final ExplainRequest request = new ExplainRequest("index4", "_doc", "0");
request.query(QueryBuilders.termQuery("field", "value"));

final ActionRequestValidationException validate = request.validate();

assertThat(validate, nullValue());
}

{
final ExplainRequest request = new ExplainRequest("index4", randomBoolean() ? "" : null, randomBoolean() ? "" : null);
request.query(QueryBuilders.termQuery("field", "value"));
final ActionRequestValidationException validate = request.validate();

assertThat(validate, not(nullValue()));
assertThat(validate.validationErrors(), hasItems("type is missing", "id is missing"));
}
}
}
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.get;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;

public class GetRequestTests extends ESTestCase {

public void testValidation() {
{
final GetRequest request = new GetRequest("index4", "_doc", "0");
final ActionRequestValidationException validate = request.validate();

assertThat(validate, nullValue());
}

{
final GetRequest request = new GetRequest("index4", randomBoolean() ? "" : null, randomBoolean() ? "" : null);
final ActionRequestValidationException validate = request.validate();

assertThat(validate, not(nullValue()));
assertThat(validate.validationErrors(), hasItems("type is missing", "id is missing"));
}
}
}
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.action.update;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
Expand Down Expand Up @@ -61,6 +62,9 @@
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
import static org.elasticsearch.script.MockScriptEngine.mockInlineScript;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -513,6 +517,25 @@ public void testToValidateUpsertRequestWithVersion() {
assertThat(updateRequest.validate().validationErrors(), contains("can't provide version in upsert request"));
}

public void testValidate() {
{
UpdateRequest request = new UpdateRequest("index", "type", "id");
request.doc("{}", XContentType.JSON);
ActionRequestValidationException validate = request.validate();

assertThat(validate, nullValue());
}

{
UpdateRequest request = new UpdateRequest("index", randomBoolean() ? "" : null, randomBoolean() ? "" : null);
request.doc("{}", XContentType.JSON);
ActionRequestValidationException validate = request.validate();

assertThat(validate, not(nullValue()));
assertThat(validate.validationErrors(), hasItems("type is missing", "id is missing"));
}
}

public void testParentAndRoutingExtraction() throws Exception {
GetResult getResult = new GetResult("test", "type", "1", 0, false, null, null);
IndexRequest indexRequest = new IndexRequest("test", "type", "1");
Expand Down

0 comments on commit 4ecb79b

Please sign in to comment.