Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define 'File' Schema Type without filter #1144

Open
malichishti opened this issue Jul 26, 2017 · 3 comments
Open

Define 'File' Schema Type without filter #1144

malichishti opened this issue Jul 26, 2017 · 3 comments

Comments

@malichishti
Copy link

Hi,

Is it possible to achieve below without the filter i.e. using an attribute or any other way?
image

@rliberoff
Copy link

Hi,

I have the very same project. Working with ASP.NET Core and Swashbuckle.AspNetCore 1.2.0, the following code does not generate the appropriate "produces", and the "Response Content Type" always shows "application/json" instead of the configured values.

Example of the code:

[HttpPost]
[Produces(@"application/pdf")]
[ProducesResponseType(typeof(FileResult), 200)]
[ProducesResponseType(typeof(void), 400)]
[ProducesResponseType(typeof(void), 401)]
[Route(@"api/[controller]/Report")]
public async Task<IActionResult> GenerateReport([FromBody]Request request)
{
	Stream stream = await GenerateReportAsync(request).ConfigureAwait(false);
	return this.File(stream, @"application/pdf", @"report.pdf");
}

I had to create an IOperationFilter to add it.

Thank you.

@BernhardNinaus
Copy link

BernhardNinaus commented Feb 18, 2022

Expectations

My expectations would be something like this:

...
[ProducesResponseType(typeof(FileResult), 200)]
[ProducesResponseType(typeof(ErrorModel), 403, MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(ErrorModel), 404, MediaTypeNames.Application.Json)]
...

The status' code 200 response type would be declared either way:

[Produces(MediaTypeNames.Application.Pdf)] // could act as default, and already does, nice
// or
[ProducesResponseType(typeof(FileResult), 200, MediaTypeNames.Application.Pdf)]

and would produce:

...
"responses": {
  "200": {
    "description": "Success",
    "content": {
      "application/pdf": {
        "schema": {
          "type": "string",
          "format": "binary"
        }
      }
    }
  },
  "403": {
    "description": "Forbidden",
    "content": {
      "application/json": {
        "schema": {
            "$ref": "#/components/schemas/ErrorModel"
        }
      }
    }
  },
  "404": {
    "description": "Not Found",
    "content": {
      "application/json": {
        "schema": {
            "$ref": "#/components/schemas/ErrorModel"
        }
      }
    }
  }
}
...

What changes or should change, imho

In this example, status code 200 stays the same, if you use it with:

[Produces(MediaTypeNames.Application.Pdf)]

But currently if you use:

[ProducesResponseType(typeof(ErrorModel), statusCode, MediaTypeNames.Application.Json)]

or anything defined with content type in ProducesResponseType, you will get just:

"{statusCode}": {
  "description": "{statusCodeDescription}"
}

and the only change would be to add:

"content": {
  "application/json": {
    "schema": {
      "$ref": "#/components/schemas/ErrorModel"
    }
  }
}

if content type is defined.



That is at least the only thing is see wrong here, as @rliberoff already pointed out.

@rliberoff could you post your IOperationFilter, or what did you try to do with it?

@BernhardNinaus
Copy link

Okay, I found a PR on the Swashbuckle.AspNetCore repo which actually implemented this in the SwaggerResponseAttribute domaindrivendev/Swashbuckle.AspNetCore#1956

However, it is not perfect because [Produces(MediaTypeNames.Application.Pdf)] overrides the more specific SwaggerResponse...

But anyway, could be another issue in the other repo because it is out of scope.
I think this issue can be closed, because the "file" type is not anymore in Open API 3.0 anyway.

I hope, I didn't disturb you all to much, have a nice day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants