Skip to content

Commit 8480542

Browse files
committed
test: adds missing unit tests for media type
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 27a986c commit 8480542

File tree

9 files changed

+297
-0
lines changed

9 files changed

+297
-0
lines changed

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiMediaTypeTests.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Threading.Tasks;
7+
using FluentAssertions;
68
using Microsoft.OpenApi.Reader;
9+
using Microsoft.OpenApi.Reader.V31;
710
using Microsoft.OpenApi.Tests;
811
using Xunit;
912

@@ -14,6 +17,104 @@ public class OpenApiMediaTypeTests
1417
{
1518
private const string SampleFolderPath = "V31Tests/Samples/OpenApiMediaType/";
1619

20+
[Fact]
21+
public async Task ParseMediaTypeWithExampleShouldSucceed()
22+
{
23+
// Act
24+
var mediaType = await OpenApiModelFactory.LoadAsync<OpenApiMediaType>(Path.Combine(SampleFolderPath, "mediaTypeWithExample.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
25+
26+
// Assert
27+
mediaType.Should().BeEquivalentTo(
28+
new OpenApiMediaType
29+
{
30+
Example = 5,
31+
Schema = new OpenApiSchema()
32+
{
33+
Type = JsonSchemaType.Number,
34+
Format = "float"
35+
}
36+
}, options => options.IgnoringCyclicReferences()
37+
.Excluding(m => m.Example.Parent)
38+
);
39+
}
40+
41+
[Fact]
42+
public async Task ParseMediaTypeWithExamplesShouldSucceed()
43+
{
44+
// Act
45+
var mediaType = await OpenApiModelFactory.LoadAsync<OpenApiMediaType>(Path.Combine(SampleFolderPath, "mediaTypeWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_1, new(), SettingsFixture.ReaderSettings);
46+
47+
// Assert
48+
mediaType.Should().BeEquivalentTo(
49+
new OpenApiMediaType
50+
{
51+
Examples = new Dictionary<string, IOpenApiExample>
52+
{
53+
["example1"] = new OpenApiExample()
54+
{
55+
Value = 5
56+
},
57+
["example2"] = new OpenApiExample()
58+
{
59+
Value = 7.5
60+
}
61+
},
62+
Schema = new OpenApiSchema()
63+
{
64+
Type = JsonSchemaType.Number,
65+
Format = "float"
66+
}
67+
}, options => options.IgnoringCyclicReferences()
68+
.Excluding(m => m.Examples["example1"].Value.Parent)
69+
.Excluding(m => m.Examples["example2"].Value.Parent));
70+
}
71+
72+
[Fact]
73+
public async Task ParseMediaTypeWithEmptyArrayInExamplesWorks()
74+
{
75+
// Arrange
76+
var expected = @"{
77+
""schema"": {
78+
""type"": ""array"",
79+
""items"": {
80+
""type"": ""object"",
81+
""properties"": {
82+
""id"": {
83+
""type"": ""string""
84+
}
85+
}
86+
}
87+
},
88+
""examples"": {
89+
""Success response - no results"": {
90+
""summary"": ""empty array summary"",
91+
""description"": ""empty array description"",
92+
""value"": [ ]
93+
},
94+
""Success response - with results"": {
95+
""summary"": ""array summary"",
96+
""description"": ""array description"",
97+
""value"": [
98+
1
99+
]
100+
}
101+
}
102+
}
103+
";
104+
MapNode node;
105+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "examplesWithEmptyArray.json")))
106+
{
107+
node = TestHelper.CreateYamlMapNode(stream);
108+
}
109+
110+
// Act
111+
var mediaType = OpenApiV31Deserializer.LoadMediaType(node, new());
112+
var serialized = await mediaType.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1);
113+
114+
// Assert
115+
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), serialized.MakeLineBreaksEnvironmentNeutral());
116+
}
117+
17118
[Fact]
18119
public async Task ParseMediaTypeWithXOaiItemSchemaShouldSucceed()
19120
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schema": {
3+
"type": "array",
4+
"items": {
5+
"type": "object",
6+
"properties": {
7+
"id": {
8+
"type": "string"
9+
}
10+
}
11+
}
12+
},
13+
"examples": {
14+
"Success response - no results": {
15+
"summary": "empty array summary",
16+
"description": "empty array description",
17+
"value": []
18+
},
19+
"Success response - with results": {
20+
"summary": "array summary",
21+
"description": "array description",
22+
"value": [ 1 ]
23+
}
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: username
2+
in: null
3+
description: username to fetch
4+
required: true
5+
example: 5
6+
schema:
7+
type: number
8+
format: float
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: username
2+
in: null
3+
description: username to fetch
4+
required: true
5+
examples:
6+
example1:
7+
value: 5
8+
example2:
9+
value: 7.5
10+
schema:
11+
type: number
12+
format: float

test/Microsoft.OpenApi.Readers.Tests/V32Tests/OpenApiMediaTypeTests.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Threading.Tasks;
7+
using FluentAssertions;
68
using Microsoft.OpenApi.Reader;
9+
using Microsoft.OpenApi.Reader.V32;
710
using Microsoft.OpenApi.Tests;
811
using Xunit;
912

@@ -14,6 +17,104 @@ public class OpenApiMediaTypeTests
1417
{
1518
private const string SampleFolderPath = "V32Tests/Samples/OpenApiMediaType/";
1619

20+
[Fact]
21+
public async Task ParseMediaTypeWithExampleShouldSucceed()
22+
{
23+
// Act
24+
var mediaType = await OpenApiModelFactory.LoadAsync<OpenApiMediaType>(Path.Combine(SampleFolderPath, "mediaTypeWithExample.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
25+
26+
// Assert
27+
mediaType.Should().BeEquivalentTo(
28+
new OpenApiMediaType
29+
{
30+
Example = 5,
31+
Schema = new OpenApiSchema()
32+
{
33+
Type = JsonSchemaType.Number,
34+
Format = "float"
35+
}
36+
}, options => options.IgnoringCyclicReferences()
37+
.Excluding(m => m.Example.Parent)
38+
);
39+
}
40+
41+
[Fact]
42+
public async Task ParseMediaTypeWithExamplesShouldSucceed()
43+
{
44+
// Act
45+
var mediaType = await OpenApiModelFactory.LoadAsync<OpenApiMediaType>(Path.Combine(SampleFolderPath, "mediaTypeWithExamples.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);
46+
47+
// Assert
48+
mediaType.Should().BeEquivalentTo(
49+
new OpenApiMediaType
50+
{
51+
Examples = new Dictionary<string, IOpenApiExample>
52+
{
53+
["example1"] = new OpenApiExample()
54+
{
55+
Value = 5
56+
},
57+
["example2"] = new OpenApiExample()
58+
{
59+
Value = 7.5
60+
}
61+
},
62+
Schema = new OpenApiSchema()
63+
{
64+
Type = JsonSchemaType.Number,
65+
Format = "float"
66+
}
67+
}, options => options.IgnoringCyclicReferences()
68+
.Excluding(m => m.Examples["example1"].Value.Parent)
69+
.Excluding(m => m.Examples["example2"].Value.Parent));
70+
}
71+
72+
[Fact]
73+
public async Task ParseMediaTypeWithEmptyArrayInExamplesWorks()
74+
{
75+
// Arrange
76+
var expected = @"{
77+
""schema"": {
78+
""type"": ""array"",
79+
""items"": {
80+
""type"": ""object"",
81+
""properties"": {
82+
""id"": {
83+
""type"": ""string""
84+
}
85+
}
86+
}
87+
},
88+
""examples"": {
89+
""Success response - no results"": {
90+
""summary"": ""empty array summary"",
91+
""description"": ""empty array description"",
92+
""value"": [ ]
93+
},
94+
""Success response - with results"": {
95+
""summary"": ""array summary"",
96+
""description"": ""array description"",
97+
""value"": [
98+
1
99+
]
100+
}
101+
}
102+
}
103+
";
104+
MapNode node;
105+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "examplesWithEmptyArray.json")))
106+
{
107+
node = TestHelper.CreateYamlMapNode(stream);
108+
}
109+
110+
// Act
111+
var mediaType = OpenApiV32Deserializer.LoadMediaType(node, new());
112+
var serialized = await mediaType.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2);
113+
114+
// Assert
115+
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), serialized.MakeLineBreaksEnvironmentNeutral());
116+
}
117+
17118
[Fact]
18119
public async Task ParseMediaTypeWithItemSchemaShouldSucceed()
19120
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schema": {
3+
"type": "array",
4+
"items": {
5+
"type": "object",
6+
"properties": {
7+
"id": {
8+
"type": "string"
9+
}
10+
}
11+
}
12+
},
13+
"examples": {
14+
"Success response - no results": {
15+
"summary": "empty array summary",
16+
"description": "empty array description",
17+
"value": []
18+
},
19+
"Success response - with results": {
20+
"summary": "array summary",
21+
"description": "array description",
22+
"value": [ 1 ]
23+
}
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: username
2+
in: null
3+
description: username to fetch
4+
required: true
5+
example: 5
6+
schema:
7+
type: number
8+
format: float
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: username
2+
in: null
3+
description: username to fetch
4+
required: true
5+
examples:
6+
example1:
7+
value: 5
8+
example2:
9+
value: 7.5
10+
schema:
11+
type: number
12+
format: float
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
schema:
2+
type: array
3+
x-oai-itemSchema:
4+
type: string
5+
maxLength: 100

0 commit comments

Comments
 (0)