question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

C#. Byte array parameter not being serialized properly

See original GitHub issue

We 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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alesvicommented, Nov 17, 2016

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?

0reactions
alesvicommented, Nov 18, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing Array of Structs to byte[] - What's Wrong with my ...
SizeOf is reporting and the actual size of the data structure, thus leading to insufficient memory being allocated for the structure. If it ......
Read more >
C# – Byte array as an out parameter not recognized - iTecNote
C# – Byte array as an out parameter not recognized. argumentsc++parametersreturn-value. I had a webmethod working which returned a byte array to the...
Read more >
String (Java Platform SE 8 ) - Oracle Help Center
Each byte in the subarray is converted to a char as specified in the method above. Parameters: ascii - The bytes to be...
Read more >
Serialization (C#) - Microsoft Learn
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a...
Read more >
[Solved]-Byte array as an out parameter not recognized-C#
The byte array isn't being ignored - it's being put as the return type instead. I don't know why it's doing that, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found