-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add support for setting error type via ProducesResponseType attribute #30236
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,7 +15,7 @@ public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProv | |||||
{ | ||||||
/// <summary> | ||||||
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>. | ||||||
/// </summary> | ||||||
/// </summary> | ||||||
/// <param name="statusCode">The HTTP response status code.</param> | ||||||
public ProducesResponseTypeAttribute(int statusCode) | ||||||
: this(typeof(void), statusCode) | ||||||
|
@@ -31,6 +31,18 @@ public ProducesResponseTypeAttribute(Type type, int statusCode) | |||||
{ | ||||||
Type = type ?? throw new ArgumentNullException(nameof(type)); | ||||||
StatusCode = statusCode; | ||||||
OverrideDefaultErrorType = false; | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>. | ||||||
/// </summary> | ||||||
/// <param name="type">The <see cref="Type"/> of object that is going to be written in the response.</param> | ||||||
/// <param name="statusCode">The HTTP response status code.</param> | ||||||
/// <param name="overrideDefaultErrorType">Whether or not to override default error type</param> | ||||||
public ProducesResponseTypeAttribute(Type type, int statusCode, bool overrideDefaultErrorType) : this(type, statusCode) | ||||||
{ | ||||||
OverrideDefaultErrorType = overrideDefaultErrorType; | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -43,6 +55,19 @@ public ProducesResponseTypeAttribute(Type type, int statusCode) | |||||
/// </summary> | ||||||
public int StatusCode { get; set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Gets or sets whether or not the default error type should be used when one | ||||||
/// is expliclty provided as a type in the <see cref="ProducesResponseTypeAttribute"/>. | ||||||
/// | ||||||
/// When <see langword="true"/>, the ApiExplorer will use the provided <see cref="Type"/> | ||||||
/// as the default type for error objects. | ||||||
/// | ||||||
/// When <see langword="false"/>, the ApiExplorer will use the globally configured default | ||||||
/// error type. | ||||||
/// </summary> | ||||||
/// <value></value> | ||||||
public bool OverrideDefaultErrorType { get; set; } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Open to alternative ideas for names here. My creativity puttered and this was the most explicit name that I could come up with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'll play around with this to get a sense for the bug. Declaring the error behavior here is too far disconnected from the implementation that configures one to make sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another approach is to change the below: aspnetcore/src/Mvc/Mvc.Core/src/ApplicationModels/ApiBehaviorApplicationModelProvider.cs Lines 45 to 46 in b98bcea
to read from an option other than |
||||||
|
||||||
/// <inheritdoc /> | ||||||
void IApiResponseMetadataProvider.SetContentTypes(MediaTypeCollection contentTypes) | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace ApiExplorerWebSite | ||
{ | ||
[Route("ApiExplorerVoid/[action]")] | ||
[ApiController] | ||
public class ApiExplorerVoidController : Controller | ||
{ | ||
[ProducesResponseType(typeof(void), 401, true)] | ||
public IActionResult ActionWithVoidType() => Ok(); | ||
|
||
[ProducesResponseType(typeof(void), 401)] | ||
public IActionResult ActionWithVoidTypeAndOverrideDisabled() => Ok(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Extra line |
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading this correctly, the flag means the opposite of what the sentence above implies. That is,
true
means we use the explicitly-provided one, andfalse
means we use the global one. Also typo in "explicitly".