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

Optionally require a valid content type for all rest requests with content #22691

Merged
merged 43 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e4b2d08
Require a valid content type for all rest requests with content
jaymode Jan 12, 2017
c53e296
start addressing review feedback
jaymode Jan 20, 2017
9448729
review updates for index templates
jaymode Jan 20, 2017
7a267ff
use Objects.requireNonNull for pipeline requests
jaymode Jan 20, 2017
3e78549
deprecate loadFromSource without xcontenttype
jaymode Jan 20, 2017
63ab38a
more deprecations :)
jaymode Jan 20, 2017
99f551f
cleanup deprecated usages and remove source methods without xcontenttype
jaymode Jan 23, 2017
76c9f5e
Merge branch 'master' into content_type
jaymode Jan 23, 2017
d4f0e13
more updates and add a sendErrorResponse method
jaymode Jan 23, 2017
b5d9e6f
fix line length
jaymode Jan 23, 2017
221f797
fix percolatequerybuilder serialization
jaymode Jan 23, 2017
f43bf3b
add a BWC compatibility layer that defaults to auto detection fallback
jaymode Jan 24, 2017
323e634
Merge branch 'master' into content_type
jaymode Jan 24, 2017
c6be540
serialization tests and updates
jaymode Jan 24, 2017
8072a05
Merge branch 'master' into content_type
jaymode Jan 26, 2017
0be9af3
review round 2 updates
jaymode Jan 26, 2017
84db4dd
change onOrAfter to after for now
jaymode Jan 26, 2017
2391d2d
rollback change to bwc test version
jaymode Jan 26, 2017
19cdb27
put mapping request can take bytes so we can pass from rest layer
jaymode Jan 26, 2017
0264168
fix reindex parsing of content-type
jaymode Jan 26, 2017
b18cae7
docs updates
jaymode Jan 26, 2017
7e94108
fix index request serialization for reindex
jaymode Jan 26, 2017
65bfe9e
make reindex work with the removal of the default value in IndexRequest
jaymode Jan 26, 2017
1d1d47e
add content type message to test
jaymode Jan 26, 2017
7357e4e
fix testing broken mapping to use valid json
jaymode Jan 26, 2017
88a1a56
ensure we consume the source_content_type parameter before checking i…
jaymode Jan 26, 2017
842806a
handle backwards compatibility in rest test client
jaymode Jan 27, 2017
26c6df8
modify rest test framework to ignore deprecated missing source_conten…
jaymode Jan 27, 2017
a441458
address more feedback
jaymode Jan 27, 2017
4d37b1b
Merge branch 'master' into content_type
jaymode Jan 27, 2017
e208082
address non-reindex comments
jaymode Jan 30, 2017
7b89522
more feedback changes
jaymode Jan 30, 2017
d2a3c6f
Merge branch 'master' into content_type
jaymode Jan 30, 2017
5270a44
add comment
jaymode Jan 30, 2017
e4f46d3
use writeOptionalWriteable the right way
jaymode Jan 30, 2017
4bd706a
use xcontent type to avoid deprecated method
jaymode Jan 30, 2017
dd19f71
add missing setting registration
jaymode Feb 1, 2017
9f862a6
Merge branch 'master' into content_type
jaymode Feb 1, 2017
4699539
Merge branch 'master' into content_type
jaymode Feb 1, 2017
fd62a34
use 5.3.0-snapshot for rolling upgrade
jaymode Feb 1, 2017
9bbdd71
Merge branch 'master' into content_type
jaymode Feb 2, 2017
ed3ee08
Merge branch 'master' into content_type
jaymode Feb 2, 2017
159b450
add status
jaymode Feb 2, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.plugin.noop.NoopPlugin;
import org.elasticsearch.plugin.noop.action.bulk.NoopBulkAction;
Expand Down Expand Up @@ -80,7 +81,7 @@ public TransportBulkRequestExecutor(TransportClient client, String indexName, St
public boolean bulkIndex(List<String> bulkData) {
NoopBulkRequestBuilder builder = NoopBulkAction.INSTANCE.newRequestBuilder(client);
for (String bulkItem : bulkData) {
builder.add(new IndexRequest(indexName, typeName).source(bulkItem.getBytes(StandardCharsets.UTF_8)));
builder.add(new IndexRequest(indexName, typeName).source(bulkItem.getBytes(StandardCharsets.UTF_8), XContentType.JSON));
}
BulkResponse bulkResponse;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;

public class NoopBulkRequestBuilder extends ActionRequestBuilder<BulkRequest, BulkResponse, NoopBulkRequestBuilder>
implements WriteRequestBuilder<NoopBulkRequestBuilder> {
Expand Down Expand Up @@ -95,17 +96,17 @@ public NoopBulkRequestBuilder add(UpdateRequestBuilder request) {
/**
* Adds a framed data in binary format
*/
public NoopBulkRequestBuilder add(byte[] data, int from, int length) throws Exception {
request.add(data, from, length, null, null);
public NoopBulkRequestBuilder add(byte[] data, int from, int length, XContentType xContentType) throws Exception {
request.add(data, from, length, null, null, xContentType);
return this;
}

/**
* Adds a framed data in binary format
*/
public NoopBulkRequestBuilder add(byte[] data, int from, int length, @Nullable String defaultIndex, @Nullable String defaultType)
throws Exception {
request.add(data, from, length, defaultIndex, defaultType);
public NoopBulkRequestBuilder add(byte[] data, int from, int length, @Nullable String defaultIndex, @Nullable String defaultType,
XContentType xContentType) throws Exception {
request.add(data, from, length, defaultIndex, defaultType, xContentType);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
}
bulkRequest.timeout(request.paramAsTime("timeout", BulkShardRequest.DEFAULT_TIMEOUT));
bulkRequest.setRefreshPolicy(request.param("refresh"));
bulkRequest.add(request.content(), defaultIndex, defaultType, defaultRouting, defaultFields, null, defaultPipeline, null, true);
bulkRequest.add(request.content(), defaultIndex, defaultType, defaultRouting, defaultFields, null, defaultPipeline, null, true,
request.getXContentType());

// short circuit the call to the transport layer
return channel -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package org.elasticsearch.action.admin.cluster.storedscripts;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

Expand All @@ -35,6 +37,7 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
private String id;
private String scriptLang;
private BytesReference script;
private XContentType xContentType;

public PutStoredScriptRequest() {
super();
Expand Down Expand Up @@ -92,17 +95,31 @@ public BytesReference script() {
return script;
}

public XContentType xContentType() {
return xContentType;
}

@Deprecated
Copy link
Member

Choose a reason for hiding this comment

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

shall we add javadocs with the motivation for the deprecation and what it is replaced with?
I assume that these methods can be removed in master once this PR is backported to 5.x?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes this can go away in master after backporting.

public PutStoredScriptRequest script(BytesReference source) {
this.script = source;
return this;
}

public PutStoredScriptRequest script(BytesReference source, XContentType xContentType) {
Copy link
Member

Choose a reason for hiding this comment

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

does it still make sense to store in the cluster state whatever format was sent? Maybe we should rather translate to json in the request like we do with mappings?

Copy link
Member

Choose a reason for hiding this comment

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

forget about this comment, scripts are stored in json format, the conversion happens in ScriptMetaData, which is ok I think. we just seem to be sending out via transport whatever format we have, but the master will do the conversion before storing it.

Copy link
Member

Choose a reason for hiding this comment

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

we may want to simplify things and move this conversion so we don't need to send over the content_type as part of this request, but up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I'll leave this one as is so we do not do a double parse of the script or move the ScriptMetaData logic into the request class (see the different ways stored scripts can be defined comment in the parseStoredScript method).

Copy link
Member

Choose a reason for hiding this comment

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

fine with me!

this.script = source;
this.xContentType = xContentType;
return this;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
scriptLang = in.readString();
id = in.readOptionalString();
script = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0_UNRELEASED) && in.readBoolean()) {
xContentType = XContentType.readFrom(in);
}
}

@Override
Expand All @@ -111,13 +128,25 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(scriptLang);
out.writeOptionalString(id);
out.writeBytesReference(script);
if (out.getVersion().onOrAfter(Version.V_5_3_0_UNRELEASED)) {
if (xContentType == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
xContentType.writeTo(out);
Copy link
Member

Choose a reason for hiding this comment

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

can you fix the indentation here?

}
}
}

@Override
public String toString() {
String sSource = "_na_";
try {
sSource = XContentHelper.convertToJson(script, false);
if (xContentType == null) {
Copy link
Member

Choose a reason for hiding this comment

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

can you clarify that this if is only needed for bw comp and should go away once the bw comp layer is removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm actually going to remove this and require a non-null xContentType.

sSource = XContentHelper.convertToJson(script, false);
} else {
sSource = XContentHelper.convertToJson(script, false, xContentType);
}
} catch (Exception e) {
// ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType;

public class PutStoredScriptRequestBuilder extends AcknowledgedRequestBuilder<PutStoredScriptRequest,
PutStoredScriptResponse, PutStoredScriptRequestBuilder> {
Expand All @@ -40,9 +41,14 @@ public PutStoredScriptRequestBuilder setId(String id) {
return this;
}

@Deprecated
Copy link
Member

Choose a reason for hiding this comment

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

javadocs?

public PutStoredScriptRequestBuilder setSource(BytesReference source) {
request.script(source);
return this;
}

public PutStoredScriptRequestBuilder setSource(BytesReference source, XContentType xContentType) {
request.script(source, xContentType);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,20 @@ public CreateIndexRequest alias(Alias alias) {

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #source(String, XContentType)}
*/
@Deprecated
public CreateIndexRequest source(String source) {
return source(source.getBytes(StandardCharsets.UTF_8));
Copy link
Member

Choose a reason for hiding this comment

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

here too, let's change it to create new BytesArray directly?

}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(String source, XContentType xContentType) {
return source(source.getBytes(StandardCharsets.UTF_8), xContentType);
Copy link
Member

Choose a reason for hiding this comment

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

maybe we could directly create new BytesArray(source) here instead

}

/**
* Sets the settings and mappings as a single source.
*/
Expand All @@ -345,23 +354,51 @@ public CreateIndexRequest source(XContentBuilder source) {

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #source(byte[], XContentType)}
*/
@Deprecated
public CreateIndexRequest source(byte[] source) {
return source(source, 0, source.length);
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(byte[] source, XContentType xContentType) {
return source(source, 0, source.length, xContentType);
}
Copy link
Member

Choose a reason for hiding this comment

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

what were we doing before? if content type is null we considered the source settings?

Copy link
Member

Choose a reason for hiding this comment

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

I think that we previously supported settings in properties format as part of the create index api request body? Is that it? Oh my....

Copy link
Member Author

Choose a reason for hiding this comment

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

That's correct.


/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #source(byte[], int, int, XContentType)}
*/
@Deprecated
public CreateIndexRequest source(byte[] source, int offset, int length) {
return source(new BytesArray(source, offset, length));
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(byte[] source, int offset, int length, XContentType xContentType) {
return source(new BytesArray(source, offset, length), xContentType);
}

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #source(BytesReference, XContentType)}
*/
@Deprecated
public CreateIndexRequest source(BytesReference source) {
XContentType xContentType = XContentFactory.xContentType(source);
source(source, xContentType);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequest source(BytesReference source, XContentType xContentType) {
if (xContentType != null) {
source(XContentHelper.convertToMap(source, false).v2());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;

import java.util.Map;

Expand Down Expand Up @@ -191,7 +192,9 @@ public CreateIndexRequestBuilder addAlias(Alias alias) {

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #setSource(String, XContentType)}
*/
@Deprecated
public CreateIndexRequestBuilder setSource(String source) {
request.source(source);
return this;
Expand All @@ -200,6 +203,16 @@ public CreateIndexRequestBuilder setSource(String source) {
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(String source, XContentType xContentType) {
request.source(source, xContentType);
return this;
}

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #setSource(BytesReference, XContentType)}
*/
@Deprecated
public CreateIndexRequestBuilder setSource(BytesReference source) {
request.source(source);
return this;
Expand All @@ -208,6 +221,16 @@ public CreateIndexRequestBuilder setSource(BytesReference source) {
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(BytesReference source, XContentType xContentType) {
request.source(source, xContentType);
return this;
}

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #setSource(byte[], XContentType)}
*/
@Deprecated
public CreateIndexRequestBuilder setSource(byte[] source) {
request.source(source);
return this;
Expand All @@ -216,11 +239,29 @@ public CreateIndexRequestBuilder setSource(byte[] source) {
/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(byte[] source, XContentType xContentType) {
request.source(source, xContentType);
return this;
}

/**
* Sets the settings and mappings as a single source.
* @deprecated use {@link #setSource(byte[], int, int, XContentType)}
*/
@Deprecated
public CreateIndexRequestBuilder setSource(byte[] source, int offset, int length) {
request.source(source, offset, length);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
public CreateIndexRequestBuilder setSource(byte[] source, int offset, int length, XContentType xContentType) {
request.source(source, offset, length, xContentType);
return this;
}

/**
* Sets the settings and mappings as a single source.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@
import com.carrotsearch.hppc.ObjectHashSet;

import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.Index;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -245,7 +250,7 @@ public static XContentBuilder buildFromSimplifiedDef(String type, Object... sour
*/
public PutMappingRequest source(XContentBuilder mappingBuilder) {
try {
return source(mappingBuilder.string());
return source(mappingBuilder.string(), mappingBuilder.contentType());
} catch (IOException e) {
throw new IllegalArgumentException("Failed to build json for mapping request", e);
}
Expand All @@ -259,17 +264,30 @@ public PutMappingRequest source(Map mappingSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(mappingSource);
return source(builder.string());
return source(builder.string(), XContentType.JSON);
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + mappingSource + "]", e);
}
}

/**
* The mapping source definition.
* @deprecated use {@link #source(String, XContentType)}
*/
@Deprecated
public PutMappingRequest source(String mappingSource) {
this.source = mappingSource;
return source(mappingSource, XContentFactory.xContentType(mappingSource));
Copy link
Member

Choose a reason for hiding this comment

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

not something that you changed but maybe something to fix as a followup, we have 4 xContentTypes, but given that source is a string, we only support yaml or json here. I wonder if source should rather be a BytesReference? I never thought we'd support yaml here though :) who knows if anybody is using that format. I also wonder if instead of converting here we should keep around the xContentType and carry it around all the way to DocumentMapperParser#parse where we actually parse it. That may be a bigger effort but sounds cleaner to me.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, the way I suggested to go before is totally different from what I am saying in this second review. That previous direction was probably the most correct but had a high cost (bw comp mainly). It's the only one that doesn't require any conversion, but just parsing in MapperService once we know to content type. We can still go that way if we really want to... but in most of the cases we get json so we may end up complicating things to handle edge cases which is never good.

Copy link
Member

Choose a reason for hiding this comment

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

we should also add tests for all these different content types around mappings, otherwise it's all theories. I can look into it as a followup

}

/**
* The mapping source definition.
*/
public PutMappingRequest source(String mappingSource, XContentType xContentType) {
try {
this.source = XContentHelper.convertToJson(new BytesArray(mappingSource.getBytes(StandardCharsets.UTF_8)), false, xContentType);
} catch (IOException e) {
throw new UncheckedIOException("failed to convert mapping source to json", e);
}
return this;
}

Expand All @@ -290,7 +308,11 @@ public void readFrom(StreamInput in) throws IOException {
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
type = in.readOptionalString();
source = in.readString();
if (in.getVersion().onOrAfter(Version.V_5_3_0_UNRELEASED)) {
source = in.readString();
} else {
source = XContentHelper.convertToJson(new BytesArray(in.readString().getBytes(StandardCharsets.UTF_8)), false);
}
updateAllTypes = in.readBoolean();
readTimeout(in);
concreteIndex = in.readOptionalWriteable(Index::new);
Expand Down
Loading