NSwag: Invalid handling of 201 response types
hi @rsuter
I’ve already seen a few issues reported to this topic but I was not able to find a “best way to handle” it. In both c# and typescript generated code the 201 response type throws an exception instead of returning the result. Of course I can catch it and get the result extracted out of the exception but this is not a convenient approach.
if (status === 200) {
let result200: SampleEntity = null;
let resultData200 = responseText === "" ? null : JSON.parse(responseText, this.jsonParseReviver);
result200 = resultData200 ? SampleEntity.fromJS(resultData200) : new SampleEntity();
return result200;
} else if (status === 201) {
let result201: SampleEntity = null;
let resultData201 = responseText === "" ? null : JSON.parse(responseText, this.jsonParseReviver);
result201 = resultData201 ? SampleEntity.fromJS(resultData201) : new SampleEntity();
this.throwException("A server error occurred.", status, responseText, result201);
} else if (status !== 200 && status !== 204) {
this.throwException("An unexpected server error occurred.", status, responseText);
}
On the controller of my asp.net core project I add an attribute on the post-method to tell swagger about the new response type. I am producing a 201 by calling the CreatedAtRoute(…) method.
[ProducesResponseType(typeof(SampleEntity), (int)HttpStatusCode.Created)]
Do you have any suggestion how to handle it? I’d like to keep my 201 response type without the need of catching all exceptions for post requests. Is there a better way instead of manually modifying the generated service client after its creation?
Thanks for your help!
Jan
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (7 by maintainers)
@freedeveloperjagg : Yes, you’re probably missing the
ProducesResponseTypeattribute above your controller method to declare the expected return type.Also, please don’t reply to closed issues, open your own issue, otherwise your question might be missed.