-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Validate op_type for _create
#27483
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
Validate op_type for _create
#27483
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.elasticsearch.rest.action.RestStatusToXContentListener; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Locale; | ||
|
|
||
| import static org.elasticsearch.rest.RestRequest.Method.POST; | ||
| import static org.elasticsearch.rest.RestRequest.Method.PUT; | ||
|
|
@@ -63,9 +64,16 @@ public String getName() { | |
|
|
||
| @Override | ||
| public RestChannelConsumer prepareRequest(RestRequest request, final NodeClient client) throws IOException { | ||
| validateOpType(request.params().get("op_type")); | ||
| request.params().put("op_type", "create"); | ||
| return RestIndexAction.this.prepareRequest(request, client); | ||
| } | ||
|
|
||
| public void validateOpType(String opType) { | ||
| if (null != opType && !"create".equals(opType.toLowerCase(Locale.US))) { | ||
|
||
| throw new IllegalArgumentException("opType must be 'create', found: [" + opType + "]"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * 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.rest.action.document; | ||
|
|
||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.rest.RestController; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| public class RestIndexActionTests extends ESTestCase { | ||
|
|
||
| public void testCreateOpTypeValidation() throws Exception { | ||
| Settings settings = settings(Version.CURRENT).build(); | ||
| RestIndexAction.CreateHandler create = new RestIndexAction(settings, mock(RestController.class)).new CreateHandler(settings); | ||
|
|
||
| String opType = randomFrom("CREATE", null); | ||
| create.validateOpType(opType); | ||
|
|
||
| String illegalOpType = randomFrom("index", "unknown", ""); | ||
| IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> create.validateOpType(illegalOpType)); | ||
| assertThat(e.getMessage(), equalTo("opType must be 'create', found: [" + illegalOpType + "]")); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe make this static and pkg private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@s1monw what about private (or pkg private) and final?
It cannot be set to
staticas the inner classCreateHandleritself is not static.