|
1 | | -using System.IO; |
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
2 | 3 | using System.Net.Http; |
3 | 4 | using System.Text.Json.Nodes; |
4 | 5 | using System.Threading.Tasks; |
5 | 6 | using Microsoft.OpenApi.Models; |
| 7 | +using Microsoft.OpenApi.Models.Interfaces; |
6 | 8 | using Microsoft.OpenApi.Reader; |
| 9 | +using Microsoft.OpenApi.Services; |
7 | 10 | using Microsoft.OpenApi.Writers; |
8 | 11 | using Xunit; |
9 | 12 |
|
@@ -202,5 +205,83 @@ public async Task ParseLocalReferenceToJsonSchemaResourceWorks() |
202 | 205 | // Assert |
203 | 206 | Assert.Equal(JsonSchemaType.Object | JsonSchemaType.Null, schema.Type); |
204 | 207 | } |
| 208 | + |
| 209 | + [Fact] |
| 210 | + public void ResolveSubSchema_ShouldTraverseKnownKeywords() |
| 211 | + { |
| 212 | + var schema = new OpenApiSchema |
| 213 | + { |
| 214 | + Type = JsonSchemaType.Object, |
| 215 | + Properties = new Dictionary<string, IOpenApiSchema> |
| 216 | + { |
| 217 | + ["a"] = new OpenApiSchema |
| 218 | + { |
| 219 | + Properties = new Dictionary<string, IOpenApiSchema> |
| 220 | + { |
| 221 | + ["b"] = new OpenApiSchema { Type = JsonSchemaType.String } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + }; |
| 226 | + |
| 227 | + var path = new[] { "properties", "a", "properties", "b" }; |
| 228 | + |
| 229 | + var result = OpenApiWorkspace.ResolveSubSchema(schema, path); |
| 230 | + |
| 231 | + Assert.NotNull(result); |
| 232 | + Assert.Equal(JsonSchemaType.String, result!.Type); |
| 233 | + } |
| 234 | + |
| 235 | + public static IEnumerable<object[]> SubSchemaKeywordPropertyPaths => |
| 236 | + [ |
| 237 | + [new[] { "properties", "properties" }], |
| 238 | + [new[] { "properties", "allOf" }] |
| 239 | + ]; |
| 240 | + |
| 241 | + |
| 242 | + [Theory] |
| 243 | + [MemberData(nameof(SubSchemaKeywordPropertyPaths))] |
| 244 | + public void ResolveSubSchema_ShouldHandleUserDefinedKeywordNamedProperty(string[] pathSegments) |
| 245 | + { |
| 246 | + var schema = new OpenApiSchema |
| 247 | + { |
| 248 | + Type = JsonSchemaType.Object, |
| 249 | + Properties = new Dictionary<string, IOpenApiSchema> |
| 250 | + { |
| 251 | + ["properties"] = new OpenApiSchema { Type = JsonSchemaType.String }, |
| 252 | + ["allOf"] = new OpenApiSchema { Type = JsonSchemaType.String } |
| 253 | + } |
| 254 | + }; |
| 255 | + |
| 256 | + var result = OpenApiWorkspace.ResolveSubSchema(schema, pathSegments); |
| 257 | + |
| 258 | + Assert.NotNull(result); |
| 259 | + Assert.Equal(JsonSchemaType.String, result!.Type); |
| 260 | + } |
| 261 | + |
| 262 | + [Fact] |
| 263 | + public void ResolveSubSchema_ShouldRecurseIntoAllOfComposition() |
| 264 | + { |
| 265 | + var schema = new OpenApiSchema |
| 266 | + { |
| 267 | + AllOf = |
| 268 | + [ |
| 269 | + new OpenApiSchema |
| 270 | + { |
| 271 | + Properties = new Dictionary<string, IOpenApiSchema> |
| 272 | + { |
| 273 | + ["x"] = new OpenApiSchema { Type = JsonSchemaType.Integer } |
| 274 | + } |
| 275 | + } |
| 276 | + ] |
| 277 | + }; |
| 278 | + |
| 279 | + var path = new[] { "allOf", "0", "properties", "x" }; |
| 280 | + |
| 281 | + var result = OpenApiWorkspace.ResolveSubSchema(schema, path); |
| 282 | + |
| 283 | + Assert.NotNull(result); |
| 284 | + Assert.Equal(JsonSchemaType.Integer, result!.Type); |
| 285 | + } |
205 | 286 | } |
206 | 287 | } |
0 commit comments