Skip to content

[Backport 8.5] Fix NPEs and race condition in deserializers #407

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

Merged
merged 1 commit into from
Sep 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ protected JsonpDeserializer<B> unwrap() {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper) {
B builder = builderDeserializer.deserialize(parser, mapper);
return build.apply(builder);
return builder == null ? null : build.apply(builder);
}

@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
B builder = builderDeserializer.deserialize(parser, mapper, event);
return build.apply(builder);
return builder == null ? null : build.apply(builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,13 @@ public EnumSet<Event> nativeEvents() {
public EnumSet<Event> acceptedEvents() {
// Accepted events is computed lazily
// no need for double-checked lock, we don't care about computing it several times
if (acceptedEvents == null) {
acceptedEvents = EnumSet.of(Event.START_ARRAY);
acceptedEvents.addAll(itemDeserializer.acceptedEvents());
EnumSet<Event> events = acceptedEvents;
if (events == null) {
events = EnumSet.of(Event.START_ARRAY);
events.addAll(itemDeserializer.acceptedEvents());
acceptedEvents = events;
}
return acceptedEvents;
return events;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
* @see JsonpDeserializable
* @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4">Initialization of Classes and Interfaces</a>
*/
class LazyDeserializer<T> extends DelegatingDeserializer.SameType<T> {
public class LazyDeserializer<T> extends DelegatingDeserializer.SameType<T> {

private final Supplier<JsonpDeserializer<T>> ctor;
private volatile JsonpDeserializer<T> deserializer = null;

LazyDeserializer(Supplier<JsonpDeserializer<T>> ctor) {
public LazyDeserializer(Supplier<JsonpDeserializer<T>> ctor) {
this.ctor = ctor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ protected JsonpDeserializer<B> unwrap() {
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper) {
ObjectBuilder<T> builder = builderDeserializer.deserialize(parser, mapper);
return builder.build();
return builder == null ? null : builder.build();
}

@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
ObjectBuilder<T> builder = builderDeserializer.deserialize(parser, mapper, event);
return builder.build();
return builder == null ? null : builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public FieldDeserializer(String name) {
this.name = name;
}

public abstract EnumSet<Event> acceptedEvents();

public abstract void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName, ObjectType object);

public abstract void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName, ObjectType object, Event event);
Expand All @@ -67,6 +69,11 @@ public String name() {
return this.name;
}

@Override
public EnumSet<Event> acceptedEvents() {
return deserializer.acceptedEvents();
}

public void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName, ObjectType object) {
FieldType fieldValue = deserializer.deserialize(parser, mapper);
setter.accept(object, fieldValue);
Expand All @@ -90,6 +97,11 @@ public void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName,
public void deserialize(JsonParser parser, JsonpMapper mapper, String fieldName, Object object, Event event) {
JsonpUtils.skipValue(parser, event);
}

@Override
public EnumSet<Event> acceptedEvents() {
return EnumSet.allOf(Event.class);
}
};

//---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -242,6 +254,8 @@ public void shortcutProperty(String name) {
throw new NoSuchElementException("No deserializer was setup for '" + name + "'");
}

//acceptedEvents = EnumSet.copyOf(acceptedEvents);
//acceptedEvents.addAll(shortcutProperty.acceptedEvents());
acceptedEvents = EventSetObjectAndString;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package co.elastic.clients.json;

import co.elastic.clients.elasticsearch.model.ModelTestCase;
import jakarta.json.stream.JsonParser;
import org.junit.jupiter.api.Test;

import java.util.List;

public class JsonpDeserializerBaseTest extends ModelTestCase {

@Test
public void testArrayDeserializer() {

JsonpDeserializer<List<Integer>> deser =
JsonpDeserializer.arrayDeserializer(JsonpDeserializer.integerDeserializer());

assertFalse(deser.nativeEvents().contains(JsonParser.Event.VALUE_NUMBER));
assertTrue(deser.acceptedEvents().contains(JsonParser.Event.VALUE_NUMBER));

List<Integer> values = fromJson("[ 42, 43 ]", deser);
assertEquals(2, values.size());
assertEquals(42, values.get(0));
assertEquals(43, values.get(1));

// Single value representation
values = fromJson("42", deser);
assertEquals(1, values.size());
assertEquals(42, values.get(0));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package co.elastic.clients.json;

import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
import co.elastic.clients.elasticsearch.model.ModelTestCase;
import co.elastic.clients.elasticsearch.transform.UpdateTransformRequest;
import org.junit.jupiter.api.Test;

import java.io.StringReader;

public class ObjectBuilderDeserializerTest extends ModelTestCase {

@Test
public void testNullObjectValue() {
// Should also accept null for optional values
String json = "{ \"index_prefixes\": null }";
fromJson(json, TextProperty.class);
}

@Test
public void testNullObjectValueInFunctionBuilder() {
String json = "{\n" +
" \"retention_policy\": null\n" +
" }";

UpdateTransformRequest.Builder builder = new UpdateTransformRequest.Builder();
builder.transformId("foo");
builder.withJson(new StringReader(json));
builder.build();
}

}