-
Notifications
You must be signed in to change notification settings - Fork 179
Enable $batch request with minimal API #1510
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
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
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
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
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
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
103 changes: 103 additions & 0 deletions
103
src/Microsoft.AspNetCore.OData/Batch/ODataMiniBatchMiddleware.cs
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,103 @@ | ||
| //----------------------------------------------------------------------------- | ||
| // <copyright file="ODataMiniBatchMiddleware.cs" company=".NET Foundation"> | ||
| // Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
| // See License.txt in the project root for license information. | ||
| // </copyright> | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.OData.Extensions; | ||
| using Microsoft.AspNetCore.Routing; | ||
| using Microsoft.AspNetCore.Routing.Template; | ||
|
|
||
| namespace Microsoft.AspNetCore.OData.Batch; | ||
|
|
||
| /// <summary> | ||
| /// Defines the middleware for handling OData $batch requests in the minimal API scenarios. | ||
| /// This middleware essentially acts like branching middleware and redirects OData $batch | ||
| /// requests to the appropriate ODataBatchHandler. | ||
| /// </summary> | ||
| public class ODataMiniBatchMiddleware | ||
| { | ||
| internal const string MinimalApiMetadataKey = "MS_MinimalApiMetadataKey_386F76A4-5E7C-4B4D-8A20-261A23C3DD9A"; | ||
|
|
||
| private readonly RequestDelegate _next; | ||
| private readonly TemplateMatcher _routeMatcher; | ||
| private readonly ODataBatchHandler _handler; | ||
| private readonly ODataMiniMetadata _metadata; | ||
|
|
||
| /// <summary> | ||
| /// Instantiates a new instance of <see cref="ODataBatchMiddleware"/>. | ||
| /// </summary> | ||
| /// <param name="routePattern">The route pattern for the OData $batch endpoint.</param> | ||
| /// <param name="handler">The handler for processing batch requests.</param> | ||
| /// <param name="metadata">the metadata for the OData $batch endpoint.</param> | ||
| /// <param name="next">The next middleware.</param> | ||
| public ODataMiniBatchMiddleware(string routePattern, ODataBatchHandler handler, ODataMiniMetadata metadata, RequestDelegate next) | ||
| { | ||
| if (routePattern == null) | ||
| { | ||
| throw Error.ArgumentNull(nameof(routePattern)); | ||
| } | ||
|
|
||
| // ensure _routePattern starts with / | ||
| string newRouteTemplate = routePattern.StartsWith("/", StringComparison.Ordinal) ? routePattern.Substring(1) : routePattern; | ||
| RouteTemplate parsedTemplate = TemplateParser.Parse(newRouteTemplate); | ||
| _routeMatcher = new TemplateMatcher(parsedTemplate, new RouteValueDictionary()); | ||
| _handler = handler ?? throw Error.ArgumentNull(nameof(handler)); | ||
| _metadata = metadata ?? throw Error.ArgumentNull(nameof(metadata)); | ||
|
|
||
| _next = next; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Invoke the OData $Batch middleware. | ||
| /// </summary> | ||
| /// <param name="context">The http context.</param> | ||
| /// <returns>A task that can be awaited.</returns> | ||
| public async Task Invoke(HttpContext context) | ||
| { | ||
| if (context == null) | ||
| { | ||
| throw Error.ArgumentNull(nameof(context)); | ||
| } | ||
|
|
||
| // The batch middleware should not handle the options requests for cors to properly function. | ||
| bool isPostRequest = HttpMethods.IsPost(context.Request.Method); | ||
| if (!isPostRequest) | ||
| { | ||
| await _next(context); | ||
| } | ||
| else | ||
| { | ||
| string path = context.Request.Path; | ||
| RouteValueDictionary routeData = new RouteValueDictionary(); | ||
| if (_routeMatcher.TryMatch(path, routeData)) | ||
| { | ||
| if (routeData.Count > 0) | ||
| { | ||
| Merge(context.ODataFeature().BatchRouteData, routeData); | ||
| } | ||
|
|
||
| // Add the ODataMiniMetadata to the context items for later retrieval. | ||
| context.Items.Add(MinimalApiMetadataKey, _metadata); | ||
|
|
||
| await _handler.ProcessBatchAsync(context, _next); | ||
| } | ||
| else | ||
| { | ||
| await _next(context); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void Merge(RouteValueDictionary batchRouteData, RouteValueDictionary routeData) | ||
| { | ||
| foreach (var item in routeData) | ||
| { | ||
| batchRouteData.Add(item.Key, item.Value); | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
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.
What is this key used for? Is it specific for minimal API?
Can I use another value other than the one you have provided?
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.
That's only for minimal API scenario only. It's a key to save the metadata into HttpContext.Items and retrieve it from the HttpContext is it's OData $batch request.