How to enable the "text/plain" content type in the documentation generated by NSwag?
Asked Answered
R

0

7

I'm trying to create a controller in an AspNetCore 3.1 API. c #, with the help of "NSwag.AspNetCore" 13.1.3. The purpose of this controller is to receive and return plain text (not json).

The controller code looks like this:

[HttpPost]
[Route("api/BodyTypes/JsonPlainBody")]
[Consumes("text/plain")]    
[Produces("text/plain")]
public string PlainStringBody([FromBody] string content)
{
    return content;
}   

Excerpt from the "swagger.json" file that describes this service:

...
"/api/BodyTypes/JsonPlainBody": {
  "post": {
    "tags": [
      "Acessórios - Operações diversas"
    ],
    "operationId": "Acessorios_PlainStringBody",
    "requestBody": {
      "x-name": "content",
      "content": {
        "application/json": {
          "schema": {
            "type": "string"
          }
        }
      },
      "required": true,
      "x-position": 1
    },
    "responses": {
      "200": {
        "description": "",
        "content": {
          "application/json": {
            "schema": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}
...

The representation of this controller in SwaggerUI, does not allow me to select the content type "text / plain", consequently, generating the http request in the wrong format, causing the server to return an error related to the data format (415 - Error: Unsupported Media Type):

enter image description here

When testing this same controller in Postman, editing using the content type for "text / plain", everything works as expected:

enter image description here

Any help is most welcome.

Rosalbarosalee answered 11/3, 2020 at 13:19 Comment(1)
This looks like a bug in NSwag, see github.com/RicoSuter/NSwag/issues/2421 and github.com/RicoSuter/NSwag/issues/420#issuecomment-590099138Lapierre

© 2022 - 2024 — McMap. All rights reserved.