Skip to content

Commit 41a231e

Browse files
Copilotbaywet
andcommitted
Add comprehensive tests for media types components support
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
1 parent 31b3821 commit 41a231e

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Microsoft.OpenApi.Tests.Models
9+
{
10+
[Collection("DefaultSettings")]
11+
public class OpenApiComponentsMediaTypesTests
12+
{
13+
public static OpenApiComponents ComponentsWithMediaTypes = new()
14+
{
15+
MediaTypes = new Dictionary<string, IOpenApiMediaType>()
16+
{
17+
["application/json"] = new OpenApiMediaType()
18+
{
19+
Schema = new OpenApiSchema()
20+
{
21+
Type = JsonSchemaType.Object,
22+
Properties = new Dictionary<string, IOpenApiSchema>()
23+
{
24+
["name"] = new OpenApiSchema()
25+
{
26+
Type = JsonSchemaType.String
27+
},
28+
["age"] = new OpenApiSchema()
29+
{
30+
Type = JsonSchemaType.Integer
31+
}
32+
}
33+
}
34+
},
35+
["text/plain"] = new OpenApiMediaType()
36+
{
37+
Schema = new OpenApiSchema()
38+
{
39+
Type = JsonSchemaType.String
40+
}
41+
}
42+
}
43+
};
44+
45+
[Fact]
46+
public async Task SerializeMediaTypesAsV32JsonWorks()
47+
{
48+
// Arrange
49+
var expected =
50+
"""
51+
{
52+
"mediaTypes": {
53+
"application/json": {
54+
"schema": {
55+
"type": "object",
56+
"properties": {
57+
"name": {
58+
"type": "string"
59+
},
60+
"age": {
61+
"type": "integer"
62+
}
63+
}
64+
}
65+
},
66+
"text/plain": {
67+
"schema": {
68+
"type": "string"
69+
}
70+
}
71+
}
72+
}
73+
""";
74+
75+
// Act
76+
var actual = await ComponentsWithMediaTypes.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2);
77+
78+
// Assert
79+
actual = actual.MakeLineBreaksEnvironmentNeutral();
80+
expected = expected.MakeLineBreaksEnvironmentNeutral();
81+
Assert.Equal(expected, actual);
82+
}
83+
84+
[Fact]
85+
public async Task SerializeMediaTypesAsV31JsonWorks()
86+
{
87+
// Arrange - When serializing to v3.1, mediaTypes should be prefixed with x-oai-
88+
var expected =
89+
"""
90+
{
91+
"x-oai-mediaTypes": {
92+
"application/json": {
93+
"schema": {
94+
"type": "object",
95+
"properties": {
96+
"name": {
97+
"type": "string"
98+
},
99+
"age": {
100+
"type": "integer"
101+
}
102+
}
103+
}
104+
},
105+
"text/plain": {
106+
"schema": {
107+
"type": "string"
108+
}
109+
}
110+
}
111+
}
112+
""";
113+
114+
// Act
115+
var actual = await ComponentsWithMediaTypes.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1);
116+
117+
// Assert
118+
actual = actual.MakeLineBreaksEnvironmentNeutral();
119+
expected = expected.MakeLineBreaksEnvironmentNeutral();
120+
Assert.Equal(expected, actual);
121+
}
122+
123+
[Fact]
124+
public async Task SerializeMediaTypesAsV30JsonWorks()
125+
{
126+
// Arrange - When serializing to v3.0, mediaTypes should be prefixed with x-oai-
127+
var expected =
128+
"""
129+
{
130+
"x-oai-mediaTypes": {
131+
"application/json": {
132+
"schema": {
133+
"type": "object",
134+
"properties": {
135+
"name": {
136+
"type": "string"
137+
},
138+
"age": {
139+
"type": "integer"
140+
}
141+
}
142+
}
143+
},
144+
"text/plain": {
145+
"schema": {
146+
"type": "string"
147+
}
148+
}
149+
}
150+
}
151+
""";
152+
153+
// Act
154+
var actual = await ComponentsWithMediaTypes.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);
155+
156+
// Assert
157+
actual = actual.MakeLineBreaksEnvironmentNeutral();
158+
expected = expected.MakeLineBreaksEnvironmentNeutral();
159+
Assert.Equal(expected, actual);
160+
}
161+
162+
[Fact]
163+
public void CopyConstructorCopiesMediaTypes()
164+
{
165+
// Arrange
166+
var original = ComponentsWithMediaTypes;
167+
168+
// Act
169+
var copy = new OpenApiComponents(original);
170+
171+
// Assert
172+
Assert.NotNull(copy.MediaTypes);
173+
Assert.Equal(2, copy.MediaTypes.Count);
174+
Assert.True(copy.MediaTypes.ContainsKey("application/json"));
175+
Assert.True(copy.MediaTypes.ContainsKey("text/plain"));
176+
}
177+
178+
[Fact]
179+
public void WorkspaceCanRegisterMediaTypeComponents()
180+
{
181+
// Arrange
182+
var mediaType = new OpenApiMediaType()
183+
{
184+
Schema = new OpenApiSchema()
185+
{
186+
Type = JsonSchemaType.Object
187+
}
188+
};
189+
190+
var doc = new OpenApiDocument()
191+
{
192+
Components = new OpenApiComponents()
193+
{
194+
MediaTypes = new Dictionary<string, IOpenApiMediaType>()
195+
{
196+
["application/json"] = mediaType
197+
}
198+
}
199+
};
200+
201+
// Act
202+
doc.RegisterComponents();
203+
204+
// Assert
205+
Assert.Equal(1, doc.Workspace.ComponentsCount());
206+
}
207+
208+
[Fact]
209+
public void WorkspaceCanRegisterMediaTypeForDocument()
210+
{
211+
// Arrange
212+
var mediaType = new OpenApiMediaType()
213+
{
214+
Schema = new OpenApiSchema()
215+
{
216+
Type = JsonSchemaType.String
217+
}
218+
};
219+
220+
var doc = new OpenApiDocument();
221+
222+
// Act
223+
var result = doc.Workspace.RegisterComponentForDocument(doc, mediaType, "text/plain");
224+
225+
// Assert
226+
Assert.True(result);
227+
Assert.Equal(1, doc.Workspace.ComponentsCount());
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)