Как комментировать ошибки, используя Swashbuckle Swagger - PullRequest
0 голосов
/ 27 сентября 2019

Я использовал пакет Nuget Swashbuckle swagger в .ap Framework webapi.

Я пытаюсь комментировать ответы API.Это отлично подходит для успешных ответов, я просто указываю тип моей объектной модели:

[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(WebAPI.Models.APIResponse), Description = "Successful operation.")]

Если я возвращаю ошибку, подобную этой:

return this.Request.CreateErrorResponse(HttpStatusCode.NotFound, "Document not found.");

Я получаю следующие ответы json / xml

{
  "Message": "Document not found."
}

<Error>
    <Message>Document not found.</Message>
</Error>

Если есть необработанное исключение, я получу http 500 с ответом json / xml, подобным этому (Нет, подробности об исключении возвращаются только при выполнении вызова rest с локальной машины .:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "This is a test error",
  "ExceptionType": "System.Exception",
  "StackTrace": "   at WebAPI.Controllers.MyController.Post(Guid id) in ..."
}

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>This is a test error</ExceptionMessage
    <ExceptionType>System.Exception</ExceptionType>
    <StackTrace>   at WebAPI.Controllers.MyController.Post(Guid id) in ...</StackTrace>
</Error>

Для этих ответов, как мне их аннотировать с помощью Swashbuckle?

Я пробовал:

[SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(HttpResponseMessage), Description = "Error.")]

[SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(HttpResponseException), Description = "Error.")]

[SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(Exception), Description = "Error.")]

В разделе примера модели я либо получаюсообщение «Объект не является примитивом», как в случае Exception и HttpResponse, или пример не отражает, как будет выглядеть ответ на самом деле. Например, HttpResponseException выглядит так в пользовательском интерфейсе swagger:

{
  "Response": {},
  "Message": "string",
  "Data": {},
  "InnerException": {},
  "StackTrace": "string",
  "HelpLink": "string",
  "Source": "string",
  "HResult": 0
}

<?xml version="1.0"?>
<HttpResponseException>
  <!-- invalid XML -->
  <Message>string</Message>
  <Data>
    <!-- additional elements allowed -->
  </Data>
  <!-- invalid XML -->
  <StackTrace>string</StackTrace>
  <HelpLink>string</HelpLink>
  <Source>string</Source>
  <HResult>1</HResult>
</HttpResponseException>
...