-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiPathItemDeserializer.cs
More file actions
106 lines (95 loc) · 4.24 KB
/
OpenApiPathItemDeserializer.cs
File metadata and controls
106 lines (95 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
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;
namespace Microsoft.OpenApi.Reader.V2
{
/// <summary>
/// Class containing logic to deserialize Open API V2 document into
/// runtime Open API object model.
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static readonly FixedFieldMap<OpenApiPathItem> _pathItemFixedFields = new()
{
{"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
},
};
private static readonly PatternFieldMap<OpenApiPathItem> _pathItemPatternFields =
new()
{
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))},
};
public static OpenApiPathItem LoadPathItem(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("PathItem");
var pathItem = new OpenApiPathItem();
ParseMap(mapNode, pathItem, _pathItemFixedFields, _pathItemPatternFields, doc: hostDocument);
return pathItem;
}
private static void LoadPathParameters(OpenApiPathItem pathItem, ParseNode node, OpenApiDocument hostDocument)
{
node.Context.SetTempStorage(TempStorageKeys.BodyParameter, null);
node.Context.SetTempStorage(TempStorageKeys.FormParameters, null);
pathItem.Parameters = node.CreateList(LoadParameter, hostDocument);
// Build request body based on information determined while parsing OpenApiOperation
var bodyParameter = node.Context.GetFromTempStorage<OpenApiParameter>(TempStorageKeys.BodyParameter);
if (bodyParameter != null)
{
var requestBody = CreateRequestBody(node.Context, bodyParameter);
foreach (var opPair in pathItem.Operations.Where(x => x.Value.RequestBody is null))
{
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
)
{
opPair.Value.RequestBody = requestBody;
}
}
}
else
{
var formParameters = node.Context.GetFromTempStorage<List<OpenApiParameter>>(TempStorageKeys.FormParameters);
if (formParameters != null)
{
var requestBody = CreateFormBody(node.Context, formParameters);
foreach (var opPair in pathItem.Operations.Where(x => x.Value.RequestBody is null))
{
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
)
{
opPair.Value.RequestBody = requestBody;
}
}
}
}
}
}
}