From 657b7edd2622ef7230b56bbf66fc9bfe02702c1b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 18 May 2023 21:27:49 +0000 Subject: [PATCH] Fixes deserialization of enums when using booleans instead of strings (#482) * Fixes deserialization of enums when using booleans instead of strings Signed-off-by: Patrick Schlindwein * Fixes CHANGELOG.md Signed-off-by: Patrick Schlindwein * Fixes checkstyle failure Signed-off-by: Patrick Schlindwein * Include boolean support in existing JsonEnum.Deserializer Signed-off-by: Patrick Schlindwein --------- Signed-off-by: Patrick Schlindwein (cherry picked from commit 5af5674fa18095d2c9ecadf921e623718da64d9f) Signed-off-by: github-actions[bot] --- CHANGELOG.md | 1 + .../org/opensearch/client/json/JsonEnum.java | 34 ++++++++++++++----- .../client/opensearch/model/EnumTest.java | 18 ++++++++-- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d36796323..e91b224385 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java b/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java index 6d5aaf9e03..ab63641594 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonEnum.java @@ -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(); @@ -62,19 +62,24 @@ default void serialize(JsonGenerator generator, JsonpMapper params) { class Deserializer extends JsonpDeserializerBase { private final Map lookupTable; + private static final EnumSet ACCEPTED_EVENTS = EnumSet.of( + JsonParser.Event.VALUE_STRING, + JsonParser.Event.KEY_NAME, + JsonParser.Event.VALUE_TRUE, + JsonParser.Event.VALUE_FALSE + ); + + private static final EnumSet 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); } } @@ -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); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java index 4624385103..7faaee7105 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java @@ -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() { @@ -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); + } }