Skip to content

Commit 6b599f0

Browse files
authored
Merge pull request #117 from Microsoft/dm/OpenApiAny
Updated all properties that are defined as Any to use OpenApiAny
2 parents 60e3466 + 7f4674e commit 6b599f0

File tree

9 files changed

+169
-140
lines changed

9 files changed

+169
-140
lines changed

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

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

57-
// TODO: add more codes to identify each primitive types
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); // Note(darrmi): This may be better as decimal. Further investigation required.
65+
}
66+
67+
if (DateTime.TryParse(value, out var datetimeValue))
68+
{
69+
return new OpenApiDateTime(datetimeValue);
70+
}
5871

5972

6073
// if we can't identify the type of value, return it as string.

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 0 additions & 48 deletions
This file was deleted.

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

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

Lines changed: 32 additions & 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+
LoadExamples(o,n);
3737
}
3838
},
3939
{
@@ -72,6 +72,37 @@ private static void ProcessProduces(OpenApiResponse response, ParsingContext con
7272
}
7373
}
7474

75+
private static void LoadExamples(OpenApiResponse response, ParseNode node)
76+
{
77+
var mapNode = node.CheckMapNode("examples");
78+
foreach (var mediaTypeNode in mapNode)
79+
{
80+
LoadExample(response, mediaTypeNode.Name, mediaTypeNode.Value);
81+
}
82+
}
83+
84+
private static void LoadExample(OpenApiResponse response, string mediaType, ParseNode node)
85+
{
86+
var exampleNode = node.CreateAny();
87+
88+
if (response.Content == null)
89+
{
90+
response.Content = new Dictionary<string, OpenApiMediaType>();
91+
}
92+
OpenApiMediaType mediaTypeObject;
93+
if (response.Content.ContainsKey(mediaType))
94+
{
95+
mediaTypeObject = response.Content[mediaType];
96+
}
97+
else
98+
{
99+
mediaTypeObject = new OpenApiMediaType();
100+
response.Content.Add(mediaType, mediaTypeObject);
101+
}
102+
mediaTypeObject.Example = exampleNode;
103+
104+
}
105+
75106
public static OpenApiResponse LoadResponse(ParseNode node)
76107
{
77108
var mapNode = node.CheckMapNode("response");

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
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 FluentAssertions;
7+
using Microsoft.OpenApi.Any;
8+
using Microsoft.OpenApi.Readers.ParseNodes;
9+
using Microsoft.OpenApi.Readers.V3;
10+
using SharpYaml.Serialization;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.IO;
14+
using System.Linq;
15+
using System.Text;
16+
using System.Threading.Tasks;
17+
using Xunit;
18+
19+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
20+
{
21+
[Collection("DefaultSettings")]
22+
public class OpenApiAnyTests
23+
{
24+
25+
[Fact]
26+
public void ParseMapAsAnyShouldSucceed()
27+
{
28+
29+
var input = @"
30+
aString: fooBar
31+
aInteger: 10
32+
aDouble: 2.34
33+
aDateTime: 2017-01-01
34+
";
35+
var yamlStream = new YamlStream();
36+
yamlStream.Load(new StringReader(input));
37+
var yamlNode = yamlStream.Documents.First().RootNode;
38+
39+
var context = new ParsingContext();
40+
var diagnostic = new OpenApiDiagnostic();
41+
42+
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
43+
44+
var anyMap = node.CreateAny();
45+
46+
diagnostic.Errors.Should().BeEmpty();
47+
48+
anyMap.ShouldBeEquivalentTo(
49+
new OpenApiObject
50+
{
51+
["aString"] = new OpenApiString("fooBar"),
52+
["aInteger"] = new OpenApiInteger(10),
53+
["aDouble"] = new OpenApiDouble(2.34),
54+
["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01"))
55+
});
56+
}
57+
58+
59+
[Fact]
60+
public void ParseListAsAnyShouldSucceed()
61+
{
62+
63+
var input = @"
64+
- fooBar
65+
- 10
66+
- 2.34
67+
- 2017-01-01
68+
";
69+
var yamlStream = new YamlStream();
70+
yamlStream.Load(new StringReader(input));
71+
var yamlNode = yamlStream.Documents.First().RootNode;
72+
73+
var context = new ParsingContext();
74+
var diagnostic = new OpenApiDiagnostic();
75+
76+
var node = new ListNode(context, diagnostic, (YamlSequenceNode)yamlNode);
77+
78+
var any = node.CreateAny();
79+
80+
diagnostic.Errors.Should().BeEmpty();
81+
82+
any.ShouldBeEquivalentTo(
83+
new OpenApiArray
84+
{
85+
new OpenApiString("fooBar"),
86+
new OpenApiInteger(10),
87+
new OpenApiDouble(2.34),
88+
new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01"))
89+
});
90+
}
91+
92+
[Fact]
93+
public void ParseScalarIntegertAsAnyShouldSucceed()
94+
{
95+
96+
var input = @"
97+
10
98+
";
99+
var yamlStream = new YamlStream();
100+
yamlStream.Load(new StringReader(input));
101+
var yamlNode = yamlStream.Documents.First().RootNode;
102+
103+
var context = new ParsingContext();
104+
var diagnostic = new OpenApiDiagnostic();
105+
106+
var node = new ValueNode(context, diagnostic, (YamlScalarNode)yamlNode);
107+
108+
var any = node.CreateAny();
109+
110+
diagnostic.Errors.Should().BeEmpty();
111+
112+
any.ShouldBeEquivalentTo(
113+
new OpenApiInteger(10)
114+
);
115+
}
116+
117+
}
118+
}
119+

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiInfoTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public void ParseAdvancedInfoShouldSucceed()
7373
},
7474
["x-list"] = new OpenApiArray
7575
{
76-
new OpenApiString("1"),
77-
new OpenApiString("2")
76+
new OpenApiInteger(1),
77+
new OpenApiInteger(2)
7878
}
7979
}
8080
});

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ public void ParseBasicSchemaWithExampleShouldSucceed()
173173
Example = new OpenApiObject
174174
{
175175
["name"] = new OpenApiString("Puma"),
176-
// TODO: Issue #26. This should be properly parsed as a number.
177-
["id"] = new OpenApiString("1")
176+
["id"] = new OpenApiInteger(1)
178177
}
179178
});
180179
}

0 commit comments

Comments
 (0)