ava-innersource / Liquid-Application-Framework-1.0-deprecated

Liquid is a framework to speed up the development of microservices
MIT License
25 stars 13 forks source link

LightAPI converts the request content to JSON #103

Closed guilhermeluizsp closed 4 years ago

guilhermeluizsp commented 4 years ago

Just before sending any request, the LightAPI will transform the content to a ByteArrayContent by calling the extension method ConvertToByteArrayContent

https://github.com/Avanade/Liquid-Application-Framework/blob/1d6c37c44fb5bbb6f84ba2f1d1a8a1b3d646c2ad/src/Liquid.Domain/API/LightAPI.cs#L288-L291

https://github.com/Avanade/Liquid-Application-Framework/blob/1d6c37c44fb5bbb6f84ba2f1d1a8a1b3d646c2ad/src/Liquid.Domain/API/LightAPI.cs#L336-L339

https://github.com/Avanade/Liquid-Application-Framework/blob/1d6c37c44fb5bbb6f84ba2f1d1a8a1b3d646c2ad/src/Liquid.Domain/API/LightAPI.cs#L388-L391

https://github.com/Avanade/Liquid-Application-Framework/blob/1d6c37c44fb5bbb6f84ba2f1d1a8a1b3d646c2ad/src/Liquid.Domain/API/LightAPI.cs#L519

https://github.com/Avanade/Liquid-Application-Framework/blob/1d6c37c44fb5bbb6f84ba2f1d1a8a1b3d646c2ad/src/Liquid.Domain/API/LightAPI.cs#L586

This extension method serializes the request body to JSON

https://github.com/Avanade/Liquid-Application-Framework/blob/1d6c37c44fb5bbb6f84ba2f1d1a8a1b3d646c2ad/src/Liquid.Domain/Extensions/ObjectExtensions.cs#L20-L31

By doing this, we can't consume XML or form-data APIs. Also, looking at the first line of the ConvertToByteArrayContent, we can notice it serializes the object itself. This may be a bug because we usually pass an HttpContent to this method and we should serialize its content (by calling ReadAsStringAsync first)

guilhermeluizsp commented 4 years ago

I'd like to note that this causes a bug when we pass a string to the body parameter. Every request method (Post, Get, Put, ...) accepts a JToken as body. JToken has an implicit conversion from string to JToken. Since the method ConvertToByteArrayContent always serializes the object (line 25), we'll be sending a serialized JSON string instead of an object.

So, instead of sending this:

{
  "a": 1,
  "b": 2
}

We're sending this:

"{
  \"a\": 1,
  \"b\": 2
}"

which prevents us from interacting with any API.

As a workaround, we can stop sending strings as bodies.

bruno-brant commented 4 years ago

I've been wondering about this and I believe that, given some documentation I found, the goal of LightApi was to be used exclusively for communication between Liquid services. For that reason, it's restrictive by design.

This means that we need to clearly document it's intent, and maybe rename it to JsonHttpClient or RestfullHttpClient or something.

bruno-brant commented 4 years ago

LightApi is a poor wrapper over HttpClient. As stated on #150, we should simply retire it in favor of a better solution such as RestSharp.