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

Return single element array with JSON-RPC is impossible? #422

Open
cl0ne opened this issue Apr 9, 2021 · 3 comments
Open

Return single element array with JSON-RPC is impossible? #422

cl0ne opened this issue Apr 9, 2021 · 3 comments

Comments

@cl0ne
Copy link
Contributor

cl0ne commented Apr 9, 2021

When we have a method that returns list of structs, depending on the element count we will get completely different results:

  • list with 2+ structs will give an expected array with 2+ objects;
  • empty list will give an empty string (okay, I get it, we have no way differentiate empty list from empty strings);
  • list with single struct will be stripped down to object instead of an array with single object (of course, we can work around this by wrapping result list with a single-field object, but that looks like an ugly hack).

The question is why encode_handler_payload unwraps single-element lists?

@vinoski
Copy link
Collaborator

vinoski commented Apr 9, 2021

This was apparently done in the very first version of the yaws_rpc module; see 1791e3d. My understanding is that the module was originally written to support Haxe, so perhaps it had/has a case that requires this.

@cl0ne
Copy link
Contributor Author

cl0ne commented Apr 9, 2021

I guess that replacing

yaws/src/yaws_rpc.erl

Lines 393 to 402 in ca704a9

encode_handler_payload({response, [ErlStruct]}, ID, RpcType) ->
encode_handler_payload({response, ErlStruct}, ID, RpcType);
encode_handler_payload({response, ErlStruct}, ID, RpcType) ->
StructStr =
case RpcType of
json -> json2:encode({struct, [{result, ErlStruct}, {id, ID},
{"jsonrpc", "2.0"}]});
haxe -> [$h, $x, $r | haxe:encode(ErlStruct)]
end,
{ok, StructStr}.

with

encode_handler_payload({response, ErlStruct}, ID, json) ->
    StructStr = json2:encode({struct, [
        {result, ErlStruct},
        {id, ID},
        {"jsonrpc", "2.0"}
    ]}),
    {ok, StructStr};

encode_handler_payload({response, [ErlStruct]}, ID, haxe) ->
    encode_handler_payload({response, ErlStruct}, ID, haxe);
encode_handler_payload({response, ErlStruct}, ID, haxe) ->
    StructStr = [$h, $x, $r | haxe:encode(ErlStruct)],
    {ok, StructStr}.

will fix the issue but breaks backward compatibility at the same time.

@vinoski
Copy link
Collaborator

vinoski commented Apr 12, 2021

Right, the backward compatibility issue is the problem with changing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants