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

Add a way to validate XContent rendering produces valid JSON/YAML #13157

Closed
dakrone opened this issue Aug 27, 2015 · 4 comments
Closed

Add a way to validate XContent rendering produces valid JSON/YAML #13157

dakrone opened this issue Aug 27, 2015 · 4 comments
Labels
:Core/Infra/REST API REST infrastructure and utilities >enhancement help wanted adoptme Team:Core/Infra Meta label for core/infra team >test Issues or PRs that are addressing/adding tests

Comments

@dakrone
Copy link
Member

dakrone commented Aug 27, 2015

It's common in tests to have a method that tests serialization and deserialization of the actual Java objects, however, it's quite possible to make a mistake in the toXContent method that does not produce either valid JSON or YAML. Right now the only real way to do this is with a REST test (other than a lot of string comparison in a unit test), which is a lot of overhead for something like this.

It would be great if we could have some kind of AssertingToXContent wrapper that could test this to ensure we don't ever generate JSON like:

{ : { "bar": "foo"} }
@clintongormley clintongormley added the >test Issues or PRs that are addressing/adding tests label Aug 28, 2015
@javanna
Copy link
Member

javanna commented Sep 14, 2016

As part of the search refactoring, we introduced quite some tests around query / search request serialization. As part of those we generate a random builder, call toXContent against it, parse it, then check that the generated query is equivalent to the initial one. I think this achieves the goal without string comparisons as we compare only the java objects through the equals method (that are also separately tested).

This is not possible though in all cases as some objects implement ToXContent although we have no parsing code for them (e.g. response objects that only get printed out). IN that case we should simply call toXContent and check its output? Or call it before and after serialization and check that it's valid json and equivalent on both ends?

I am interested in the AssertingToXContent idea. How would that work exactly? Wouldn't we need to write specific tests for each class that implements ToXContent? Can you elaborate @dakrone ?

@javanna
Copy link
Member

javanna commented Dec 16, 2016

I am digging the AssertingToXContent idea, but I think it is tricky unless the ToXContent contract is better defined. See #16347. In many cases the output of toXContent is not a valid object. Wrapping it into a new anonymous object would make it valid, unless it starts already its own anonymous object. In other words I don't think this is feasible at the moment. Thoughts @dakrone ?

@colings86 colings86 added the :Core/Infra/REST API REST infrastructure and utilities label Apr 24, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@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
Copy link
Member

jaymode commented Dec 11, 2020

Given the lack of further discussion and ideas for this, I am going to close this issue as I don’t believe the core/infra team will prioritize and have bandwidth to work on this anytime soon.

@jaymode jaymode closed this as completed Dec 11, 2020
@jaymode jaymode removed the needs:triage Requires assignment of a team area label label Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/REST API REST infrastructure and utilities >enhancement help wanted adoptme Team:Core/Infra Meta label for core/infra team >test Issues or PRs that are addressing/adding tests
Projects
None yet
Development

No branches or pull requests

7 participants