Skip to content

Commit

Permalink
Fix spurious JSON event emitted by JsonpUtils.copy() (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
swallez authored Jun 15, 2023
1 parent 4c3a13b commit 25e61a5
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public static void copy(JsonParser parser, JsonGenerator generator, JsonParser.E

case START_ARRAY:
generator.writeStartArray();
generator.writeStartObject();
while ((event = parser.next()) != Event.END_ARRAY) {
copy(parser, generator, event);
}
Expand Down
109 changes: 108 additions & 1 deletion java-client/src/test/java/co/elastic/clients/json/JsonpUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
import co.elastic.clients.elasticsearch.security.IndicesPrivileges;
import co.elastic.clients.elasticsearch.security.RoleTemplateScript;
import co.elastic.clients.elasticsearch.security.UserIndicesPrivileges;
import co.elastic.clients.json.JsonpUtils;
import co.elastic.clients.util.AllowForbiddenApis;
import jakarta.json.JsonException;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import org.junit.jupiter.api.Test;

import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -209,6 +210,47 @@ public void testJsonString() {
}
}

@Test
public void testCopy() {
// Tests round-tripping a json document that contains all event types and various kinds of nesting

String json = "{\n" +
" \"p1\": \"str1\",\n" +
" \"p2\": 42,\n" +
" \"p3\": [\"str31\", \"str32\"],\n" +
" \"p4\": {\n" +
" \"p41\": \"str41\",\n" +
" \"p42\": [\"str421\", \"str422\"],\n" +
" \"p43\": {\n" +
" \"p431\": \"str431\"\n" +
" },\n" +
" \"p44\": true,\n" +
" \"p45\": false,\n" +
" \"p46\": 3.14\n" +
" },\n" +
" \"p5\": [{\n" +
" \"p51\": {\n" +
" \"p511\": \"str511\"\n" +
" }\n" +
" }],\n" +
" \"p6\": null\n" +
"}\n";

json = normalizeIndent(json);

JsonProvider provider = JsonpUtils.provider();

JsonParser parser = provider.createParser(new StringReader(json));
StringWriter sw = new StringWriter();
JsonGenerator generator = provider.createGenerator(sw);

JsonpUtils.copy(parser, generator);
parser.close();
generator.close();

assertEquals(json, sw.toString());
}

private static String orNullHelper(Consumer<JsonGenerator> c) {
StringWriter sw = new StringWriter();
JsonGenerator generator = JsonpUtils.provider().createGenerator(sw);
Expand All @@ -221,4 +263,69 @@ private static String orNullHelper(Consumer<JsonGenerator> c) {

return sw.toString();
}

/**
* Normalizes the whitespace and indentation of a JSON string by parsing it and copying it to a string generator.
*/
private static String normalizeIndent(String json) {
JsonParser parser = JsonpUtils.provider().createParser(new StringReader(json));
StringWriter sw = new StringWriter();
JsonGenerator generator = JsonpUtils.provider().createGenerator(sw);

copyAll(parser, generator);

parser.close();
generator.close();
return sw.toString();
}

private static void copyAll(JsonParser parser, JsonGenerator generator) {
while(parser.hasNext()) {
switch (parser.next()) {
case START_OBJECT:
generator.writeStartObject();
break;

case END_OBJECT:
generator.writeEnd();
break;

case START_ARRAY:
generator.writeStartArray();
break;

case END_ARRAY:
generator.writeEnd();
break;

case KEY_NAME:
generator.writeKey(parser.getString());
break;

case VALUE_STRING:
generator.write(parser.getString());
break;

case VALUE_NULL:
generator.writeNull();
break;

case VALUE_TRUE:
generator.write(true);
break;

case VALUE_FALSE:
generator.write(false);
break;

case VALUE_NUMBER:
if (parser.isIntegralNumber()) {
generator.write(parser.getLong());
} else {
generator.write(parser.getBigDecimal());
}
break;
}
}
}
}

0 comments on commit 25e61a5

Please sign in to comment.