-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
Decouple XContentBuilder from BytesReference #28972
Decouple XContentBuilder from BytesReference #28972
Conversation
This commit removes all mentions of `BytesReference` from `XContentBuilder`. This is needed so that we can completely decouple the XContent code and move it into its own dependency. While this change appears large, it is due to two main changes, moving `.bytes()` and `.string()` out of XContentBuilder itself into static methods `BytesReference.bytes` and `Strings.toString` respectively. The rest of the change is code reacting to these changes (the majority of it in tests). Relates to elastic#28504
Pinging @elastic/es-core-infra |
…ields We assert right below it that the XContent equivalent are equal, and the byte array instances are not going to be the same exact thing regardless.
@@ -38,6 +41,16 @@ | |||
|
|||
private Integer hash = null; // we cache the hash of this reference since it can be quite costly to re-calculated it | |||
|
|||
public static BytesReference bytes(XContentBuilder xContentBuilder) { |
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.
Probably worth Javadoc.
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.
Including the warning that it closes the builder.
@@ -173,6 +170,13 @@ public XContentType contentType() { | |||
return generator.contentType(); | |||
} | |||
|
|||
/** | |||
* @return the output stream to which the built object is being written |
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.
I think it'd be worth warning that it is dangerous to modify the steam and that it might not be complete unless the builder is closed. Actually, I wonder if you can have this close the builder too?
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.
I mean, that is kind of weird, but maybe not too weird. Because this is only useful for byte array style output streams. Maybe if this doesn't close the stream it can assert that it is closed? I'm not sure how hard that is.
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.
There isn't a way that I see to assert that the OutputStream is already closed (other than closing it). I'll add documentation about the modification though.
@@ -81,7 +81,6 @@ private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws | |||
parsedGetResponse = GetResponse.fromXContent(parser); | |||
assertNull(parser.nextToken()); | |||
} | |||
assertEquals(expectedGetResponse, parsedGetResponse); |
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.
Can you explain why we need this line?
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.
Or, rather, need to remove it?
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.
That was a mistake, I will re-add this.
xContentBuilder.field("test1", "value1"); | ||
xContentBuilder.endObject(); | ||
assertThat(xContentBuilder.bytes().utf8ToString(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}")); | ||
assertThat(BytesReference.bytes(xContentBuilder).utf8ToString(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}")); |
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.
These should probably move into xcontent's tests rather than server's tests so it won't be able to use this.
But that can be another PR :).
It does make me wonder if string
should be implemented in the xcontent artifact without Lucene.
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.
I think these should use Strings.toString(xContentBuilder)
at least.
@@ -69,18 +70,18 @@ public void testSingleFieldObject() throws IOException { | |||
} | |||
|
|||
static void assertXContentBuilderAsString(final XContentBuilder expected, final XContentBuilder actual) { | |||
assertThat(actual.bytes().utf8ToString(), is(expected.bytes().utf8ToString())); | |||
assertThat(BytesReference.bytes(actual).utf8ToString(), is(BytesReference.bytes(expected).utf8ToString())); |
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.
I think these should use Strings.toString(actual)
Thanks for taking a look @nik9000, I pushed commits addressing your comments |
* Decouple XContentBuilder from BytesReference This commit removes all mentions of `BytesReference` from `XContentBuilder`. This is needed so that we can completely decouple the XContent code and move it into its own dependency. While this change appears large, it is due to two main changes, moving `.bytes()` and `.string()` out of XContentBuilder itself into static methods `BytesReference.bytes` and `Strings.toString` respectively. The rest of the change is code reacting to these changes (the majority of it in tests). Relates to #28504
Changes made in elastic#28972 seems to have changed some assumptions about how SMILE and CBOR write byte[] values and how this is tested. This changes the generation of the randomized DocumentField values back to BytesArray while expecting the JSON and YAML deserialisation to produce Base64 encoded strings and SMILE and CBOR to parse back BytesArray instances. Closes elastic#29080
Changes made in #28972 seems to have changed some assumptions about how SMILE and CBOR write byte[] values and how this is tested. This changes the generation of the randomized DocumentField values back to BytesArray while expecting the JSON and YAML deserialisation to produce Base64 encoded strings and SMILE and CBOR to parse back BytesArray instances. Closes #29080
Changes made in #28972 seems to have changed some assumptions about how SMILE and CBOR write byte[] values and how this is tested. This changes the generation of the randomized DocumentField values back to BytesArray while expecting the JSON and YAML deserialisation to produce Base64 encoded strings and SMILE and CBOR to parse back BytesArray instances. Closes #29080
* es/6.x: (89 commits) Clarify requirements of strict date formats. (#29090) Clarify that dates are always rendered as strings. (#29093) [Docs] Fix link to Grok patterns (#29088) Fix starting on Windows from another drive (#29086) Use removeTask instead of finishTask in PersistentTasksClusterService (#29055) Added minimal docs for reindex api in java-api docs Allow overriding JVM options in Windows service (#29044) Clarify how to set compiler and runtime JDKs (#29101) Fix Parsing Bug with Update By Query for Stored Scripts (#29039) TEST: write ops should execute under shard permit (#28966) [DOCS] Add X-Pack upgrade details (#29038) Revert "Improve error message for installing plugin (#28298)" Docs: HighLevelRestClient#exists (#29073) Validate regular expressions in dynamic templates. (#29013) [Tests] Fix GetResultTests and DocumentFieldTests failures (#29083) Reenable LiveVersionMapTests.testRamBytesUsed on Java 9. (#29063) Mute failing GetResultTests and DocumentFieldTests [Docs] Fix Java Api index administration usage (#28260) Improve error message for installing plugin (#28298) Decouple XContentBuilder from BytesReference (#28972) ...
This commit removes all mentions of
BytesReference
fromXContentBuilder
.This is needed so that we can completely decouple the XContent code and move it
into its own dependency.
While this change appears large, it is due to two main changes, moving
.bytes()
and.string()
out of XContentBuilder itself into static methodsBytesReference.bytes
andStrings.toString
respectively. The rest of thechange is code reacting to these changes (the majority of it in tests).
Relates to #28504