-
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 3 commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -59,6 +59,7 @@ namespace Microsoft.AspNetCore.OpenApi.Generated | |||||
using Microsoft.AspNetCore.Mvc.Controllers; | ||||||
using Microsoft.Extensions.DependencyInjection; | ||||||
using Microsoft.OpenApi.Models; | ||||||
using Microsoft.OpenApi.Models.Interfaces; | ||||||
using Microsoft.OpenApi.Models.References; | ||||||
using Microsoft.OpenApi.Any; | ||||||
|
||||||
|
@@ -341,9 +342,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform | |||||
var operationParameter = operation.Parameters?.SingleOrDefault(parameter => parameter.Name == parameterComment.Name); | ||||||
if (operationParameter is not null) | ||||||
{ | ||||||
var targetOperationParameter = operationParameter is OpenApiParameterReference reference | ||||||
? reference.Target | ||||||
: (OpenApiParameter)operationParameter; | ||||||
var targetOperationParameter = UnwrapOpenApiParameter(operationParameter); | ||||||
targetOperationParameter.Description = parameterComment.Description; | ||||||
if (parameterComment.Example is { } jsonString) | ||||||
{ | ||||||
|
@@ -359,7 +358,12 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform | |||||
requestBody.Description = parameterComment.Description; | ||||||
if (parameterComment.Example is { } jsonString) | ||||||
{ | ||||||
foreach (var mediaType in requestBody.Content.Values) | ||||||
var content = requestBody?.Content?.Values; | ||||||
if (content is null) | ||||||
{ | ||||||
continue; | ||||||
} | ||||||
foreach (var mediaType in content) | ||||||
{ | ||||||
mediaType.Example = jsonString.Parse(); | ||||||
} | ||||||
|
@@ -383,6 +387,29 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform | |||||
|
||||||
return Task.CompletedTask; | ||||||
} | ||||||
|
||||||
private static OpenApiParameter UnwrapOpenApiParameter(IOpenApiParameter sourceParameter) | ||||||
{ | ||||||
if (sourceParameter is OpenApiParameterReference parameterReference) | ||||||
{ | ||||||
if (parameterReference.Target is OpenApiParameter target) | ||||||
{ | ||||||
return target; | ||||||
} | ||||||
else | ||||||
{ | ||||||
throw new InvalidOperationException($"The input schema must be an {nameof(OpenApiParameter)} or {nameof(OpenApiParameterReference)}."); | ||||||
} | ||||||
} | ||||||
else if (sourceParameter is OpenApiParameter directParameter) | ||||||
{ | ||||||
return directParameter; | ||||||
} | ||||||
else | ||||||
{ | ||||||
throw new InvalidOperationException("The input schema must be an {nameof(OpenApiParameter)} or {nameof(OpenApiParameterReference)}."); | ||||||
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.
Suggested change
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. omg I hate myself 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. This one did make me feel bad 😅 when I spotted it in the snapshots 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. it's ok I groaned harder on the other whitespace issue I really wish there was a nicer templating story for source generated files so you could catch these kinds of things more easily |
||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
{{GeneratedCodeAttribute}} | ||||||
|
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?