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.

nswag v13 can't handle text\plain and string response

See original GitHub issue

This is very old issue and it was fixed in v12, but appears again in v13: #701

Controller’s attributes:

[Produces("text/plain")]
[ProducesResponseType(typeof(FileContentResult), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseMessage), (int)HttpStatusCode.BadRequest)]
[ProducesResponseType(typeof(ErrorResponseMessage), (int)HttpStatusCode.NotFound)]

Swagger:

"get": {
        "tags": [ "Binary" ],
        "summary": "Download binary as base64 string.",
        "operationId": "DownloadBinaryAsBase64",
        "consumes": [],
        "produces": [ "text/plain" ],
        "parameters": [
          {
            "name": "binaryId",
            "in": "path",
            "description": "Id of binary to fetch.",
            "required": true,
            "type": "string",
            "format": "guid"
          }
        ],
        "responses": {
          "200": {
            "description": "Content downloaded.",
            "schema": { "type": "string" }
          },
          "400": {
            "description": "Request contains wrong data.",
            "schema": { "$ref": "#/definitions/ErrorResponseMessage" }
          },
          "404": {
            "description": "Not Found",
            "schema": { "$ref": "#/definitions/ErrorResponseMessage" }
          }
        }

Produced code (part):

var status_ = ((int)response_.StatusCode).ToString();
if (status_ == "200") 
{
    var objectResponse_ = await ReadObjectResponseAsync<string>(response_, headers_).ConfigureAwait(false);
    return objectResponse_.Object;
}

ReadObjectResponseAsync always try to deserialize the string and of course it will fail.

Revert to v12.2.5 fix the issue.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:62 (17 by maintainers)

github_iconTop GitHub Comments

6reactions
tmort93commented, Mar 9, 2020

Any idea when this patch will be released?

3reactions
diogocpcommented, Jun 8, 2022

can you please add a hard-copy of the bad generated code to your repo?

Done.

Also, is the issue still reproducible with the latest versions 13.16.1 and later?

Yes, tested with 13.16.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nswag v13 can't handle text\plain and string response -
This is very old issue and it was fixed in v12, but appears again in v13: #701. Controller's attributes: [Produces("text/plain")] ...
Read more >
NSwag generated client still serializes string for text/plain ...
I'm using NSwag to generate a c# client based on a swagger.json. My main problem is that the generated client's function for calling...
Read more >
OpenAPI/Swagger not deserializing strings correctly
This works fine in SwaggerUI and lists several media types for the response: text/plain, application/json, and text/json.
Read more >
Get started with NSwag and ASP.NET Core
Learn how to use NSwag to generate documentation and help pages for an ASP.NET Core web API.
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & ...
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
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