Skip to content

Commit 0520443

Browse files
committed
chore: refactoring
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent 4801b06 commit 0520443

File tree

2 files changed

+224
-241
lines changed

2 files changed

+224
-241
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Net.Http;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
9+
namespace Microsoft.OpenApi.Readers.Tests.V32Tests;
10+
11+
public class OpenApiPathItemDeserializerTests
12+
{
13+
private const string SampleFolderPath = "V32Tests/Samples/OpenApiPathItem/";
14+
15+
[Fact]
16+
public async Task ParsePathItemWithQueryAndAdditionalOperationsV32Works()
17+
{
18+
// Arrange & Act
19+
var result = await OpenApiDocument.LoadAsync($"{SampleFolderPath}pathItemWithQueryAndAdditionalOperations.yaml", SettingsFixture.ReaderSettings);
20+
var pathItem = result.Document.Paths["/pets"];
21+
22+
// Assert
23+
Assert.Equal("Pet operations", pathItem.Summary);
24+
Assert.Equal("Operations available for pets", pathItem.Description);
25+
26+
// Regular operations
27+
var getOp = Assert.Contains(HttpMethod.Get, pathItem.Operations);
28+
Assert.Equal("getPets", getOp.OperationId);
29+
30+
var postOp = Assert.Contains(HttpMethod.Post, pathItem.Operations);
31+
Assert.Equal("createPet", postOp.OperationId);
32+
33+
// Query operation should now be on one of the operations
34+
// Since the YAML structure changed, we need to check which operation has the query
35+
var queryOp = Assert.Contains(new HttpMethod("Query"), pathItem.Operations);
36+
Assert.Equal("Query pets with complex filters", queryOp.Summary);
37+
Assert.Equal("queryPets", queryOp.OperationId);
38+
Assert.Single(queryOp.Parameters);
39+
Assert.Equal("filter", queryOp.Parameters[0].Name);
40+
41+
var notifyOp = Assert.Contains(new HttpMethod("notify"), pathItem.Operations);
42+
Assert.Equal("Notify about pet updates", notifyOp.Summary);
43+
Assert.Equal("notifyPetUpdates", notifyOp.OperationId);
44+
Assert.NotNull(notifyOp.RequestBody);
45+
46+
var subscribeOp = Assert.Contains(new HttpMethod("subscribe"), pathItem.Operations);
47+
Assert.Equal("Subscribe to pet events", subscribeOp.Summary);
48+
Assert.Equal("subscribePetEvents", subscribeOp.OperationId);
49+
Assert.Single(subscribeOp.Parameters);
50+
Assert.Equal("events", subscribeOp.Parameters[0].Name);
51+
}
52+
53+
[Fact]
54+
public async Task ParsePathItemWithV32ExtensionsWorks()
55+
{
56+
// Arrange & Act
57+
var result = await OpenApiDocument.LoadAsync($"{SampleFolderPath}pathItemWithV32Extensions.yaml", SettingsFixture.ReaderSettings);
58+
var pathItem = result.Document.Paths["/pets"];
59+
60+
// Assert
61+
Assert.Equal("Pet operations", pathItem.Summary);
62+
Assert.Equal("Operations available for pets", pathItem.Description);
63+
64+
// Regular operations
65+
Assert.True(pathItem.Operations?.ContainsKey(HttpMethod.Get));
66+
var getOp = Assert.Contains(HttpMethod.Get, pathItem.Operations);
67+
Assert.Equal("getPets", getOp.OperationId);
68+
69+
// Query operation from extension should now be on the operation
70+
var queryOp = Assert.Contains(new HttpMethod("Query"), pathItem.Operations);
71+
Assert.Equal("Query pets with complex filters", queryOp.Summary);
72+
Assert.Equal("queryPets", queryOp.OperationId);
73+
74+
// Additional operations from extension should now be on the operation
75+
var notifyOp = Assert.Contains(new HttpMethod("notify"), pathItem.Operations);
76+
Assert.Equal("Notify about pet updates", notifyOp.Summary);
77+
Assert.Equal("notifyPetUpdates", notifyOp.OperationId);
78+
}
79+
80+
[Fact]
81+
public async Task SerializeV32PathItemToV31ProducesExtensions()
82+
{
83+
// Arrange
84+
var pathItem = new OpenApiPathItem
85+
{
86+
Summary = "Test path",
87+
Operations = new Dictionary<HttpMethod, OpenApiOperation>
88+
{
89+
[HttpMethod.Get] = new OpenApiOperation
90+
{
91+
Summary = "Get operation",
92+
OperationId = "getOp",
93+
Responses = new OpenApiResponses
94+
{
95+
["200"] = new OpenApiResponse { Description = "Success" }
96+
}
97+
},
98+
[new HttpMethod("Query")] = new OpenApiOperation
99+
{
100+
Summary = "Query operation",
101+
OperationId = "queryOp",
102+
Responses = new OpenApiResponses
103+
{
104+
["200"] = new OpenApiResponse { Description = "Success" }
105+
}
106+
},
107+
[new HttpMethod("notify")] = new OpenApiOperation
108+
{
109+
Summary = "Notify operation",
110+
OperationId = "notifyOp",
111+
Responses = new OpenApiResponses
112+
{
113+
["200"] = new OpenApiResponse { Description = "Success" }
114+
}
115+
}
116+
}
117+
};
118+
119+
// Act
120+
var yaml = await pathItem.SerializeAsYamlAsync(OpenApiSpecVersion.OpenApi3_1);
121+
122+
// Assert
123+
Assert.Contains("x-oas-query:", yaml);
124+
Assert.Contains("x-oas-additionalOperations:", yaml);
125+
Assert.Contains("queryOp", yaml);
126+
Assert.Contains("notifyOp", yaml);
127+
}
128+
129+
[Fact]
130+
public async Task SerializeV32PathItemToV3ProducesExtensions()
131+
{
132+
// Arrange
133+
var pathItem = new OpenApiPathItem
134+
{
135+
Summary = "Test path",
136+
Operations = new Dictionary<HttpMethod, OpenApiOperation>
137+
{
138+
[HttpMethod.Get] = new OpenApiOperation
139+
{
140+
Summary = "Get operation",
141+
OperationId = "getOp",
142+
Responses = new OpenApiResponses
143+
{
144+
["200"] = new OpenApiResponse { Description = "Success" }
145+
}
146+
},
147+
[new HttpMethod("Query")] = new OpenApiOperation
148+
{
149+
Summary = "Query operation",
150+
OperationId = "queryOp",
151+
Responses = new OpenApiResponses
152+
{
153+
["200"] = new OpenApiResponse { Description = "Success" }
154+
}
155+
},
156+
[new HttpMethod("notify")] = new OpenApiOperation
157+
{
158+
Summary = "Notify operation",
159+
OperationId = "notifyOp",
160+
Responses = new OpenApiResponses
161+
{
162+
["200"] = new OpenApiResponse { Description = "Success" }
163+
}
164+
}
165+
}
166+
};
167+
168+
// Act
169+
var yaml = await pathItem.SerializeAsYamlAsync(OpenApiSpecVersion.OpenApi3_0);
170+
171+
// Assert
172+
Assert.Contains("x-oas-query:", yaml);
173+
Assert.Contains("x-oas-additionalOperations:", yaml);
174+
Assert.Contains("queryOp", yaml);
175+
Assert.Contains("notifyOp", yaml);
176+
}
177+
178+
[Fact]
179+
public void PathItemShallowCopyIncludesV32Fields()
180+
{
181+
// Arrange
182+
var original = new OpenApiPathItem
183+
{
184+
Summary = "Original",
185+
Operations = new Dictionary<HttpMethod, OpenApiOperation>
186+
{
187+
[HttpMethod.Get] = new OpenApiOperation
188+
{
189+
Summary = "Get operation",
190+
OperationId = "getOp",
191+
Responses = new OpenApiResponses()
192+
},
193+
[new HttpMethod("Query")] = new OpenApiOperation
194+
{
195+
Summary = "Query operation",
196+
OperationId = "queryOp"
197+
},
198+
[new HttpMethod("notify")] = new OpenApiOperation
199+
{
200+
Summary = "Notify operation",
201+
OperationId = "notifyOp"
202+
}
203+
}
204+
};
205+
206+
// Act
207+
var copy = original.CreateShallowCopy();
208+
209+
// Assert
210+
Assert.NotNull(original.Operations);
211+
Assert.NotNull(copy.Operations);
212+
Assert.Contains(HttpMethod.Get, original.Operations);
213+
Assert.Contains(HttpMethod.Get, copy.Operations);
214+
215+
var copyQueryOp = Assert.Contains(new HttpMethod("Query"), copy.Operations);
216+
Assert.Contains(new HttpMethod("Query"), original.Operations);
217+
Assert.Equal("Query operation", copyQueryOp.Summary);
218+
Assert.Equal("queryOp", copyQueryOp.OperationId);
219+
Assert.Contains(new HttpMethod("notify"), original.Operations);
220+
var copyNotifyOp = Assert.Contains(new HttpMethod("notify"), copy.Operations);
221+
Assert.Equal("Notify operation", copyNotifyOp.Summary);
222+
Assert.Equal("notifyOp", copyNotifyOp.OperationId);
223+
}
224+
}

0 commit comments

Comments
 (0)