Skip to content

Commit a2337db

Browse files
committed
- Add deserializer for Discriminator, Xml, Encoding
- Fix deserializer for SecurityScheme, Schema, Document - Update V2Tests
1 parent 74a90eb commit a2337db

13 files changed

+358
-20
lines changed

src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System;
67
using System.Collections.Generic;
78
using Microsoft.OpenApi.Extensions;
89
using Microsoft.OpenApi.Models;
@@ -21,7 +22,7 @@ internal static partial class OpenApiV2Deserializer
2122
{
2223
"swagger", (o, n) =>
2324
{
24-
/* Ignore it */
25+
o.SpecVersion = new Version(2, 0);
2526
}
2627
},
2728
{"info", (o, n) => o.Info = LoadInfo(n)},
@@ -31,10 +32,7 @@ internal static partial class OpenApiV2Deserializer
3132
"schemes", (o, n) => n.Context.SetTempStorage(
3233
"schemes",
3334
n.CreateSimpleList(
34-
s =>
35-
{
36-
return s.GetScalarValue();
37-
}))
35+
s => s.GetScalarValue()))
3836
},
3937
{
4038
"consumes",

src/Microsoft.OpenApi.Readers/V2/OpenApiSchemaDeserializer.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,27 @@ internal static partial class OpenApiV2Deserializer
163163
o.Default = new OpenApiString(n.GetScalarValue());
164164
}
165165
},
166-
167-
// discriminator
166+
{
167+
"discriminator", (o, n) =>
168+
{
169+
o.Discriminator = new OpenApiDiscriminator()
170+
{
171+
PropertyName = n.GetScalarValue()
172+
};
173+
}
174+
},
168175
{
169176
"readOnly", (o, n) =>
170177
{
171178
o.ReadOnly = bool.Parse(n.GetScalarValue());
172179
}
173180
},
174-
// xml
181+
{
182+
"xml", (o, n) =>
183+
{
184+
o.Xml = LoadXml(n);
185+
}
186+
},
175187
{
176188
"externalDocs", (o, n) =>
177189
{

src/Microsoft.OpenApi.Readers/V2/OpenApiSecuritySchemeDeserializer.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace Microsoft.OpenApi.Readers.V2
1616
/// </summary>
1717
internal static partial class OpenApiV2Deserializer
1818
{
19+
private static string flowValue;
20+
21+
private static readonly OpenApiOAuthFlow flow = new OpenApiOAuthFlow();
22+
1923
private static readonly FixedFieldMap<OpenApiSecurityScheme> SecuritySchemeFixedFields =
2024
new FixedFieldMap<OpenApiSecurityScheme>
2125
{
@@ -26,13 +30,32 @@ internal static partial class OpenApiV2Deserializer
2630
{"description", (o, n) => o.Description = n.GetScalarValue()},
2731
{"name", (o, n) => o.Name = n.GetScalarValue()},
2832
{"in", (o, n) => o.In = n.GetScalarValue().GetEnumFromDisplayName<ParameterLocation>()},
29-
{"scheme", (o, n) => o.Scheme = n.GetScalarValue()},
30-
{"bearerFormat", (o, n) => o.BearerFormat = n.GetScalarValue()},
3133
{
32-
"openIdConnectUrl",
33-
(o, n) => o.OpenIdConnectUrl = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute)
34+
"flow", (o, n) =>
35+
{
36+
flowValue = n.GetScalarValue();
37+
}
3438
},
35-
{"flows", (o, n) => o.Flows = LoadOAuthFlows(n)}
39+
{
40+
"authorizationUrl",
41+
(o, n) =>
42+
{
43+
flow.AuthorizationUrl = new Uri(n.GetScalarValue());
44+
}
45+
},
46+
{
47+
"tokenUrl",
48+
(o, n) =>
49+
{
50+
flow.TokenUrl = new Uri(n.GetScalarValue());
51+
}
52+
},
53+
{
54+
"scopes", (o, n) =>
55+
{
56+
flow.Scopes = n.CreateMap(LoadString);
57+
}
58+
}
3659
};
3760

3861
private static readonly PatternFieldMap<OpenApiSecurityScheme> SecuritySchemePatternFields =
@@ -51,6 +74,27 @@ public static OpenApiSecurityScheme LoadSecurityScheme(ParseNode node)
5174
property.ParseField(securityScheme, SecuritySchemeFixedFields, SecuritySchemePatternFields);
5275
}
5376

77+
securityScheme.Flows = new OpenApiOAuthFlows();
78+
;
79+
80+
// Put the Flow object in the right Flows property based on the string in "flow"
81+
if (flowValue == OpenApiConstants.Implicit)
82+
{
83+
securityScheme.Flows.Implicit = flow;
84+
}
85+
else if (flowValue == OpenApiConstants.Password)
86+
{
87+
securityScheme.Flows.Password = flow;
88+
}
89+
else if (flowValue == OpenApiConstants.Application)
90+
{
91+
securityScheme.Flows.ClientCredentials = flow;
92+
}
93+
else if (flowValue == OpenApiConstants.AccessCode)
94+
{
95+
securityScheme.Flows.AuthorizationCode = flow;
96+
}
97+
5498
return securityScheme;
5599
}
56100
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System;
7+
using Microsoft.OpenApi.Any;
8+
using Microsoft.OpenApi.Extensions;
9+
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Readers.ParseNodes;
11+
12+
namespace Microsoft.OpenApi.Readers.V2
13+
{
14+
/// <summary>
15+
/// Class containing logic to deserialize Open API V3 document into
16+
/// runtime Open API object model.
17+
/// </summary>
18+
internal static partial class OpenApiV2Deserializer
19+
{
20+
private static readonly FixedFieldMap<OpenApiXml> XmlFixedFields = new FixedFieldMap<OpenApiXml>
21+
{
22+
{
23+
"name", (o, n) =>
24+
{
25+
o.Name = n.GetScalarValue();
26+
}
27+
},
28+
{
29+
"namespace", (o, n) =>
30+
{
31+
o.Namespace = new Uri(n.GetScalarValue());
32+
}
33+
},
34+
{
35+
"prefix", (o, n) =>
36+
{
37+
o.Prefix = n.GetScalarValue();
38+
}
39+
},
40+
{
41+
"attribute", (o, n) =>
42+
{
43+
o.Attribute = bool.Parse(n.GetScalarValue());
44+
}
45+
},
46+
{
47+
"wrapped", (o, n) =>
48+
{
49+
o.Wrapped = bool.Parse(n.GetScalarValue());
50+
}
51+
},
52+
};
53+
54+
private static readonly PatternFieldMap<OpenApiXml> XmlPatternFields =
55+
new PatternFieldMap<OpenApiXml>
56+
{
57+
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
58+
};
59+
60+
public static OpenApiXml LoadXml(ParseNode node)
61+
{
62+
var mapNode = node.CheckMapNode("xml");
63+
64+
var xml = new OpenApiXml();
65+
foreach (var property in mapNode)
66+
{
67+
property.ParseField(xml, XmlFixedFields, XmlPatternFields);
68+
}
69+
70+
return xml;
71+
}
72+
}
73+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using Microsoft.OpenApi.Any;
7+
using Microsoft.OpenApi.Extensions;
8+
using Microsoft.OpenApi.Models;
9+
using Microsoft.OpenApi.Readers.ParseNodes;
10+
11+
namespace Microsoft.OpenApi.Readers.V3
12+
{
13+
/// <summary>
14+
/// Class containing logic to deserialize Open API V3 document into
15+
/// runtime Open API object model.
16+
/// </summary>
17+
internal static partial class OpenApiV3Deserializer
18+
{
19+
private static readonly FixedFieldMap<OpenApiDiscriminator> DiscriminatorFixedFields =
20+
new FixedFieldMap<OpenApiDiscriminator>
21+
{
22+
{
23+
"propertyName", (o, n) =>
24+
{
25+
o.PropertyName = n.GetScalarValue();
26+
}
27+
},
28+
{
29+
"mapping", (o, n) =>
30+
{
31+
o.Mapping = n.CreateMap(LoadString);
32+
}
33+
}
34+
};
35+
36+
private static readonly PatternFieldMap<OpenApiDiscriminator> DiscriminatorPatternFields =
37+
new PatternFieldMap<OpenApiDiscriminator>
38+
{
39+
};
40+
41+
public static OpenApiDiscriminator LoadDiscriminator(ParseNode node)
42+
{
43+
var mapNode = node.CheckMapNode("discriminator");
44+
45+
var discriminator = new OpenApiDiscriminator();
46+
foreach (var property in mapNode)
47+
{
48+
property.ParseField(discriminator, DiscriminatorFixedFields, DiscriminatorPatternFields);
49+
}
50+
51+
return discriminator;
52+
}
53+
}
54+
}

src/Microsoft.OpenApi.Readers/V3/OpenApiEncodingDeserializer.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System;
7+
using Microsoft.OpenApi.Extensions;
8+
using Microsoft.OpenApi.Models;
9+
using Microsoft.OpenApi.Readers.ParseNodes;
10+
611
namespace Microsoft.OpenApi.Readers.V3
712
{
813
/// <summary>
@@ -11,5 +16,65 @@ namespace Microsoft.OpenApi.Readers.V3
1116
/// </summary>
1217
internal static partial class OpenApiV3Deserializer
1318
{
19+
private static readonly FixedFieldMap<OpenApiEncoding> EncodingFixedFields = new FixedFieldMap<OpenApiEncoding>
20+
{
21+
{
22+
"contentType", (o, n) =>
23+
{
24+
o.ContentType = n.GetScalarValue();
25+
}
26+
},
27+
{
28+
"headers", (o, n) =>
29+
{
30+
o.Headers = n.CreateMap(LoadHeader);
31+
}
32+
},
33+
{
34+
"style", (o, n) =>
35+
{
36+
ParameterStyle style;
37+
if (Enum.TryParse(n.GetScalarValue(), out style))
38+
{
39+
o.Style = style;
40+
}
41+
else
42+
{
43+
o.Style = null;
44+
}
45+
}
46+
},
47+
{
48+
"explode", (o, n) =>
49+
{
50+
o.Explode = bool.Parse(n.GetScalarValue());
51+
}
52+
},
53+
{
54+
"allowedReserved", (o, n) =>
55+
{
56+
o.AllowReserved = bool.Parse(n.GetScalarValue());
57+
}
58+
},
59+
};
60+
61+
private static readonly PatternFieldMap<OpenApiEncoding> EncodingPatternFields =
62+
new PatternFieldMap<OpenApiEncoding>
63+
{
64+
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
65+
};
66+
67+
public static OpenApiEncoding LoadEncoding(ParseNode node)
68+
{
69+
var mapNode = node.CheckMapNode("encoding");
70+
71+
var encoding = new OpenApiEncoding();
72+
foreach (var property in mapNode)
73+
{
74+
property.ParseField(encoding, EncodingFixedFields, EncodingPatternFields);
75+
}
76+
77+
return encoding;
78+
}
1479
}
1580
}

src/Microsoft.OpenApi.Readers/V3/OpenApiExampleDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal static partial class OpenApiV3Deserializer
4646

4747
public static OpenApiExample LoadExample(ParseNode node)
4848
{
49-
var mapNode = node.CheckMapNode("Example");
49+
var mapNode = node.CheckMapNode("example");
5050

5151
var pointer = mapNode.GetReferencePointer();
5252
if (pointer != null)

src/Microsoft.OpenApi.Readers/V3/OpenApiSchemaDeserializer.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ internal static partial class OpenApiV3Deserializer
187187
o.Nullable = bool.Parse(n.GetScalarValue());
188188
}
189189
},
190-
// discriminator
190+
{
191+
"discriminator", (o, n) =>
192+
{
193+
o.Discriminator = LoadDiscriminator(n);
194+
}
195+
},
191196
{
192197
"readOnly", (o, n) =>
193198
{
@@ -200,7 +205,12 @@ internal static partial class OpenApiV3Deserializer
200205
o.WriteOnly = bool.Parse(n.GetScalarValue());
201206
}
202207
},
203-
// xml
208+
{
209+
"xml", (o, n) =>
210+
{
211+
o.Xml = LoadXml(n);
212+
}
213+
},
204214
{
205215
"externalDocs", (o, n) =>
206216
{

0 commit comments

Comments
 (0)