Skip to content

Commit 72959fd

Browse files
committed
- Add reader v3 tests for discriminator and xml
1 parent 5bc9159 commit 72959fd

File tree

15 files changed

+140
-18
lines changed

15 files changed

+140
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static partial class OpenApiV3Deserializer
2828
{
2929
"mapping", (o, n) =>
3030
{
31-
o.Mapping = n.CreateMap(LoadString);
31+
o.Mapping = n.CreateSimpleMap(LoadString);
3232
}
3333
}
3434
};

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
<None Include="V3Tests\Samples\OpenApiInfo\advancedInfo.yaml">
6868
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6969
</None>
70-
<None Include="V3Tests\Samples\OpenApiInfo\basicInfo.json">
71-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
70+
<None Update="V3Tests\Samples\OpenApiDiscriminator\basicDiscriminator.yaml">
71+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7272
</None>
7373
<None Update="V3Tests\Samples\OpenApiExample\advancedExample.yaml">
7474
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -88,31 +88,38 @@
8888
<None Update="V3Tests\Samples\OpenApiDocument\basicDocumentWithMultipleServers.yaml">
8989
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9090
</None>
91+
<None Update="V3Tests\Samples\OpenApiInfo\basicInfo.yaml">
92+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
93+
</None>
9194
<None Update="V3Tests\Samples\OpenApiInfo\minimalInfo.json">
9295
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9396
</None>
97+
<None Update="V3Tests\Samples\OpenApiInfo\minimalInfo.yaml">
98+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
99+
</None>
94100
<None Update="V3Tests\Samples\OpenApiInfo\minimalInfoObject.json">
95101
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
96102
</None>
97-
<None Update="V3Tests\Samples\OpenApiDocument\petStoreExpanded30.yaml">
103+
<None Update="V3Tests\Samples\OpenApiDocument\petStoreExpanded.yaml">
98104
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
99105
</None>
100-
<None Update="V3Tests\Samples\OpenApiDocument\petstore30.yaml">
106+
<None Update="V3Tests\Samples\OpenApiDocument\petStore.yaml">
101107
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
102108
</None>
103-
<None Update="V3Tests\Samples\OpenApiDocument\petStoreWithTagAndSecurity30.yaml">
109+
<None Update="V3Tests\Samples\OpenApiDocument\petStoreWithTagAndSecurity.yaml">
104110
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
105111
</None>
106112
<None Update="V3Tests\Samples\OpenApiDocument\minimalDocument.yaml">
107113
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
108114
</None>
115+
<None Update="V3Tests\Samples\OpenApiXml\basicXml.yaml">
116+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
117+
</None>
109118
</ItemGroup>
110119

