-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Update to Microsoft.OpenApi v2.0.0-preview.17 #61541
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
a06f8b0
daa8e00
4c45af2
ef254f4
b9841dc
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 |
---|---|---|
|
@@ -4,30 +4,30 @@ | |
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Routing.Patterns; | ||
using Microsoft.OpenApi.Models; | ||
|
||
internal static class ApiDescriptionExtensions | ||
{ | ||
/// <summary> | ||
/// Maps the HTTP method of the ApiDescription to the OpenAPI <see cref="OperationType"/> . | ||
/// Maps the HTTP method of the ApiDescription to the HttpMethod. | ||
/// </summary> | ||
/// <param name="apiDescription">The ApiDescription to resolve an operation type from.</param> | ||
/// <returns>The <see cref="OperationType"/> associated with the given <paramref name="apiDescription"/>.</returns> | ||
public static OperationType GetOperationType(this ApiDescription apiDescription) => | ||
/// <param name="apiDescription">The ApiDescription to resolve an HttpMethod from.</param> | ||
/// <returns>The <see cref="HttpMethod"/> associated with the given <paramref name="apiDescription"/>.</returns> | ||
public static HttpMethod GetHttpMethod(this ApiDescription apiDescription) => | ||
apiDescription.HttpMethod?.ToUpperInvariant() switch | ||
{ | ||
"GET" => OperationType.Get, | ||
"POST" => OperationType.Post, | ||
"PUT" => OperationType.Put, | ||
"DELETE" => OperationType.Delete, | ||
"PATCH" => OperationType.Patch, | ||
"HEAD" => OperationType.Head, | ||
"OPTIONS" => OperationType.Options, | ||
"TRACE" => OperationType.Trace, | ||
"GET" => HttpMethod.Get, | ||
"POST" => HttpMethod.Post, | ||
"PUT" => HttpMethod.Put, | ||
"DELETE" => HttpMethod.Delete, | ||
"PATCH" => HttpMethod.Patch, | ||
"HEAD" => HttpMethod.Head, | ||
"OPTIONS" => HttpMethod.Options, | ||
"TRACE" => HttpMethod.Trace, | ||
_ => throw new InvalidOperationException($"Unsupported HTTP method: {apiDescription.HttpMethod}"), | ||
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. IIRC there's a bug logged somewhere about an exception being throw for the empty string in MVC instead of being a GET. If you're touching this area maybe fix that here? 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. 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 the right fix for that bug is to fix the ApiExplorer layer so that |
||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.OpenApi.Models; | ||
using System.Net.Http; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
|
@@ -14,19 +14,19 @@ internal static class OpenApiConstants | |
internal const string SchemaId = "x-schema-id"; | ||
internal const string RefId = "x-ref-id"; | ||
internal const string DefaultOpenApiResponseKey = "default"; | ||
// Since there's a finite set of operation types that can be included in a given | ||
// OpenApiPaths, we can pre-allocate an array of these types and use a direct | ||
// Since there's a finite set of HTTP methods that can be included in a given | ||
// OpenApiPaths, we can pre-allocate an array of these methods and use a direct | ||
// lookup on the OpenApiPaths dictionary to avoid allocating an enumerator | ||
// over the KeyValuePairs in OpenApiPaths. | ||
internal static readonly OperationType[] OperationTypes = [ | ||
OperationType.Get, | ||
OperationType.Post, | ||
OperationType.Put, | ||
OperationType.Delete, | ||
OperationType.Options, | ||
OperationType.Head, | ||
OperationType.Patch, | ||
OperationType.Trace | ||
internal static readonly HttpMethod[] HttpMethods = [ | ||
HttpMethod.Get, | ||
HttpMethod.Post, | ||
HttpMethod.Put, | ||
HttpMethod.Delete, | ||
HttpMethod.Options, | ||
HttpMethod.Head, | ||
HttpMethod.Patch, | ||
HttpMethod.Trace | ||
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've seen an issue that there's a new QUERY method coming soon, so that might need adding shortly. 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. 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. AFAIK, the OpenAPI spec doesn't yet support all HTTP methods. There's been discussion of loosening this requirement in the past but at the moment it only supports this fixed set. |
||
]; | ||
// Represents primitive types that should never be represented as | ||
// a schema reference and always inlined. | ||
|
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.
I like this - I might borrow it for Swashbuckle 🙂
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.
Hmmm -- I wonder if it might be worth integrating into the Microsoft.OpenAPI APIs? Let me know how it shakes out for you in practice and we can consider moving it to the base library?