Skip to content

SmileParser.getValueAsString() FIELD_NAME bug (same for CBOR, Protobuf) #541

@johhud1

Description

@johhud1

I believe there is a bug in SmileParser#getValueAsString().

Comparing the method implementation in SmileParser

    @Override
    public String getValueAsString() throws IOException
    {
        // inlined 'getText()' for common case of having String
        if (_tokenIncomplete) {
            _tokenIncomplete = false;
            int tb = _typeAsInt;
            int type = (tb >> 5);
            if (type == 2 || type == 3) { // tiny & short ASCII
                return _decodeShortAsciiValue(1 + (tb & 0x3F));
            }
            if (type == 4 || type == 5) { // tiny & short Unicode
                return _decodeShortUnicodeValue(2 + (tb & 0x3F));
            }
            _finishToken();
        }
        if (_currToken == JsonToken.VALUE_STRING) {
            return _textBuffer.contentsAsString();
        }
        if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) {
            return null;
        }
        return getText();
    }

To the implementation in jackson-core UTF8StreamJsonParser.java

    public String getValueAsString() throws IOException
    {
        if (_currToken == JsonToken.VALUE_STRING) {
            if (_tokenIncomplete) {
                _tokenIncomplete = false;
                return _finishAndReturnString(); // only strings can be incomplete
            }
            return _textBuffer.contentsAsString();
        }
        if (_currToken == JsonToken.FIELD_NAME) {
            return currentName();
        }
        return super.getValueAsString(null);
    }

The output is different when invoking the two methods on tokens of type JsonToken.FIELD_NAME, whereas I would expect them to behave similarly ( returning the current token field name as a string ).

I have a fork with a simple test update that reproduces the issue and a naive fix that worked for my use case.
dabe16a
The updated test fails without the change in SmileParser.java .

This came to my attention while trying to use the OpenSearch java sdk with jackson and 'application/smile' encoding. The deserialized responses from opensearch contained only null properties after switching to Smile ObjectMapper. The usage in question is here https://github.com/opensearch-project/opensearch-java/blob/main/java-client/src/main/java/org/opensearch/client/json/ObjectDeserializer.java#L179 ; but it appears the usage there is correct.

Please let me know if there's any more info or help I can provide; or if I'm misunderstanding or mistaken.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions