Skip to content

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

Closed
@br3no

Description

@br3no

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions