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

Error in JsonpUtils when copying arrays from a JSON into the generator #591

Closed
br3no opened this issue Jun 5, 2023 · 1 comment · Fixed by #597
Closed

Error in JsonpUtils when copying arrays from a JSON into the generator #591

br3no opened this issue Jun 5, 2023 · 1 comment · Fixed by #597

Comments

@br3no
Copy link

br3no commented Jun 5, 2023

Java API client version

8.8.0

Java version

19

Elasticsearch Version

8.8.0

Problem description

This line introduced in the context of #508 seems to be mistaken.

            case START_ARRAY:
                generator.writeStartArray();
         --->   generator.writeStartObject();
                while ((event = parser.next()) != Event.END_ARRAY) {
                    copy(parser, generator, event);
                }

The extra opening of a { when writing an array breaks the serialization of objects containing arrays copied using this method. The following code snippet demonstrates how to reproduce the error:

...
try {
      javaClient.search(
          s -> s.index("logs").query(q -> q.queryString(qb -> qb.query("message:(log"))),
          JsonData.class);
    } catch (ElasticsearchException e) {
      System.out.println("Boom! " + e.response().toString());
    }
...

The query contains a syntax error (unclosed parens). The code path within the server goes through the pointed out line above. The response object wrapped in the ElasticsearchException seems to be copied using this method. Trying to call toString on it leads to the following error:

Exception in thread "main" jakarta.json.stream.JsonGenerationException: Illegal method during JSON generation, not valid in current context IN_OBJECT
	at org.eclipse.parsson.JsonGeneratorImpl.writeStartObject(JsonGeneratorImpl.java:121)
	at co.elastic.clients.json.JsonpUtils.copy(JsonpUtils.java:167)
	at co.elastic.clients.json.JsonpUtils.copy(JsonpUtils.java:180)
	at co.elastic.clients.json.JsonpUtils.copy(JsonpUtils.java:157)
	at co.elastic.clients.json.jackson.JacksonJsonBuffer.serialize(JacksonJsonBuffer.java:98)
	at co.elastic.clients.elasticsearch._types.ErrorCause.serializeInternal(ErrorCause.java:168)
	at co.elastic.clients.elasticsearch._types.ErrorCause.serialize(ErrorCause.java:160)
	at co.elastic.clients.elasticsearch._types.ErrorResponse.serializeInternal(ErrorResponse.java:96)
	at co.elastic.clients.elasticsearch._types.ErrorResponse.serialize(ErrorResponse.java:89)
	at co.elastic.clients.json.JsonpUtils.toString(JsonpUtils.java:409)
	at co.elastic.clients.json.JsonpUtils.toString(JsonpUtils.java:346)
	at co.elastic.clients.elasticsearch._types.ErrorResponse.toString(ErrorResponse.java:105)
	at LuceneParseBug.main(LuceneParseBug.java:39)
	Suppressed: jakarta.json.stream.JsonGenerationException: Generating incomplete JSON
		at org.eclipse.parsson.JsonGeneratorImpl.close(JsonGeneratorImpl.java:519)
		at co.elastic.clients.json.JsonpUtils.toString(JsonpUtils.java:408)
		... 3 more
@swallez
Copy link
Member

swallez commented Jun 15, 2023

Thanks for the catch and the analysis! This is fixed (with an added test) in #597. This will be included in version 8.8.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants