-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiHeaderDeserializer.cs
More file actions
149 lines (140 loc) · 5.36 KB
/
OpenApiHeaderDeserializer.cs
File metadata and controls
149 lines (140 loc) · 5.36 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Globalization;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Models.Interfaces;
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<OpenApiHeader> _headerFixedFields = new()
{
{
"description",
(o, n, _) => o.Description = n.GetScalarValue()
},
{
"type",
(o, n, _) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType()
},
{
"format",
(o, n, _) => GetOrCreateSchema(o).Format = n.GetScalarValue()
},
{
"items",
(o, n, doc) => GetOrCreateSchema(o).Items = LoadSchema(n, doc)
},
{
"collectionFormat",
(o, n, _) => LoadStyle(o, n.GetScalarValue())
},
{
"default",
(o, n, _) => GetOrCreateSchema(o).Default = n.CreateAny()
},
{
"maximum",
(o, n, _) => GetOrCreateSchema(o).Maximum = ParserHelper.ParseDecimalWithFallbackOnOverflow(n.GetScalarValue(), decimal.MaxValue)
},
{
"exclusiveMaximum",
(o, n, _) => GetOrCreateSchema(o).ExclusiveMaximum = bool.Parse(n.GetScalarValue())
},
{
"minimum",
(o, n, _) => GetOrCreateSchema(o).Minimum = ParserHelper.ParseDecimalWithFallbackOnOverflow(n.GetScalarValue(), decimal.MinValue)
},
{
"exclusiveMinimum",
(o, n, _) => GetOrCreateSchema(o).ExclusiveMinimum = bool.Parse(n.GetScalarValue())
},
{
"maxLength",
(o, n, _) => GetOrCreateSchema(o).MaxLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"minLength",
(o, n, _) => GetOrCreateSchema(o).MinLength = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"pattern",
(o, n, _) => GetOrCreateSchema(o).Pattern = n.GetScalarValue()
},
{
"maxItems",
(o, n, _) => GetOrCreateSchema(o).MaxItems = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"minItems",
(o, n, _) => GetOrCreateSchema(o).MinItems = int.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"uniqueItems",
(o, n, _) => GetOrCreateSchema(o).UniqueItems = bool.Parse(n.GetScalarValue())
},
{
"multipleOf",
(o, n, _) => GetOrCreateSchema(o).MultipleOf = decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture)
},
{
"enum",
(o, n, _) => GetOrCreateSchema(o).Enum = n.CreateListOfAny()
}
};
private static readonly PatternFieldMap<OpenApiHeader> _headerPatternFields = new()
{
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p, n))}
};
private static OpenApiSchema GetOrCreateSchema(OpenApiHeader p)
{
return p.Schema switch {
OpenApiSchema schema => schema,
_ => (OpenApiSchema)(p.Schema = new OpenApiSchema()),
};
}
public static IOpenApiHeader LoadHeader(ParseNode node, OpenApiDocument hostDocument)
{
var mapNode = node.CheckMapNode("header");
var header = new OpenApiHeader();
foreach (var property in mapNode)
{
property.ParseField(header, _headerFixedFields, _headerPatternFields, hostDocument);
}
var schema = node.Context.GetFromTempStorage<IOpenApiSchema>("schema");
if (schema != null)
{
header.Schema = schema;
node.Context.SetTempStorage("schema", null);
}
return header;
}
private static void LoadStyle(OpenApiHeader header, string style)
{
switch (style)
{
case "csv":
header.Style = ParameterStyle.Simple;
return;
case "ssv":
header.Style = ParameterStyle.SpaceDelimited;
return;
case "pipes":
header.Style = ParameterStyle.PipeDelimited;
return;
case "tsv":
throw new NotSupportedException();
default:
throw new OpenApiReaderException("Unrecognized header style: " + style);
}
}
}
}