-
Notifications
You must be signed in to change notification settings - Fork 24.8k
[11.0 P1] What's New: OpenAPI: Binary File Response #36747
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
aspnetcore/release-notes/aspnetcore-11/includes/openapi-binary-file-response.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| ### Describe binary file responses | ||
|
|
||
| ASP.NET Core 11 introduces support for generating OpenAPI descriptions for operations that return binary file responses. This support maps the `FileContentResult` result type to an OpenAPI schema with `type: string` and `format: binary`. | ||
|
|
||
| #### [Minimal APIs](#tab/minimal-apis) | ||
|
|
||
| Use the `Produces<T>` extension method with `T` of `FileContentResult` to specify the response type and content type: | ||
| <!-- UPDATE 11.0 - API cross-link needs to be entered in the line above for the new API | ||
| such as: <xref:Microsoft.AspNetCore.NEW_API_TO_BE_ENTERED_> | ||
| --> | ||
|
wadepickett marked this conversation as resolved.
|
||
|
|
||
| ```csharp | ||
| app.MapPost("/filecontentresult", () => | ||
| { | ||
| var content = "This endpoint returns a FileContentResult!"u8.ToArray(); | ||
| return TypedResults.File(content); | ||
| }) | ||
| .Produces<FileContentResult>(contentType: MediaTypeNames.Application.Octet); | ||
| ``` | ||
|
|
||
| #### [Controllers](#tab/controllers) | ||
|
|
||
| Use the `ProducesResponseType<T>` attribute with `T` of `FileContentResult` to specify the response type and content type: | ||
|
|
||
| ```csharp | ||
| [HttpPost("filecontentresult")] | ||
| [ProducesResponseType<FileContentResult>(StatusCodes.Status200OK, MediaTypeNames.Application.Octet)] | ||
| public IActionResult PostFileContentResult() | ||
| { | ||
| var content = "This endpoint returns a FileContentResult!"u8.ToArray(); | ||
| return new FileContentResult(content, MediaTypeNames.Application.Octet); | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| The generated OpenAPI document describes the endpoint response as: | ||
|
|
||
| ```yaml | ||
| responses: | ||
| '200': | ||
| description: OK | ||
| content: | ||
| application/octet-stream: | ||
| schema: | ||
| $ref: '#/components/schemas/FileContentResult' | ||
| ``` | ||
|
|
||
| The `FileContentResult` is defined in `components/schemas` as: | ||
|
|
||
| ```yaml | ||
| components: | ||
| schemas: | ||
| FileContentResult: | ||
| type: string | ||
| format: binary | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.