Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
142 stars 166 forks source link

Generated C# code contains invalid casts #423

Open bexxx opened 4 years ago

bexxx commented 4 years ago

We have documented response types and error types in our swagger. I've added the x-ms-error-response extension to all responses except the 200/OK response.

The generated code now picks up the correct type in the signature, but tries to assign all responses, incl. the errors to a field of the type of the OK response.

I generated the code with autorest --csharp --input-file=Desktop\swagger.json Is there any other switch I need to set here?

Generated code: if ((int)_statusCode == 200) { _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); _result.Body = SafeJsonConvert.DeserializeObject<IList>(_responseContent, DeserializationSettings); } if ((int)_statusCode == 401) { _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); _result.Body = SafeJsonConvert.DeserializeObject(_responseContent, DeserializationSettings); }

Both types are assigned to the same field, which will of course not work. Any clue how I can generate correctly compiling code?

fearthecowboy commented 4 years ago

x-ms-error-response didn't make it into the master branch.

For the current generator you have to lump all of the errors in the default response, and provide the error model there.

We're rewriting several generators (including c#) right now, and new code will have support for x-ms-error-response

kehepo commented 4 years ago

Not sure why my reply earlier was deleted. I recommended giving NSWag a try before the issue gets addressed in Autorest.