C#. Byte array parameter not being serialized properly
See original GitHub issueWe are having problems with requests containing byte arrays as parameters. I think the byte[] values are not being serialized properly.
More details:
We have a POST endpoint with a byte parameter. Something like:
public HttpResponseMessage Post([FromBody] PostRequest request)
Where PostRequest is:
public class PostRequest
{
public byte? Parameter { get; set; }
}
When we generate the client classes, the byte parameter becomes a byte[]. When we try to call to that client method using something like:
var api = new APIClient(new Uri("http://myapi.com/"));
byte[] byteArray = { 1 };
var requestParameters = new RequestParameters()
{
Parameter = byteArray
};
api.ClientClass.Post(requestParameters);
requestParameters.Parameter arrives as null to the API endpoint.
I’ve been debugging the client generated code and I think the problem is when serializing the requestParameters object into a JSON. The resulting JSON is:
{
"parameter": "AQ==",
}
So I guess that the API can’t deserialize that value into a byte.
The generated code that does the serialization is:
public async Task<HttpOperationResponse<PostResponse>> PostWithHttpMessagesAsync(PostRequest request, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
{
...
// Serialize Request
string _requestContent = null;
if(request != null)
{
_requestContent = SafeJsonConvert.SerializeObject(request, this.Client.SerializationSettings);
_httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
_httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
}
...
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Here you go: swagger.json.txt
I saw now that I have problems with the Get operation too. Getting a Microsoft.Rest.SerializationException: SerializationException.txt
So summarizing, I have problems both with the POST (the serialized JSON can’t be properly deserialized by the API), and with the GET (exception deserializing the response from the API). The parameter having problems is a byte in the API and a byte[] in the generated client. Shouldn’t be the parameter a byte, instead of a byte[], in the generated code as well?
Yes, I am using Swashbuckle. The problem was that the version I was using was generating bytes as strings. I updated to the latest version and it’s working fine.
Sorry for posting the problem here. It wasn’t a problem with AutoRest but with Swashbuckle.
Thanks for your help guys.