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

Clarify ToXContent contract #16347

Closed
nik9000 opened this issue Feb 1, 2016 · 15 comments
Closed

Clarify ToXContent contract #16347

nik9000 opened this issue Feb 1, 2016 · 15 comments
Labels
:Core/Infra/Core Core issues without another label >enhancement help wanted adoptme Team:Core/Infra Meta label for core/infra team

Comments

@nik9000
Copy link
Member

nik9000 commented Feb 1, 2016

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.

@nik9000 nik9000 added the discuss label Feb 1, 2016
@bleskes
Copy link
Contributor

bleskes commented Feb 12, 2016

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...

@bleskes bleskes removed the discuss label Feb 12, 2016
@nik9000
Copy link
Member Author

nik9000 commented Feb 12, 2016

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!

@javanna
Copy link
Member

javanna commented Feb 12, 2016

Would be great to clarify and clean up this contract for sure, in this case using a class that extends ToXContent doesn't say enough, you have to look at the implementation to see if you have to open a new object yourself or not.

@javanna
Copy link
Member

javanna commented Dec 16, 2016

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 toXContent methods (in separate PRs would be wise). I see that most of the objects don't open a start object, and output essentially fragments that are not valid without an ancestor. On the other hand, our ActionResponses should always print out a complete object without the need to start and end the object externally, which some REST actions do and some others do not.

How about having ToXContent for complete objects and ToXContentFragment for fragments?

@javanna javanna changed the title ToXContent and ElasticsearchException Clarify ToXContent contract Dec 16, 2016
@javanna
Copy link
Member

javanna commented Dec 16, 2016

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.

javanna added a commit that referenced this issue Jan 7, 2017
`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
@javanna
Copy link
Member

javanna commented Jan 7, 2017

#22387 is in, so we now have ToXContent for fragments and ToXContentObject for complete objects. What is left to do is moving more classes over from ToXContent to ToXContentObject. That's why I'm keeping this issue open for now.

@andyb-elastic
Copy link
Contributor

@javanna would it be helpful to have another person on this? (I see there are already a couple assignees)

@javanna
Copy link
Member

javanna commented Aug 8, 2017

@andy-elastic yes it would be helpful, @colings86 made quite some progress on this recently, see #25771

@colings86 colings86 removed their assignment Nov 3, 2017
@colings86 colings86 added the :Core/Infra/Core Core issues without another label label Apr 24, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@cbuescher
Copy link
Member

@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):

Screenshot 2019-04-09 at 09 59 33
Screenshot 2019-04-09 at 09 59 47

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?

@javanna
Copy link
Member

javanna commented Apr 9, 2019

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?

+1

cbuescher pushed a commit to cbuescher/elasticsearch that referenced this issue Apr 9, 2019
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
cbuescher pushed a commit that referenced this issue Apr 15, 2019
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
cbuescher pushed a commit that referenced this issue Apr 15, 2019
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
gurkankaymak pushed a commit to gurkankaymak/elasticsearch that referenced this issue May 27, 2019
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
@tlrx tlrx removed their assignment Jul 4, 2019
@javanna javanna removed their assignment Aug 15, 2019
@pugnascotia
Copy link
Contributor

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?

@javanna / @cbuescher was a refactoring issue ever opened for the above?

@javanna
Copy link
Member

javanna commented Sep 12, 2019

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.

@rjernst rjernst added the Team:Core/Infra Meta label for core/infra team label May 4, 2020
@rjernst rjernst added the needs:triage Requires assignment of a team area label label Dec 3, 2020
@jaymode jaymode added help wanted adoptme and removed needs:triage Requires assignment of a team area label labels Dec 14, 2020
@cbuescher cbuescher removed their assignment Dec 20, 2023
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@thecoop
Copy link
Member

thecoop commented Oct 25, 2024

Given this has been open for several years, and the core change (ToXContentFragment) is in, I'm closing this. Any remaining classes with ambiguities can be dealt with on an individual basis.

@thecoop thecoop closed this as completed Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label >enhancement help wanted adoptme Team:Core/Infra Meta label for core/infra team
Projects
None yet
Development

No branches or pull requests