opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

[BUG]JsonEnum cannot recognize uppercase during deserialization.

Open lihuimingxs opened this issue 11 months ago • 1 comments

What is the bug?

JsonEnum deserialization employs a lookupTable, which is a Map<String, T>.  In the deserialize method, the corresponding enumeration is obtained through the method this.lookupTable.get(value).  A problem arises regarding the case sensitivity of strings:  If the value in the enumeration class is lowercase, and uppercase letters are used in the DSL, an error will be reported. However, the DSL permits the use of uppercase letters. This can be quite confusing for developers.

How can one reproduce the bug?

Taking ZerotermsQuery as an example:


@JsonpDeserializable
public enum ZeroTermsQuery implements JsonEnum {
    All("all"),
    None("none");

    private final String jsonValue;
    public static final JsonEnum.Deserializer<ZeroTermsQuery> _DESERIALIZER = new JsonEnum.Deserializer(values());

    private ZeroTermsQuery(String jsonValue) {
        this.jsonValue = jsonValue;
    }

    public String jsonValue() {
        return this.jsonValue;
    }
}

DSL:


GET test_index/_search
{
  "query": {
    "match": {
      "text": {
          "fuzziness": "AUTO",
          "auto_generate_synonyms_phrase_query": true,
          "query": "product manager",
          "zero_terms_query": "NONE",
          "fuzzy_transpositions": true,
          "boost": 1.0,
          "prefix_length": 0,
          "operator": "OR",
          "lenient": false,
          "max_expansions": 50
      }
    }
  }
}

That will be thrown JsonparsingException when executing the anti sequence method.


public T deserialize(String value, JsonParser parser) {
            T result = (JsonEnum)this.lookupTable.get(value);
            if (result == null) {
                throw new JsonParsingException("Invalid enum '" + value + "'", parser.getLocation());
            } else {
                return result;
            }
        }

What is the expected behavior?

case-insensitive.

What is your host/environment?

Operating system, version.

Do you have any screenshots?

If applicable, add screenshots to help explain your problem.

Do you have any additional context?

Add any other context about the problem.

lihuimingxs avatar Jan 15 '25 10:01 lihuimingxs

Catch All Triage - 1, 2, 3, 4, 5

andrross avatar Feb 03 '25 17:02 andrross

The uppercase variants of ZeroTermsQuery have been added in the client's v3.0.0 release

Xtansia avatar May 16 '25 04:05 Xtansia