This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3818 - Support Consumes in ApiExplorer
This change adds a list of ApiRequestFormat objects to ApiDescription object which include the content type and formatter for each supported content type which can be understood by the action. Computation is aware of the [Consumes] attribute via the IApiRequestMetadataProvider metadata interface, and aware of Input Formatters via the new IApiRequestFormatMetadataProvider interface. This algorithm is essentially the same as what we do for produces/output-formatters. We iterate the filters and ask them what content types they think are supported. Then we cross check that list with the formatters, and ask them which from that list are supported. If no [Consumes] filters are used, the formatters will include everything they support by default. This feature and data is only available when an action has a [FromBody] parameter, which will naturally exclude actions that handle GET or DELETE and don't process the body.
- Loading branch information
Showing
11 changed files
with
488 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,24 @@ | ||
// 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.AspNet.Mvc.Formatters; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNet.Mvc.ApiExplorer | ||
{ | ||
/// <summary> | ||
/// A possible format for the body of a request. | ||
/// </summary> | ||
public class ApiRequestFormat | ||
{ | ||
/// <summary> | ||
/// The formatter used to read this request. | ||
/// </summary> | ||
public IInputFormatter Formatter { get; set; } | ||
|
||
/// <summary> | ||
/// The media type of the request. | ||
/// </summary> | ||
public MediaTypeHeaderValue MediaType { get; set; } | ||
} | ||
} |
This file contains 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
35 changes: 35 additions & 0 deletions
35
src/Microsoft.AspNet.Mvc.Core/ApiExplorer/IApiRequestFormatMetadataProvider.cs
This file contains 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,35 @@ | ||
// 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 System; | ||
using System.Collections.Generic; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNet.Mvc.ApiExplorer | ||
{ | ||
/// <summary> | ||
/// Provides metadata information about the request format to an <c>IApiDescriptionProvider</c>. | ||
/// </summary> | ||
/// <remarks> | ||
/// An <see cref="Formatters.IInputFormatter"/> should implement this interface to expose metadata information | ||
/// to an <c>IApiDescriptionProvider</c>. | ||
/// </remarks> | ||
public interface IApiRequestFormatMetadataProvider | ||
{ | ||
/// <summary> | ||
/// Gets a filtered list of content types which are supported by the <see cref="Formatters.IInputFormatter"/> | ||
/// for the <paramref name="objectType"/> and <paramref name="contentType"/>. | ||
/// </summary> | ||
/// <param name="contentType"> | ||
/// The content type for which the supported content types are desired, or <c>null</c> if any content | ||
/// type can be used. | ||
/// </param> | ||
/// <param name="objectType"> | ||
/// The <see cref="Type"/> for which the supported content types are desired. | ||
/// </param> | ||
/// <returns>Content types which are supported by the <see cref="Formatters.IInputFormatter"/>.</returns> | ||
IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes( | ||
MediaTypeHeaderValue contentType, | ||
Type objectType); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Microsoft.AspNet.Mvc.Core/ApiExplorer/IApiRequestMetadataProvider.cs
This file contains 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,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 System.Collections.Generic; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNet.Mvc.ApiExplorer | ||
{ | ||
/// <summary> | ||
/// Provides a a set of possible content types than can be consumed by the action. | ||
/// </summary> | ||
public interface IApiRequestMetadataProvider | ||
{ | ||
/// <summary> | ||
/// Configures a collection of allowed content types which can be consumed by the action. | ||
/// </summary> | ||
void SetContentTypes(IList<MediaTypeHeaderValue> contentTypes); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.