Skip to content

Commit 7109809

Browse files
committed
Updated reading of properties typed Any
1 parent 3c2042f commit 7109809

File tree

7 files changed

+44
-18
lines changed

7 files changed

+44
-18
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ public override IOpenApiAny CreateAny()
5454
return new OpenApiBoolean(false);
5555
}
5656

57+
if (int.TryParse(value, out var intValue))
58+
{
59+
return new OpenApiInteger(intValue);
60+
}
61+
62+
if (Double.TryParse(value, out var dblValue))
63+
{
64+
return new OpenApiDouble(dblValue);
65+
}
66+
67+
if (DateTime.TryParse(value, out var datetimeValue))
68+
{
69+
return new OpenApiDateTime(datetimeValue);
70+
}
5771
// TODO: add more codes to identify each primitive types
5872

5973

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

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

6-
using Microsoft.OpenApi.Extensions;
6+
77
using Microsoft.OpenApi.Models;
88
using Microsoft.OpenApi.Readers.ParseNodes;
9+
using System.Collections.Generic;
910

1011
namespace Microsoft.OpenApi.Readers.V2
1112
{
@@ -15,24 +16,35 @@ namespace Microsoft.OpenApi.Readers.V2
1516
/// </summary>
1617
internal static partial class OpenApiV2Deserializer
1718
{
18-
private static readonly FixedFieldMap<OpenApiExample> ExampleFixedFields = new FixedFieldMap<OpenApiExample>();
19-
20-
private static readonly PatternFieldMap<OpenApiExample> ExamplePatternFields =
21-
new PatternFieldMap<OpenApiExample>
19+
public static void LoadExamples(OpenApiResponse response, ParseNode node)
20+
{
21+
var mapNode = node.CheckMapNode("examples");
22+
foreach (var mediaTypeNode in mapNode)
2223
{
23-
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
24-
};
24+
LoadExample(response, mediaTypeNode.Name, mediaTypeNode.Value);
25+
}
26+
}
2527

26-
public static OpenApiExample LoadExample(ParseNode node)
28+
public static void LoadExample(OpenApiResponse response, string mediaType, ParseNode node)
2729
{
28-
var mapNode = node.CheckMapNode("Example");
29-
var example = new OpenApiExample();
30-
foreach (var property in mapNode)
30+
var exampleNode = node.CreateAny();
31+
32+
if (response.Content == null)
33+
{
34+
response.Content = new Dictionary<string, OpenApiMediaType>();
35+
}
36+
OpenApiMediaType mediaTypeObject;
37+
if (response.Content.ContainsKey(mediaType))
38+
{
39+
mediaTypeObject = response.Content[mediaType];
40+
}
41+
else
3142
{
32-
property.ParseField(example, ExampleFixedFields, ExamplePatternFields);
43+
mediaTypeObject = new OpenApiMediaType();
44+
response.Content.Add(mediaType, mediaTypeObject);
3345
}
46+
mediaTypeObject.Example = exampleNode;
3447

35-
return example;
3648
}
3749
}
3850
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal static partial class OpenApiV2Deserializer
3333
{
3434
"examples", (o, n) =>
3535
{
36-
/*o.Examples = ((ListNode)n).Select(s=> new AnyNode(s)).ToList();*/
36+
OpenApiV2Deserializer.LoadExamples(o,n);
3737
}
3838
},
3939
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ internal static partial class OpenApiV2Deserializer
181181
{
182182
"example", (o, n) =>
183183
{
184-
o.Example = new OpenApiString(n.GetScalarValue());
184+
o.Example = n.CreateAny();
185185
}
186186
},
187187
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal static partial class OpenApiV3Deserializer
3535
{
3636
"example", (o, n) =>
3737
{
38-
o.Example = new OpenApiString(n.GetScalarValue());
38+
o.Example = n.CreateAny();
3939
}
4040
},
4141
//Encoding

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ internal static partial class OpenApiV3Deserializer
210210
{
211211
"example", (o, n) =>
212212
{
213-
o.Example = new OpenApiString(n.GetScalarValue());
213+
o.Example = n.CreateAny();
214214
}
215215
},
216216
{

test/Microsoft.OpenApi.Readers.Tests/InfoTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void ParseCompleteHeaderOpenApi()
6666
info.Extensions["x-list"].Should().BeOfType<OpenApiArray>();
6767
var arrayValue = (OpenApiArray)(info.Extensions["x-list"]);
6868
arrayValue.Count.Should().Be(2);
69-
arrayValue.Select(e => ((OpenApiString)e).Value).Should().Equal("1", "2");
69+
arrayValue.Select(e => ((OpenApiInteger)e).Value).Should().Equal(1, 2);
7070
}
7171
}
7272
}

0 commit comments

Comments
 (0)