-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Clarify ToXContent contract #16347
Comments
This is a good one. The reason why some ToXContent implementations don't call start and end object is that sometime you want to output within another context, where you for example have added some information for context. For an example look at org.elasticsearch.rest.BytesRestResponse#convert where we enrich the top level error. I'm not saying this is the only solution, but this is why it's like that... |
Thanks for clarifying. I think it'd be more clear if we had two interfaces for the different contracts. We frequently use "innerToXContent" when we want to support this. I get that we have a bazillion interfaces already but it'd be nice to know more about the things that implement ToXContent. We could even have consistent tests for them! |
Would be great to clarify and clean up this contract for sure, in this case using a class that extends |
We have been encountering this inconsistency while writing parsing code for the High Level REST client. We could fix it as we go through all the How about having |
Discussed in FixItFriday, we agreed that this needs to be fixed. We are going to differentiate between the two contracts (self-contained objects and fragments) using two different interfaces. |
`ToXContentObject` extends `ToXContent` without adding new methods to it, while allowing to mark classes that output complete xcontent objects to distinguish them from classes that require starting and ending an anonymous object externally. Ideally ToXContent would be renamed to ToXContentFragment, but that would be a huge change in our codebase, hence we simply document the fact that toXContent outputs fragments with no guarantees that the output is valid per se without an external ancestor. Relates to #16347
#22387 is in, so we now have |
@javanna would it be helpful to have another person on this? (I see there are already a couple assignees) |
@andy-elastic yes it would be helpful, @colings86 made quite some progress on this recently, see #25771 |
Pinging @elastic/es-core-infra |
@javanna I was just revisiting this old issue to see what we would need to close it. There's a great amount of classes now implementing ToXContentObject or ToXContentFragment in the code base. Its a bit hard to check what still directly implements "ToXContent" since we cannot completely eliminate the interface (in several places e.g. in generics definitions in Aggregations it makes sense to allow the generic interface), but from a quick glance at the type-hierarchy in my IDE there should be only a couple of classes left that need changing (and some in tests that we can probably ignore): Should be put the remaining classes in a refactoring issues (checkbox list to tick off one by one), work of that list and call it done when we go a majority of the classes cut over? What else would be need to do before we can close this issue? |
+1 |
This change adds either ToXContentObject or ToXContentFragment to classes directly implementing ToXContent currently. This helps in reasoning about whether those implementations output full xcontent object or just fragments. Relates to elastic#16347
This change adds either ToXContentObject or ToXContentFragment to classes directly implementing ToXContent currently. This helps in reasoning about whether those implementations output full xcontent object or just fragments. Relates to #16347
This change adds either ToXContentObject or ToXContentFragment to classes directly implementing ToXContent currently. This helps in reasoning about whether those implementations output full xcontent object or just fragments. Relates to #16347
This change adds either ToXContentObject or ToXContentFragment to classes directly implementing ToXContent currently. This helps in reasoning about whether those implementations output full xcontent object or just fragments. Relates to elastic#16347
@javanna / @cbuescher was a refactoring issue ever opened for the above? |
not that I know. @cbuescher opened a followup PR which you can see above with some changes that were left to make. There are still some but they are not listed in an issue as far as I know. |
Pinging @elastic/es-core-infra (Team:Core/Infra) |
Given this has been open for several years, and the core change ( |
ElasticsearchException implements ToXContent but it doens't look like it works with XContentBuilder#writeValue because ElasticsearchException doesn't output its start and end object markers. Is this a bug? If so we should probably fix it and add more comments around toXContent's contract and probably make some simple way to test that implementers comply with the contract.
The text was updated successfully, but these errors were encountered: