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

[Backport 2.x] Accept null in arrays #692

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Dependencies

### Changed
Allow null values in arrays ([#687](https://github.com/opensearch-project/opensearch-java/pull/687))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,13 @@ public List<T> deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
if (event == Event.START_ARRAY) {
List<T> result = new ArrayList<>();
while ((event = parser.next()) != Event.END_ARRAY) {
JsonpUtils.ensureAccepts(itemDeserializer, parser, event);
result.add(itemDeserializer.deserialize(parser, mapper, event));
// JSON null: add null unless the deserializer can handle it
if (event == Event.VALUE_NULL && !itemDeserializer.accepts(event)) {
result.add(null);
} else {
JsonpUtils.ensureAccepts(itemDeserializer, parser, event);
result.add(itemDeserializer.deserialize(parser, mapper, event));
}
}
return result;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.opensearch.client.opensearch.json;

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.List;
import org.junit.Test;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class JsonpDeserializerBaseTest extends ModelTestCase {

@Test
public void testNullArrayItem() {

String json = "[\"a\", null, \"c\"]";

// Types that don't accept null events should end up as null values in the list
{
JsonpDeserializer<String> stringDeser = JsonpDeserializer.stringDeserializer();
assertFalse(stringDeser.accepts(JsonParser.Event.VALUE_NULL));

JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));

List<String> stringList = JsonpDeserializer.arrayDeserializer(stringDeser).deserialize(parser, mapper);
assertEquals("a", stringList.get(0));
assertNull(stringList.get(1));
assertEquals("c", stringList.get(2));
}

// Types that do accept null events should end up as their null representation
{
assertTrue(FieldValue._DESERIALIZER.accepts(JsonParser.Event.VALUE_NULL));

JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
List<FieldValue> valueList = JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER).deserialize(parser, mapper);

assertEquals("a", valueList.get(0)._get());
assertTrue(valueList.get(1).isNull());
assertEquals("c", valueList.get(2)._get());
}
}
}
Loading