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] Fixes deserialization of enums when using booleans instead of strings #488

Merged
merged 1 commit into from
May 21, 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 @@ -21,6 +21,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fix missing Highlight and SourceConfig in the MultisearchBody ([#442](https://github.com/opensearch-project/opensearch-java/pull/442))
- Fix search failure with missing required property HitsMetadata.total when trackTotalHits is disabled ([#372](https://github.com/opensearch-project/opensearch-java/pull/372))
- Fix failure when deserialing response for tasks API ([#463](https://github.com/opensearch-project/opensearch-java/pull/463))
- Fix failure when deserializing boolean types for enums ([#463](https://github.com/opensearch-project/opensearch-java/pull/482))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

/**
* Base interface for enumerations in API types. Members have a JSON representation and also accept
* aliases when parsed from a string value.
* aliases when parsed from a string value. For some enumerations primitive boolean values are also supported.
*/
public interface JsonEnum extends JsonpSerializable {
String jsonValue();
Expand All @@ -62,19 +62,24 @@ default void serialize(JsonGenerator generator, JsonpMapper params) {
class Deserializer<T extends JsonEnum> extends JsonpDeserializerBase<T> {
private final Map<String, T> lookupTable;

private static final EnumSet<JsonParser.Event> ACCEPTED_EVENTS = EnumSet.of(
JsonParser.Event.VALUE_STRING,
JsonParser.Event.KEY_NAME,
JsonParser.Event.VALUE_TRUE,
JsonParser.Event.VALUE_FALSE
);

private static final EnumSet<JsonParser.Event> NATIVE_EVENTS = EnumSet.of(JsonParser.Event.VALUE_STRING);

public Deserializer(T[] values) {
super(
EnumSet.of(JsonParser.Event.VALUE_STRING, JsonParser.Event.KEY_NAME),
EnumSet.of(JsonParser.Event.VALUE_STRING)
);
super(ACCEPTED_EVENTS, NATIVE_EVENTS);

// Use the same size calculation as in java.lang.Enum.enumConstantDirectory
this.lookupTable = new HashMap<>((int)(values.length / 0.75f) + 1);
this.lookupTable = new HashMap<>((int) (values.length / 0.75f) + 1);
for (T member : values) {
this.lookupTable.put(member.jsonValue(), member);
String[] aliases = member.aliases();
if (aliases != null) {
for (String alias: aliases) {
for (String alias : aliases) {
this.lookupTable.put(alias, member);
}
}
Expand All @@ -83,7 +88,18 @@ public Deserializer(T[] values) {

@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
String value = parser.getString();
String value;
switch (event) {
case VALUE_TRUE:
value = "true";
break;
case VALUE_FALSE:
value = "false";
break;
default:
value = parser.getString();
}

return deserialize(value, parser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@

package org.opensearch.client.opensearch.model;

import org.junit.Test;
import org.opensearch.client.opensearch._types.Bytes;
import org.opensearch.client.opensearch._types.mapping.DynamicMapping;
import org.opensearch.client.opensearch._types.mapping.GeoOrientation;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

public class EnumTest extends Assert {
public class EnumTest extends ModelTestCase {

@Test
public void testSimpleEnum() {
Expand All @@ -56,4 +56,16 @@ public void testEnumWithAliases() {
assertEquals(GeoOrientation.Right, GeoOrientation._DESERIALIZER.parse(alias));
});
}

@Test
public void testEnumWithBooleanSupport() {
var booleanAsString = fromJson("\"true\"", DynamicMapping.class);
assertEquals(booleanAsString, DynamicMapping.True);

var nonBooleanEnumValue = fromJson("\"runtime\"", DynamicMapping.class);
assertEquals(nonBooleanEnumValue, DynamicMapping.Runtime);

var booleanPrimitive = fromJson("false", DynamicMapping.class);
assertEquals(booleanPrimitive, DynamicMapping.False);
}
}