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

Make sure PutMappingRequest accepts content types other than JSON. #37720

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -254,7 +254,7 @@ public static XContentBuilder buildFromSimplifiedDef(String type, Object... sour
* The mapping source definition.
*/
public PutMappingRequest source(XContentBuilder mappingBuilder) {
return source(Strings.toString(mappingBuilder), mappingBuilder.contentType());
return source(BytesReference.bytes(mappingBuilder), mappingBuilder.contentType());
}

/**
Expand All @@ -264,7 +264,7 @@ public PutMappingRequest source(Map<String, ?> mappingSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(mappingSource);
return source(Strings.toString(builder), XContentType.JSON);
return source(BytesReference.bytes(builder), builder.contentType());
Copy link
Member

Choose a reason for hiding this comment

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

This change does not hurt, but isn't it ok anyways given that we create the builder ourselves and we use json as content-type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, this didn't contribute to the bug, but I just found the code easier to follow if all variants called into #source(BytesReference source, XContentType xContentType).

} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + mappingSource + "]", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
Expand Down Expand Up @@ -135,20 +134,8 @@ private static PutMappingRequest createTestItem() throws IOException {

String type = randomAlphaOfLength(5);
request.type(type);
request.source(randomMapping());
request.source(RandomCreateIndexGenerator.randomMapping());

return request;
}

private static XContentBuilder randomMapping() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();

if (randomBoolean()) {
RandomCreateIndexGenerator.randomMappingFields(builder, true);
}

builder.endObject();
return builder;
}
}