111120
<ItemGroup>
112121
<Folder Include="V3Tests\Samples\OpenApiSecurityScheme\" />
113-
<Folder Include="V3Tests\Samples\OpenApiXml\" />
114122
<Folder Include="V3Tests\Samples\OpenApiSchema\" />
115-
<Folder Include="V3Tests\Samples\OpenApiDiscriminator\" />
116123
<Folder Include="V3Tests\Samples\OpenApiEncoding\" />
117124
</ItemGroup>
118125

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.IO;
7+
using System.Linq;
8+
using FluentAssertions;
9+
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Readers.ParseNodes;
11+
using Microsoft.OpenApi.Readers.V3;
12+
using SharpYaml.Serialization;
13+
using Xunit;
14+
15+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
16+
{
17+
[Collection("DefaultSettings")]
18+
public class OpenApiDiscriminatorTests
19+
{
20+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiDiscriminator";
21+
22+
[Fact]
23+
public void ParseBasicDiscriminatorShouldSucceed()
24+
{
25+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicDiscriminator.yaml")))
26+
{
27+
var yamlStream = new YamlStream();
28+
yamlStream.Load(new StreamReader(stream));
29+
var yamlNode = yamlStream.Documents.First().RootNode;
30+
31+
var context = new ParsingContext();
32+
var diagnostic = new OpenApiDiagnostic();
33+
34+
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
35+
36+
// Act
37+
var discriminator = OpenApiV3Deserializer.LoadDiscriminator(node);
38+
39+
// Assert
40+
discriminator.ShouldBeEquivalentTo(
41+
new OpenApiDiscriminator
42+
{
43+
PropertyName = "pet_type",
44+
Mapping =
45+
{
46+
["puppy"] = "#/components/schemas/Dog",
47+
["kitten"] = "Cat"
48+
}
49+
});
50+
}
51+
}
52+
}
53+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void ParseMinimalDocumentShouldSucceed()
144144
public void ParseStandardPetStoreDocumentShouldSucceed()
145145
{
146146
OpenApiDiagnostic context;
147-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStore30.yaml")))
147+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStore.yaml")))
148148
{
149149
var actual = new OpenApiStreamReader().Read(stream, out context);
150150

@@ -554,7 +554,7 @@ public void ParseStandardPetStoreDocumentShouldSucceed()
554554
public void ParseModifiedPetStoreDocumentWithTagAndSecurityShouldSucceed()
555555
{
556556
OpenApiDiagnostic context;
557-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreWithTagAndSecurity30.yaml")))
557+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreWithTagAndSecurity.yaml")))
558558
{
559559
var actual = new OpenApiStreamReader().Read(stream, out context);
560560

@@ -1056,7 +1056,7 @@ public void ParsePetStoreExpandedShouldSucceed()
10561056
{
10571057
OpenApiDiagnostic context;
10581058

1059-
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreExpanded30.yaml")))
1059+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "petStoreExpanded.yaml")))
10601060
{
10611061
var actual = new OpenApiStreamReader().Read(stream, out context);
10621062

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1818
[Collection("DefaultSettings")]
1919
public class OpenApiExampleTests
2020
{
21-
private const string sampleFolderPath = "V3Tests/Samples/OpenApiExample/";
21+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiExample/";
2222

2323
[Fact]
2424
public void ParseAdvancedExampleShouldSucceed()
2525
{
26-
using (var stream = File.OpenRead(Path.Combine(sampleFolderPath, "advancedExample.yaml")))
26+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedExample.yaml")))
2727
{
2828
var yamlStream = new YamlStream();
2929
yamlStream.Load(new StreamReader(stream));

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ namespace Microsoft.OpenApi.Readers.Tests.V3Tests
1919
[Collection("DefaultSettings")]
2020
public class OpenApiInfoTests
2121
{
22+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiInfo";
23+
2224
[Fact]
2325
public void ParseAdvancedInfoShouldSucceed()
2426
{
25-
using (var stream = File.OpenRead("V3Tests/Samples/OpenApiInfo/advancedInfo.yaml"))
27+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "advancedInfo.yaml")))
2628
{
2729
var yamlStream = new YamlStream();
2830
yamlStream.Load(new StreamReader(stream));
@@ -56,7 +58,7 @@ public void ParseAdvancedInfoShouldSucceed()
5658
},
5759
License = new OpenApiLicense
5860
{
59-
Extensions = {["x-disclaimer"] = new OpenApiString("Sample Extension String Disclaimer")},
61+
Extensions = { ["x-disclaimer"] = new OpenApiString("Sample Extension String Disclaimer") },
6062
Name = "licenseName",
6163
Url = new Uri("http://www.example.com/url2")
6264
},
@@ -82,7 +84,7 @@ public void ParseAdvancedInfoShouldSucceed()
8284
[Fact]
8385
public void ParseBasicInfoShouldSucceed()
8486
{
85-
using (var stream = File.OpenRead("V3Tests/Samples/OpenApiInfo/basicInfo.json"))
87+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicInfo.yaml")))
8688
{
8789
var yamlStream = new YamlStream();
8890
yamlStream.Load(new StreamReader(stream));
@@ -122,7 +124,7 @@ public void ParseBasicInfoShouldSucceed()
122124
[Fact]
123125
public void ParseMinimalInfoShouldSucceed()
124126
{
125-
using (var stream = File.OpenRead("V3Tests/Samples/OpenApiInfo/minimalInfo.json"))
127+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "minimalInfo.yaml")))
126128
{
127129
var yamlStream = new YamlStream();
128130
yamlStream.Load(new StreamReader(stream));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 System.IO;
8+
using System.Linq;
9+
using FluentAssertions;
10+
using Microsoft.OpenApi.Models;
11+
using Microsoft.OpenApi.Readers.ParseNodes;
12+
using Microsoft.OpenApi.Readers.V3;
13+
using SharpYaml.Serialization;
14+
using Xunit;
15+
16+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
17+
{
18+
[Collection("DefaultSettings")]
19+
public class OpenApiXmlTests
20+
{
21+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiXml";
22+
23+
[Fact]
24+
public void ParseBasicXmlShouldSucceed()
25+
{
26+
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "basicXml.yaml")))
27+
{
28+
var yamlStream = new YamlStream();
29+
yamlStream.Load(new StreamReader(stream));
30+
var yamlNode = yamlStream.Documents.First().RootNode;
31+
32+
var context = new ParsingContext();
33+
var diagnostic = new OpenApiDiagnostic();
34+
35+
var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);
36+
37+
// Act
38+
var xml = OpenApiV3Deserializer.LoadXml(node);
39+
40+
// Assert
41+
xml.ShouldBeEquivalentTo(
42+
new OpenApiXml
43+
{
44+
Name = "name1",
45+
Namespace = new Uri("http://example.com/schema/namespaceSample"),
46+
Prefix = "samplePrefix",
47+
Wrapped = true
48+
});
49+
}
50+
}
51+
}
52+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
propertyName: pet_type
2+
mapping:
3+
puppy: '#/components/schemas/Dog'
4+
kitten: Cat

0 commit comments

Comments
 (0)