Skip to content

Commit 8baff28

Browse files
authored
Merge pull request #2251 from microsoft/mw/use-http-method-object-instead-of-enum
feat: use http method object instead of enum
2 parents fb496b4 + 0c342ad commit 8baff28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+343
-358
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ var document = new OpenApiDocument
5555
{
5656
["/pets"] = new OpenApiPathItem
5757
{
58-
Operations = new Dictionary<OperationType, OpenApiOperation>
58+
Operations = new Dictionary<HttpMethod, OpenApiOperation>
5959
{
60-
[OperationType.Get] = new OpenApiOperation
60+
[HttpMethod.Get] = new OpenApiOperation
6161
{
6262
Description = "Returns all pets from the system that the user has access to",
6363
Responses = new OpenApiResponses

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Text;
56
using System.Text.RegularExpressions;
67
using Humanizer;
@@ -53,11 +54,11 @@ public override void Visit(IOpenApiSchema schema)
5354

5455
public override void Visit(IOpenApiPathItem pathItem)
5556
{
56-
if (pathItem.Operations.TryGetValue(OperationType.Put, out var value) &&
57+
if (pathItem.Operations.TryGetValue(HttpMethod.Put, out var value) &&
5758
value.OperationId != null)
5859
{
5960
var operationId = value.OperationId;
60-
pathItem.Operations[OperationType.Put].OperationId = ResolvePutOperationId(operationId);
61+
pathItem.Operations[HttpMethod.Put].OperationId = ResolvePutOperationId(operationId);
6162
}
6263

6364
base.Visit(pathItem);

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ private static async Task<OpenApiDocument> GetOpenApiAsync(HidiOptions options,
256256
return document;
257257
}
258258

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

263263
using (logger.BeginScope("Create Filter"))
264264
{

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiPathItem.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
1+

22
using System.Collections.Generic;
3+
using System.Net.Http;
34
using Microsoft.OpenApi.Interfaces;
45

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

1819
/// <summary>
1920
/// An alternative server array to service all operations in this path.

src/Microsoft.OpenApi/Models/OpenApiPathItem.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Net.Http;
67
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models.Interfaces;
@@ -22,8 +23,8 @@ public class OpenApiPathItem : IOpenApiExtensible, IOpenApiReferenceable, IOpenA
2223
public string Description { get; set; }
2324

2425
/// <inheritdoc/>
25-
public IDictionary<OperationType, OpenApiOperation> Operations { get; set; }
26-
= new Dictionary<OperationType, OpenApiOperation>();
26+
public IDictionary<HttpMethod, OpenApiOperation> Operations { get; set; }
27+
= new Dictionary<HttpMethod, OpenApiOperation>();
2728

2829
/// <inheritdoc/>
2930
public IList<OpenApiServer> Servers { get; set; } = [];
@@ -39,7 +40,7 @@ public class OpenApiPathItem : IOpenApiExtensible, IOpenApiReferenceable, IOpenA
3940
/// </summary>
4041
/// <param name="operationType">The operation type kind.</param>
4142
/// <param name="operation">The operation item.</param>
42-
public void AddOperation(OperationType operationType, OpenApiOperation operation)
43+
public void AddOperation(HttpMethod operationType, OpenApiOperation operation)
4344
{
4445
Operations[operationType] = operation;
4546
}
@@ -57,7 +58,7 @@ internal OpenApiPathItem(IOpenApiPathItem pathItem)
5758
Utils.CheckArgumentNull(pathItem);
5859
Summary = pathItem.Summary ?? Summary;
5960
Description = pathItem.Description ?? Description;
60-
Operations = pathItem.Operations != null ? new Dictionary<OperationType, OpenApiOperation>(pathItem.Operations) : null;
61+
Operations = pathItem.Operations != null ? new Dictionary<HttpMethod, OpenApiOperation>(pathItem.Operations) : null;
6162
Servers = pathItem.Servers != null ? new List<OpenApiServer>(pathItem.Servers) : null;
6263
Parameters = pathItem.Parameters != null ? new List<IOpenApiParameter>(pathItem.Parameters) : null;
6364
Extensions = pathItem.Extensions != null ? new Dictionary<string, IOpenApiExtension>(pathItem.Extensions) : null;
@@ -92,10 +93,10 @@ public void SerializeAsV2(IOpenApiWriter writer)
9293
// operations except "trace"
9394
foreach (var operation in Operations)
9495
{
95-
if (operation.Key != OperationType.Trace)
96+
if (operation.Key != HttpMethod.Trace)
9697
{
9798
writer.WriteOptionalObject(
98-
operation.Key.GetDisplayName(),
99+
operation.Key.Method.ToLowerInvariant(),
99100
operation.Value,
100101
(w, o) => o.SerializeAsV2(w));
101102
}
@@ -135,7 +136,7 @@ internal virtual void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersio
135136
foreach (var operation in Operations)
136137
{
137138
writer.WriteOptionalObject(
138-
operation.Key.GetDisplayName(),
139+
operation.Key.Method.ToLowerInvariant(),
139140
operation.Value,
140141
callback);
141142
}

src/Microsoft.OpenApi/Models/OperationType.cs

-53
This file was deleted.

src/Microsoft.OpenApi/Models/References/OpenApiPathItemReference.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Net.Http;
56
using Microsoft.OpenApi.Interfaces;
67
using Microsoft.OpenApi.Models.Interfaces;
78
using Microsoft.OpenApi.Writers;
@@ -64,7 +65,7 @@ public string Description
6465
}
6566

6667
/// <inheritdoc/>
67-
public IDictionary<OperationType, OpenApiOperation> Operations { get => Target?.Operations; }
68+
public IDictionary<HttpMethod, OpenApiOperation> Operations { get => Target?.Operations; }
6869

6970
/// <inheritdoc/>
7071
public IList<OpenApiServer> Servers { get => Target?.Servers; }

src/Microsoft.OpenApi/Reader/V2/OpenApiPathItemDeserializer.cs

+29-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Net.Http;
78
using Microsoft.OpenApi.Extensions;
89
using Microsoft.OpenApi.Models;
910
using Microsoft.OpenApi.Reader.ParseNodes;
@@ -18,13 +19,17 @@ internal static partial class OpenApiV2Deserializer
1819
{
1920
private static readonly FixedFieldMap<OpenApiPathItem> _pathItemFixedFields = new()
2021
{
21-
{"get", (o, n, t) => o.AddOperation(OperationType.Get, LoadOperation(n, t))},
22-
{"put", (o, n, t) => o.AddOperation(OperationType.Put, LoadOperation(n, t))},
23-
{"post", (o, n, t) => o.AddOperation(OperationType.Post, LoadOperation(n, t))},
24-
{"delete", (o, n, t) => o.AddOperation(OperationType.Delete, LoadOperation(n, t))},
25-
{"options", (o, n, t) => o.AddOperation(OperationType.Options, LoadOperation(n, t))},
26-
{"head", (o, n, t) => o.AddOperation(OperationType.Head, LoadOperation(n, t))},
27-
{"patch", (o, n, t) => o.AddOperation(OperationType.Patch, LoadOperation(n, t))},
22+
{"get", (o, n, t) => o.AddOperation(HttpMethod.Get, LoadOperation(n, t))},
23+
{"put", (o, n, t) => o.AddOperation(HttpMethod.Put, LoadOperation(n, t))},
24+
{"post", (o, n, t) => o.AddOperation(HttpMethod.Post, LoadOperation(n, t))},
25+
{"delete", (o, n, t) => o.AddOperation(HttpMethod.Delete, LoadOperation(n, t))},
26+
{"options", (o, n, t) => o.AddOperation(HttpMethod.Options, LoadOperation(n, t))},
27+
{"head", (o, n, t) => o.AddOperation(HttpMethod.Head, LoadOperation(n, t))},
28+
#if NETSTANDARD2_1_OR_GREATER
29+
{"patch", (o, n, t) => o.AddOperation(HttpMethod.Patch, LoadOperation(n, t))},
30+
#else
31+
{"patch", (o, n, t) => o.AddOperation(new HttpMethod("PATCH"), LoadOperation(n, t))},
32+
#endif
2833
{
2934
"parameters",
3035
LoadPathParameters
@@ -62,13 +67,15 @@ private static void LoadPathParameters(OpenApiPathItem pathItem, ParseNode node,
6267
var requestBody = CreateRequestBody(node.Context, bodyParameter);
6368
foreach (var opPair in pathItem.Operations.Where(x => x.Value.RequestBody is null))
6469
{
65-
switch (opPair.Key)
70+
if (opPair.Key == HttpMethod.Post || opPair.Key == HttpMethod.Put
71+
#if NETSTANDARD2_1_OR_GREATER
72+
|| opPair.Key == HttpMethod.Patch
73+
#else
74+
|| opPair.Key == new HttpMethod("PATCH")
75+
#endif
76+
)
6677
{
67-
case OperationType.Post:
68-
case OperationType.Put:
69-
case OperationType.Patch:
70-
opPair.Value.RequestBody = requestBody;
71-
break;
78+
opPair.Value.RequestBody = requestBody;
7279
}
7380
}
7481
}
@@ -80,17 +87,20 @@ private static void LoadPathParameters(OpenApiPathItem pathItem, ParseNode node,
8087
var requestBody = CreateFormBody(node.Context, formParameters);
8188
foreach (var opPair in pathItem.Operations.Where(x => x.Value.RequestBody is null))
8289
{
83-
switch (opPair.Key)
90+
if (opPair.Key == HttpMethod.Post || opPair.Key == HttpMethod.Put
91+
#if NETSTANDARD2_1_OR_GREATER
92+
|| opPair.Key == HttpMethod.Patch
93+
#else
94+
|| opPair.Key == new HttpMethod("PATCH")
95+
#endif
96+
)
8497
{
85-
case OperationType.Post:
86-
case OperationType.Put:
87-
case OperationType.Patch:
88-
opPair.Value.RequestBody = requestBody;
89-
break;
98+
opPair.Value.RequestBody = requestBody;
9099
}
91100
}
92101
}
93102
}
103+
94104
}
95105
}
96106
}

src/Microsoft.OpenApi/Reader/V3/OpenApiPathItemDeserializer.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Net.Http;
56
using Microsoft.OpenApi.Extensions;
67
using Microsoft.OpenApi.Models;
78
using Microsoft.OpenApi.Models.Interfaces;
@@ -26,14 +27,18 @@ internal static partial class OpenApiV3Deserializer
2627
"description",
2728
(o, n, _) => o.Description = n.GetScalarValue()
2829
},
29-
{"get", (o, n, t) => o.AddOperation(OperationType.Get, LoadOperation(n, t))},
30-
{"put", (o, n, t) => o.AddOperation(OperationType.Put, LoadOperation(n, t))},
31-
{"post", (o, n, t) => o.AddOperation(OperationType.Post, LoadOperation(n, t))},
32-
{"delete", (o, n, t) => o.AddOperation(OperationType.Delete, LoadOperation(n, t))},
33-
{"options", (o, n, t) => o.AddOperation(OperationType.Options, LoadOperation(n, t))},
34-
{"head", (o, n, t) => o.AddOperation(OperationType.Head, LoadOperation(n, t))},
35-
{"patch", (o, n, t) => o.AddOperation(OperationType.Patch, LoadOperation(n, t))},
36-
{"trace", (o, n, t) => o.AddOperation(OperationType.Trace, LoadOperation(n, t))},
30+
{"get", (o, n, t) => o.AddOperation(HttpMethod.Get, LoadOperation(n, t))},
31+
{"put", (o, n, t) => o.AddOperation(HttpMethod.Put, LoadOperation(n, t))},
32+
{"post", (o, n, t) => o.AddOperation(HttpMethod.Post, LoadOperation(n, t))},
33+
{"delete", (o, n, t) => o.AddOperation(HttpMethod.Delete, LoadOperation(n, t))},
34+
{"options", (o, n, t) => o.AddOperation(HttpMethod.Options, LoadOperation(n, t))},
35+
{"head", (o, n, t) => o.AddOperation(HttpMethod.Head, LoadOperation(n, t))},
36+
#if NETSTANDARD2_1_OR_GREATER
37+
{"patch", (o, n, t) => o.AddOperation(HttpMethod.Patch, LoadOperation(n, t))},
38+
#else
39+
{"patch", (o, n, t) => o.AddOperation(new HttpMethod("PATCH"), LoadOperation(n, t))},
40+
#endif
41+
{"trace", (o, n, t) => o.AddOperation(HttpMethod.Trace, LoadOperation(n, t))},
3742
{"servers", (o, n, t) => o.Servers = n.CreateList(LoadServer, t)},
3843
{"parameters", (o, n, t) => o.Parameters = n.CreateList(LoadParameter, t)}
3944
};

src/Microsoft.OpenApi/Reader/V31/OpenApiPathItemDeserializer.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Http;
23
using Microsoft.OpenApi.Extensions;
34
using Microsoft.OpenApi.Models;
45
using Microsoft.OpenApi.Models.Interfaces;
@@ -28,14 +29,18 @@ internal static partial class OpenApiV31Deserializer
2829
o.Description = n.GetScalarValue();
2930
}
3031
},
31-
{"get", (o, n, t) => o.AddOperation(OperationType.Get, LoadOperation(n, t))},
32-
{"put", (o, n, t) => o.AddOperation(OperationType.Put, LoadOperation(n, t))},
33-
{"post", (o, n, t) => o.AddOperation(OperationType.Post, LoadOperation(n, t))},
34-
{"delete", (o, n, t) => o.AddOperation(OperationType.Delete, LoadOperation(n, t))},
35-
{"options", (o, n, t) => o.AddOperation(OperationType.Options, LoadOperation(n, t))},
36-
{"head", (o, n, t) => o.AddOperation(OperationType.Head, LoadOperation(n, t))},
37-
{"patch", (o, n, t) => o.AddOperation(OperationType.Patch, LoadOperation(n, t))},
38-
{"trace", (o, n, t) => o.AddOperation(OperationType.Trace, LoadOperation(n, t))},
32+
{"get", (o, n, t) => o.AddOperation(HttpMethod.Get, LoadOperation(n, t))},
33+
{"put", (o, n, t) => o.AddOperation(HttpMethod.Put, LoadOperation(n, t))},
34+
{"post", (o, n, t) => o.AddOperation(HttpMethod.Post, LoadOperation(n, t))},
35+
{"delete", (o, n, t) => o.AddOperation(HttpMethod.Delete, LoadOperation(n, t))},
36+
{"options", (o, n, t) => o.AddOperation(HttpMethod.Options, LoadOperation(n, t))},
37+
{"head", (o, n, t) => o.AddOperation(HttpMethod.Head, LoadOperation(n, t))},
38+
#if NETSTANDARD2_1_OR_GREATER
39+
{"patch", (o, n, t) => o.AddOperation(HttpMethod.Patch, LoadOperation(n, t))},
40+
#else
41+
{"patch", (o, n, t) => o.AddOperation(new HttpMethod("PATCH"), LoadOperation(n, t))},
42+
#endif
43+
{"trace", (o, n, t) => o.AddOperation(HttpMethod.Trace, LoadOperation(n, t))},
3944
{"servers", (o, n, t) => o.Servers = n.CreateList(LoadServer, t)},
4045
{"parameters", (o, n, t) => o.Parameters = n.CreateList(LoadParameter, t)}
4146
};

0 commit comments

Comments
 (0)