alexejk / go-xmlrpc

An XML-RPC Client for Go
https://alexejk.io/article/handling-xmlrpc-in-go/
MIT License
19 stars 7 forks source link

Unable to decode empty strings properly #80

Closed vtan-fortinet closed 8 months ago

vtan-fortinet commented 8 months ago

On a response like

<?xml version="1.0"?>
<methodResponse>
    <params>
        <param>
            <value>
                <struct>
                    <member>
                        <name>somestring</name>
                        <value>
                            <string></string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodResponse>

I expect somestring to be "" but I get the raw xml instead

Test case output:

--- FAIL: TestStdDecoder_DecodeRaw (0.00s)
    --- FAIL: TestStdDecoder_DecodeRaw/struct_response_empty (0.00s)
        /home/vtan/repos/go-xmlrpc/decode_test.go:90: 
                Error Trace:    /home/vtan/repos/go-xmlrpc/decode_test.go:90
                Error:          Not equal: 
                                expected: &struct { Struct struct { Somestring string "xmlrpc:\"somestring\" json:\"somestring\"" } }{Struct:struct { Somestring string "xmlrpc:\"somestring\" json:\"somestring\"" }{Somestring:""}}
                                actual  : &struct { Struct struct { Somestring string "xmlrpc:\"somestring\" json:\"somestring\"" } }{Struct:struct { Somestring string "xmlrpc:\"somestring\" json:\"somestring\"" }{Somestring:"\n\t\t\t\t\t\t\t<string></string>\n\t\t\t\t\t\t"}}

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -2,3 +2,3 @@
                                  Struct: (struct { Somestring string "xmlrpc:\"somestring\" json:\"somestring\"" }) {
                                -  Somestring: (string) ""
                                +  Somestring: (string) (len=32) "\n\t\t\t\t\t\t\t<string></string>\n\t\t\t\t\t\t"
                                  }
                Test:           TestStdDecoder_DecodeRaw/struct_response_empty
FAIL
FAIL    alexejk.io/go-xmlrpc    0.002s
FAIL

Repro case in https://github.com/vtan-fortinet/go-xmlrpc/commit/f7513b3ad5acdb5ed41f1f16c410af04ed8f1704

alexejk commented 8 months ago

Thanks for the report, I'll take a look at this

alexejk commented 8 months ago

Hi @vtan-fortinet,

Thanks again for the report. Seems you were hitting the default case here which was added a while ago to handle the following part of the spec:

If no type is indicated, the type is string.

This was indeed due to an issue in how "existence" of the value was implemented. I've now pushed the change for the code that will handle empty values for all the known data-types in accordance to the spec. This means that while there is a way for arrays to be empty, there is no way for the struct to be empty.

v0.5.1 should be available shortly.

vtan-fortinet commented 8 months ago

Thank you for the prompt response! I've tested and I can confirm you fixed it.