-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Support all ProblemDetail types in the multi response types for generating swagger docs MinimalApi #47623
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
Comments
Moving to |
Yup. cc @mitchdenny @captainsafia |
@captainsafia assigning this one to you since you are more familiar with the interplay with Swagger. |
Can I contribute for this? |
@mehdihadeli is you would like to propose a change to the public API of ASP.NET Core consider submitting an issue using the following template so that we can run it through the review process. If you prefer you can update the description of the issue above with the template so that we don't lose the context of this conversation. Getting the API changes reviewed is the first step to make sure you don't waste any time working on API changes that may not be approved. |
Hi @mehdihadeli. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Ok, I will create a proposal for that and mention the proposal here |
It seems intentional that the I'm not enthusiastic about the API introduced in #47705 because I think that's best suited to be in app code or an external dependency. Starting to maintain strongly-typed
You might want to take advantage of the |
Hi @mehdihadeli. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Thanks for your response. This is one of my custom problem types: public class NotFoundHttpProblemResult
: IResult,
IStatusCodeHttpResult,
IContentTypeHttpResult,
IValueHttpResult,
IEndpointMetadataProvider
{
private readonly ProblemHttpResult _internalResult;
internal NotFoundHttpProblemResult(ProblemDetails problemDetails)
{
ArgumentNullException.ThrowIfNull(problemDetails);
if (problemDetails is
{
Status: not null and not StatusCodes.Status404NotFound
})
{
throw new ArgumentException(
$"{nameof(NotFoundHttpProblemResult)} only supports a 404 NotFound response status code.",
nameof(problemDetails));
}
problemDetails.Status ??= StatusCodes.Status404NotFound;
_internalResult = TypedResults.Problem(problemDetails);
}
public ProblemDetails ProblemDetails => _internalResult.ProblemDetails;
public Task ExecuteAsync(HttpContext httpContext)
{
return _internalResult.ExecuteAsync(httpContext);
}
public int? StatusCode => _internalResult.StatusCode;
public string? ContentType => _internalResult.ContentType;
object? IValueHttpResult.Value => _internalResult.ProblemDetails;
public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
{
ArgumentNullException.ThrowIfNull(method);
ArgumentNullException.ThrowIfNull(builder);
builder.Metadata.Add(new ProducesResponseTypeAttribute(typeof(ProblemDetails), StatusCodes.Status404NotFound));
}
} Here in the constructor, I don't have access to something like public sealed class ValidationProblem : IResult, IEndpointMetadataProvider, IStatusCodeHttpResult, IContentTypeHttpResult, IValueHttpResult, IValueHttpResult<HttpValidationProblemDetails>
{
internal ValidationProblem(HttpValidationProblemDetails problemDetails)
{
ArgumentNullException.ThrowIfNull(problemDetails);
if (problemDetails is { Status: not null and not StatusCodes.Status400BadRequest })
{
throw new ArgumentException($"{nameof(ValidationProblem)} only supports a 400 Bad Request response status code.", nameof(problemDetails));
}
ProblemDetails = problemDetails;
ProblemDetailsDefaults.Apply(ProblemDetails, statusCode: StatusCodes.Status400BadRequest);
}
}
Also, because public static class TypedResultsExtensions
{
public static InternalHttpProblemResult InternalProblem(
string? title = null,
string? detail = null,
string? instance = null,
string? type = null,
IDictionary<string, object?>? extensions = null
)
{
var problemDetails = CreateProblem(title, detail, instance, type, extensions);
return new(problemDetails);
}
public static UnAuthorizedHttpProblemResult UnAuthorizedProblem(
string? title = null,
string? detail = null,
string? instance = null,
string? type = null,
IDictionary<string, object?>? extensions = null
)
{
var problemDetails = CreateProblem(title, detail, instance, type, extensions);
return new(problemDetails);
}
private static ProblemDetails CreateProblem(
string? title,
string? detail,
string? instance,
string? type,
IDictionary<string, object?>? extensions
)
{
var problemDetails = new ProblemDetails
{
Detail = detail,
Instance = instance,
Type = type
};
problemDetails.Title = title ?? problemDetails.Title;
if (extensions is not null)
{
foreach (var extension in extensions)
{
problemDetails.Extensions.Add(extension);
}
}
return problemDetails;
}
} How can I handle these limitations? |
@mehdihadeli Is there a reason you are not using the |
Because I want to make my minimal api compatible with app.MapPost("/", Handle);
async Task<Results<CreatedAtRoute<CreateProductResponse>, UnAuthorizedHttpProblemResult, ValidationProblem>> Handle([AsParameters] CreateProductRequestParameters requestParameters)
{
} And as document says it is better we handle open-api-docuemntation with |
As mentioned in the corresponding API issue, providing status code specific ProblemDetails-generating extension methods isn't something we'd like to pursue in the framework at the moment. Although TypedResults is the recommended approach for built-in Result types, it's totally valid to use |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Hi,
I want to create an api with
multiple result types
with usingTypedResults
, but seems, .net core 7 only supportsValidationProblem
for generating swagger metadata automatically andProblemHttpResult
type doesn't generate any swagger metadata. Maybe we need to declare dedicated problem types likeValidationProblem
for others, likeInternalProbelm
,UnAuthorizedProblem
, ...I had to handle it manually for myself like this (we have some limitation because
ProblemHttpResult
is sealed class and I can't inherit from it directly, so I created aHttpProblemResultBase
for replacement, alsoProducesResponseTypeMetadata
was internal, and I had to create a custom one):And my route now should be for generating swagger metadata for problems automatically:
The text was updated successfully, but these errors were encountered: