Skip to content

Commit af85cc9

Browse files
committed
test: adds deserialization tests for OpenAPI header
Signed-off-by: Vincent Biret <[email protected]>
1 parent 0fdfae1 commit af85cc9

File tree

10 files changed

+344
-0
lines changed

10 files changed

+344
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using Microsoft.OpenApi.Reader;
9+
using Xunit;
10+
11+
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
12+
{
13+
[Collection("DefaultSettings")]
14+
public class OpenApiHeaderTests
15+
{
16+
private const string SampleFolderPath = "V31Tests/Samples/OpenApiHeader/";
17+
18+
[Fact]
19+
public async Task ParseBasicHeaderShouldSucceed()
20+
{
21+
// Arrange
22+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicHeader.yaml"));
23+
24+
// Act
25+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings);
26+
27+
// Assert
28+
Assert.Equivalent(
29+
new OpenApiHeader
30+
{
31+
Description = "The number of allowed requests in the current period",
32+
Schema = new OpenApiSchema()
33+
{
34+
Type = JsonSchemaType.Integer
35+
}
36+
}, header);
37+
}
38+
39+
[Fact]
40+
public async Task ParseHeaderWithContentShouldSucceed()
41+
{
42+
// Arrange
43+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithContent.yaml"));
44+
45+
// Act
46+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings);
47+
48+
// Assert
49+
Assert.Equivalent(
50+
new OpenApiHeader
51+
{
52+
Description = "A complex header with content",
53+
Content = new Dictionary<string, OpenApiMediaType>()
54+
{
55+
["application/json"] = new()
56+
{
57+
Schema = new OpenApiSchema()
58+
{
59+
Type = JsonSchemaType.Object,
60+
Properties = new Dictionary<string, IOpenApiSchema>()
61+
{
62+
["timestamp"] = new OpenApiSchema()
63+
{
64+
Type = JsonSchemaType.String,
65+
Format = "date-time"
66+
},
67+
["value"] = new OpenApiSchema()
68+
{
69+
Type = JsonSchemaType.Integer
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}, header);
76+
}
77+
78+
[Fact]
79+
public async Task ParseHeaderWithMultipleContentTypesShouldSucceed()
80+
{
81+
// Arrange
82+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithMultipleContentTypes.yaml"));
83+
84+
// Act
85+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings);
86+
87+
// Assert
88+
Assert.Equivalent(
89+
new OpenApiHeader
90+
{
91+
Description = "A header that accepts multiple content types",
92+
Content = new Dictionary<string, OpenApiMediaType>()
93+
{
94+
["application/json"] = new()
95+
{
96+
Schema = new OpenApiSchema()
97+
{
98+
Type = JsonSchemaType.Object,
99+
Properties = new Dictionary<string, IOpenApiSchema>()
100+
{
101+
["data"] = new OpenApiSchema()
102+
{
103+
Type = JsonSchemaType.String
104+
}
105+
}
106+
}
107+
},
108+
["text/plain"] = new()
109+
{
110+
Schema = new OpenApiSchema()
111+
{
112+
Type = JsonSchemaType.String
113+
}
114+
}
115+
}
116+
}, header);
117+
}
118+
119+
[Fact]
120+
public async Task ParseHeaderWithStyleAndContentShouldPreferContent()
121+
{
122+
// Arrange
123+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithStyleAndContent.yaml"));
124+
125+
// Act
126+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_1, new(), settings: SettingsFixture.ReaderSettings);
127+
128+
// Assert
129+
// Both content and style can be present, content takes precedence for serialization behavior
130+
Assert.NotNull(header.Content);
131+
Assert.Single(header.Content);
132+
Assert.True(header.Content.ContainsKey("application/json"));
133+
Assert.Equal(ParameterStyle.Simple, header.Style); // Style can still be present
134+
}
135+
}
136+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: "The number of allowed requests in the current period"
2+
schema:
3+
type: integer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description: "A complex header with content"
2+
content:
3+
application/json:
4+
schema:
5+
type: object
6+
properties:
7+
timestamp:
8+
type: string
9+
format: date-time
10+
value:
11+
type: integer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description: "A header that accepts multiple content types"
2+
content:
3+
application/json:
4+
schema:
5+
type: object
6+
properties:
7+
data:
8+
type: string
9+
text/plain:
10+
schema:
11+
type: string
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description: "A header with both style and content (content should take precedence)"
2+
style: simple
3+
schema:
4+
type: string
5+
content:
6+
application/json:
7+
schema:
8+
type: object
9+
properties:
10+
value:
11+
type: string
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using Microsoft.OpenApi.Reader;
9+
using Xunit;
10+
11+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
12+
{
13+
[Collection("DefaultSettings")]
14+
public class OpenApiHeaderTests
15+
{
16+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiHeader/";
17+
18+
[Fact]
19+
public async Task ParseBasicHeaderShouldSucceed()
20+
{
21+
// Arrange
22+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "basicHeader.yaml"));
23+
24+
// Act
25+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_0, new(), settings: SettingsFixture.ReaderSettings);
26+
27+
// Assert
28+
Assert.Equivalent(
29+
new OpenApiHeader
30+
{
31+
Description = "The number of allowed requests in the current period",
32+
Schema = new OpenApiSchema()
33+
{
34+
Type = JsonSchemaType.Integer
35+
}
36+
}, header);
37+
}
38+
39+
[Fact]
40+
public async Task ParseHeaderWithContentShouldSucceed()
41+
{
42+
// Arrange
43+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithContent.yaml"));
44+
45+
// Act
46+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_0, new(), settings: SettingsFixture.ReaderSettings);
47+
48+
// Assert
49+
Assert.Equivalent(
50+
new OpenApiHeader
51+
{
52+
Description = "A complex header with content",
53+
Content = new Dictionary<string, OpenApiMediaType>()
54+
{
55+
["application/json"] = new()
56+
{
57+
Schema = new OpenApiSchema()
58+
{
59+
Type = JsonSchemaType.Object,
60+
Properties = new Dictionary<string, IOpenApiSchema>()
61+
{
62+
["timestamp"] = new OpenApiSchema()
63+
{
64+
Type = JsonSchemaType.String,
65+
Format = "date-time"
66+
},
67+
["value"] = new OpenApiSchema()
68+
{
69+
Type = JsonSchemaType.Integer
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}, header);
76+
}
77+
78+
[Fact]
79+
public async Task ParseHeaderWithMultipleContentTypesShouldSucceed()
80+
{
81+
// Arrange
82+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithMultipleContentTypes.yaml"));
83+
84+
// Act
85+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_0, new(), settings: SettingsFixture.ReaderSettings);
86+
87+
// Assert
88+
Assert.Equivalent(
89+
new OpenApiHeader
90+
{
91+
Description = "A header that accepts multiple content types",
92+
Content = new Dictionary<string, OpenApiMediaType>()
93+
{
94+
["application/json"] = new()
95+
{
96+
Schema = new OpenApiSchema()
97+
{
98+
Type = JsonSchemaType.Object,
99+
Properties = new Dictionary<string, IOpenApiSchema>()
100+
{
101+
["data"] = new OpenApiSchema()
102+
{
103+
Type = JsonSchemaType.String
104+
}
105+
}
106+
}
107+
},
108+
["text/plain"] = new()
109+
{
110+
Schema = new OpenApiSchema()
111+
{
112+
Type = JsonSchemaType.String
113+
}
114+
}
115+
}
116+
}, header);
117+
}
118+
119+
[Fact]
120+
public async Task ParseHeaderWithStyleAndContentShouldPreferContent()
121+
{
122+
// Arrange
123+
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "headerWithStyleAndContent.yaml"));
124+
125+
// Act
126+
var header = await OpenApiModelFactory.LoadAsync<OpenApiHeader>(stream, OpenApiSpecVersion.OpenApi3_0, new(), settings: SettingsFixture.ReaderSettings);
127+
128+
// Assert
129+
// Both content and style can be present, content takes precedence for serialization behavior
130+
Assert.NotNull(header.Content);
131+
Assert.Single(header.Content);
132+
Assert.True(header.Content.ContainsKey("application/json"));
133+
Assert.Equal(ParameterStyle.Simple, header.Style); // Style can still be present
134+
}
135+
}
136+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: "The number of allowed requests in the current period"
2+
schema:
3+
type: integer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description: "A complex header with content"
2+
content:
3+
application/json:
4+
schema:
5+
type: object
6+
properties:
7+
timestamp:
8+
type: string
9+
format: date-time
10+
value:
11+
type: integer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description: "A header that accepts multiple content types"
2+
content:
3+
application/json:
4+
schema:
5+
type: object
6+
properties:
7+
data:
8+
type: string
9+
text/plain:
10+
schema:
11+
type: string
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
description: "A header with both style and content (content should take precedence)"
2+
style: simple
3+
schema:
4+
type: string
5+
content:
6+
application/json:
7+
schema:
8+
type: object
9+
properties:
10+
value:
11+
type: string

0 commit comments

Comments
 (0)