Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ var document = new OpenApiDocument
{
["/pets"] = new OpenApiPathItem
{
Operations = new Dictionary<OperationType, OpenApiOperation>
Operations = new Dictionary<HttpMethod, OpenApiOperation>
{
[OperationType.Get] = new OpenApiOperation
[HttpMethod.Get] = new OpenApiOperation
{
Description = "Returns all pets from the system that the user has access to",
Responses = new OpenApiResponses
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using Humanizer;
Expand Down Expand Up @@ -53,11 +54,11 @@ public override void Visit(IOpenApiSchema schema)

public override void Visit(IOpenApiPathItem pathItem)
{
if (pathItem.Operations.TryGetValue(OperationType.Put, out var value) &&
if (pathItem.Operations.TryGetValue(HttpMethod.Put, out var value) &&
value.OperationId != null)
{
var operationId = value.OperationId;
pathItem.Operations[OperationType.Put].OperationId = ResolvePutOperationId(operationId);
pathItem.Operations[HttpMethod.Put].OperationId = ResolvePutOperationId(operationId);
}

base.Visit(pathItem);
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ private static async Task<OpenApiDocument> GetOpenApiAsync(HidiOptions options,
return document;
}

private static Func<string, OperationType?, OpenApiOperation, bool>? FilterOpenApiDocument(string? filterByOperationIds, string? filterByTags, Dictionary<string, List<string>> requestUrls, OpenApiDocument document, ILogger logger)
private static Func<string, HttpMethod, OpenApiOperation, bool>? FilterOpenApiDocument(string? filterByOperationIds, string? filterByTags, Dictionary<string, List<string>> requestUrls, OpenApiDocument document, ILogger logger)
{
Func<string, OperationType?, OpenApiOperation, bool>? predicate = null;
Func<string, HttpMethod, OpenApiOperation, bool>? predicate = null;

using (logger.BeginScope("Create Filter"))
{
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.OpenApi/Models/Interfaces/IOpenApiPathItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


using System.Collections.Generic;
using System.Net.Http;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Models.Interfaces;
Expand All @@ -13,7 +14,7 @@ public interface IOpenApiPathItem : IOpenApiDescribedElement, IOpenApiSummarized
/// <summary>
/// Gets the definition of operations on this path.
/// </summary>
public IDictionary<OperationType, OpenApiOperation> Operations { get; }
public IDictionary<HttpMethod, OpenApiOperation> Operations { get; }

/// <summary>
/// An alternative server array to service all operations in this path.
Expand Down
15 changes: 8 additions & 7 deletions src/Microsoft.OpenApi/Models/OpenApiPathItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Net.Http;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
Expand All @@ -13,7 +14,7 @@
/// <summary>
/// Path Item Object: to describe the operations available on a single path.
/// </summary>
public class OpenApiPathItem : IOpenApiExtensible, IOpenApiReferenceable, IOpenApiPathItem

Check warning on line 17 in src/Microsoft.OpenApi/Models/OpenApiPathItem.cs

View workflow job for this annotation

GitHub Actions / Build

'IOpenApiPathItem' implements 'IOpenApiReferenceable' so 'IOpenApiReferenceable' can be removed from the inheritance list. (https://rules.sonarsource.com/csharp/RSPEC-1939)
{
/// <inheritdoc/>
public string Summary { get; set; }
Expand All @@ -22,8 +23,8 @@
public string Description { get; set; }

/// <inheritdoc/>
public IDictionary<OperationType, OpenApiOperation> Operations { get; set; }
= new Dictionary<OperationType, OpenApiOperation>();
public IDictionary<HttpMethod, OpenApiOperation> Operations { get; set; }
= new Dictionary<HttpMethod, OpenApiOperation>();

/// <inheritdoc/>
public IList<OpenApiServer> Servers { get; set; } = [];
Expand All @@ -39,7 +40,7 @@
/// </summary>
/// <param name="operationType">The operation type kind.</param>
/// <param name="operation">The operation item.</param>
public void AddOperation(OperationType operationType, OpenApiOperation operation)
public void AddOperation(HttpMethod operationType, OpenApiOperation operation)
{
Operations[operationType] = operation;
}
Expand All @@ -57,7 +58,7 @@
Utils.CheckArgumentNull(pathItem);
Summary = pathItem.Summary ?? Summary;
Description = pathItem.Description ?? Description;
Operations = pathItem.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
Operations = pathItem.Operations != null ? new Dictionary<HttpMethod, OpenApiOperation>(pathItem.Operations) : null;
Servers = pathItem.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
Parameters = pathItem.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
Extensions = pathItem.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
Expand Down Expand Up @@ -90,16 +91,16 @@
writer.WriteStartObject();

// operations except "trace"
foreach (var operation in Operations)
{
if (operation.Key != OperationType.Trace)
if (operation.Key != HttpMethod.Trace)
{
writer.WriteOptionalObject(
operation.Key.GetDisplayName(),
operation.Key.Method.ToLowerInvariant(),
operation.Value,
(w, o) => o.SerializeAsV2(w));
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

// parameters
writer.WriteOptionalCollection(OpenApiConstants.Parameters, Parameters, (w, p) => p.SerializeAsV2(w));
Expand Down Expand Up @@ -135,7 +136,7 @@
foreach (var operation in Operations)
{
writer.WriteOptionalObject(
operation.Key.GetDisplayName(),
operation.Key.Method.ToLowerInvariant(),
operation.Value,
callback);
}
Expand Down
53 changes: 0 additions & 53 deletions src/Microsoft.OpenApi/Models/OperationType.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Net.Http;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -64,7 +65,7 @@ public string Description
}

/// <inheritdoc/>
public IDictionary<OperationType, OpenApiOperation> Operations { get => Target?.Operations; }
public IDictionary<HttpMethod, OpenApiOperation> Operations { get => Target?.Operations; }

/// <inheritdoc/>
public IList<OpenApiServer> Servers { get => Target?.Servers; }
Expand Down
48 changes: 29 additions & 19 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiPathItemDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
Expand All @@ -18,13 +19,17 @@
{
private static readonly FixedFieldMap<OpenApiPathItem> _pathItemFixedFields = new()
{
{"get", (o, n, t) => o.AddOperation(OperationType.Get, LoadOperation(n, t))},
{"put", (o, n, t) => o.AddOperation(OperationType.Put, LoadOperation(n, t))},
{"post", (o, n, t) => o.AddOperation(OperationType.Post, LoadOperation(n, t))},
{"delete", (o, n, t) => o.AddOperation(OperationType.Delete, LoadOperation(n, t))},
{"options", (o, n, t) => o.AddOperation(OperationType.Options, LoadOperation(n, t))},
{"head", (o, n, t) => o.AddOperation(OperationType.Head, LoadOperation(n, t))},
{"patch", (o, n, t) => o.AddOperation(OperationType.Patch, LoadOperation(n, t))},
{"get", (o, n, t) => o.AddOperation(HttpMethod.Get, LoadOperation(n, t))},
{"put", (o, n, t) => o.AddOperation(HttpMethod.Put, LoadOperation(n, t))},
{"post", (o, n, t) => o.AddOperation(HttpMethod.Post, LoadOperation(n, t))},
{"delete", (o, n, t) => o.AddOperation(HttpMethod.Delete, LoadOperation(n, t))},
{"options", (o, n, t) => o.AddOperation(HttpMethod.Options, LoadOperation(n, t))},
{"head", (o, n, t) => o.AddOperation(HttpMethod.Head, LoadOperation(n, t))},
#if NETSTANDARD2_1_OR_GREATER
{"patch", (o, n, t) => o.AddOperation(HttpMethod.Patch, LoadOperation(n, t))},
#else
{"patch", (o, n, t) => o.AddOperation(new HttpMethod("PATCH"), LoadOperation(n, t))},
#endif
{
"parameters",
LoadPathParameters
Expand Down Expand Up @@ -60,17 +65,19 @@
if (bodyParameter != null)
{
var requestBody = CreateRequestBody(node.Context, bodyParameter);
foreach (var opPair in pathItem.Operations.Where(x => x.Value.RequestBody is null))
{
switch (opPair.Key)
if (opPair.Key == HttpMethod.Post || opPair.Key == HttpMethod.Put
#if NETSTANDARD2_1_OR_GREATER
|| opPair.Key == HttpMethod.Patch
#else
|| opPair.Key == new HttpMethod("PATCH")
#endif
)
{
case OperationType.Post:
case OperationType.Put:
case OperationType.Patch:
opPair.Value.RequestBody = requestBody;
break;
opPair.Value.RequestBody = requestBody;
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
}
else
{
Expand All @@ -78,19 +85,22 @@
if (formParameters != null)
{
var requestBody = CreateFormBody(node.Context, formParameters);
foreach (var opPair in pathItem.Operations.Where(x => x.Value.RequestBody is null))
{
switch (opPair.Key)
if (opPair.Key == HttpMethod.Post || opPair.Key == HttpMethod.Put
#if NETSTANDARD2_1_OR_GREATER
|| opPair.Key == HttpMethod.Patch
#else
|| opPair.Key == new HttpMethod("PATCH")
#endif
)
{
case OperationType.Post:
case OperationType.Put:
case OperationType.Patch:
opPair.Value.RequestBody = requestBody;
break;
opPair.Value.RequestBody = requestBody;
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
}
}

}
}
}
21 changes: 13 additions & 8 deletions src/Microsoft.OpenApi/Reader/V3/OpenApiPathItemDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Net.Http;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
Expand All @@ -26,14 +27,18 @@ internal static partial class OpenApiV3Deserializer
"description",
(o, n, _) => o.Description = n.GetScalarValue()
},
{"get", (o, n, t) => o.AddOperation(OperationType.Get, LoadOperation(n, t))},
{"put", (o, n, t) => o.AddOperation(OperationType.Put, LoadOperation(n, t))},
{"post", (o, n, t) => o.AddOperation(OperationType.Post, LoadOperation(n, t))},
{"delete", (o, n, t) => o.AddOperation(OperationType.Delete, LoadOperation(n, t))},
{"options", (o, n, t) => o.AddOperation(OperationType.Options, LoadOperation(n, t))},
{"head", (o, n, t) => o.AddOperation(OperationType.Head, LoadOperation(n, t))},
{"patch", (o, n, t) => o.AddOperation(OperationType.Patch, LoadOperation(n, t))},
{"trace", (o, n, t) => o.AddOperation(OperationType.Trace, LoadOperation(n, t))},
{"get", (o, n, t) => o.AddOperation(HttpMethod.Get, LoadOperation(n, t))},
{"put", (o, n, t) => o.AddOperation(HttpMethod.Put, LoadOperation(n, t))},
{"post", (o, n, t) => o.AddOperation(HttpMethod.Post, LoadOperation(n, t))},
{"delete", (o, n, t) => o.AddOperation(HttpMethod.Delete, LoadOperation(n, t))},
{"options", (o, n, t) => o.AddOperation(HttpMethod.Options, LoadOperation(n, t))},
{"head", (o, n, t) => o.AddOperation(HttpMethod.Head, LoadOperation(n, t))},
#if NETSTANDARD2_1_OR_GREATER
{"patch", (o, n, t) => o.AddOperation(HttpMethod.Patch, LoadOperation(n, t))},
#else
{"patch", (o, n, t) => o.AddOperation(new HttpMethod("PATCH"), LoadOperation(n, t))},
#endif
{"trace", (o, n, t) => o.AddOperation(HttpMethod.Trace, LoadOperation(n, t))},
{"servers", (o, n, t) => o.Servers = n.CreateList(LoadServer, t)},
{"parameters", (o, n, t) => o.Parameters = n.CreateList(LoadParameter, t)}
};
Expand Down
21 changes: 13 additions & 8 deletions src/Microsoft.OpenApi/Reader/V31/OpenApiPathItemDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
Expand Down Expand Up @@ -28,14 +29,18 @@ internal static partial class OpenApiV31Deserializer
o.Description = n.GetScalarValue();
}
},
{"get", (o, n, t) => o.AddOperation(OperationType.Get, LoadOperation(n, t))},
{"put", (o, n, t) => o.AddOperation(OperationType.Put, LoadOperation(n, t))},
{"post", (o, n, t) => o.AddOperation(OperationType.Post, LoadOperation(n, t))},
{"delete", (o, n, t) => o.AddOperation(OperationType.Delete, LoadOperation(n, t))},
{"options", (o, n, t) => o.AddOperation(OperationType.Options, LoadOperation(n, t))},
{"head", (o, n, t) => o.AddOperation(OperationType.Head, LoadOperation(n, t))},
{"patch", (o, n, t) => o.AddOperation(OperationType.Patch, LoadOperation(n, t))},
{"trace", (o, n, t) => o.AddOperation(OperationType.Trace, LoadOperation(n, t))},
{"get", (o, n, t) => o.AddOperation(HttpMethod.Get, LoadOperation(n, t))},
{"put", (o, n, t) => o.AddOperation(HttpMethod.Put, LoadOperation(n, t))},
{"post", (o, n, t) => o.AddOperation(HttpMethod.Post, LoadOperation(n, t))},
{"delete", (o, n, t) => o.AddOperation(HttpMethod.Delete, LoadOperation(n, t))},
{"options", (o, n, t) => o.AddOperation(HttpMethod.Options, LoadOperation(n, t))},
{"head", (o, n, t) => o.AddOperation(HttpMethod.Head, LoadOperation(n, t))},
#if NETSTANDARD2_1_OR_GREATER
{"patch", (o, n, t) => o.AddOperation(HttpMethod.Patch, LoadOperation(n, t))},
#else
{"patch", (o, n, t) => o.AddOperation(new HttpMethod("PATCH"), LoadOperation(n, t))},
#endif
{"trace", (o, n, t) => o.AddOperation(HttpMethod.Trace, LoadOperation(n, t))},
{"servers", (o, n, t) => o.Servers = n.CreateList(LoadServer, t)},
{"parameters", (o, n, t) => o.Parameters = n.CreateList(LoadParameter, t)}
};
Expand Down
Loading