|
3 | 3 | using System.IO;
|
4 | 4 | using FluentAssertions;
|
5 | 5 | using Json.Schema;
|
| 6 | +using Microsoft.OpenApi.Extensions; |
6 | 7 | using Microsoft.OpenApi.Interfaces;
|
7 | 8 | using Microsoft.OpenApi.Models;
|
| 9 | +using Microsoft.OpenApi.Tests; |
8 | 10 | using Microsoft.OpenApi.Writers;
|
9 | 11 | using Xunit;
|
10 | 12 |
|
@@ -352,5 +354,49 @@ public void ParseDocumentWithExampleInSchemaShouldSucceed()
|
352 | 354 | // Assert
|
353 | 355 | Assert.NotNull(actual);
|
354 | 356 | }
|
| 357 | + |
| 358 | + [Fact] |
| 359 | + public void ParseDocumentWithPatternPropertiesInSchemaWorks() |
| 360 | + { |
| 361 | + // Arrange |
| 362 | + using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "docWithPatternPropertiesInSchema.yaml")); |
| 363 | + |
| 364 | + // Act |
| 365 | + var doc = new OpenApiStreamReader().Read(stream, out var diagnostic); |
| 366 | + |
| 367 | + var actualSchema = doc.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; |
| 368 | + |
| 369 | + var expectedSchema = new JsonSchemaBuilder() |
| 370 | + .Type(SchemaValueType.Object) |
| 371 | + .Properties( |
| 372 | + ("prop1", new JsonSchemaBuilder().Type(SchemaValueType.String)), |
| 373 | + ("prop2", new JsonSchemaBuilder().Type(SchemaValueType.String)), |
| 374 | + ("prop3", new JsonSchemaBuilder().Type(SchemaValueType.String))) |
| 375 | + .PatternProperties( |
| 376 | + ("^x-.*$", new JsonSchemaBuilder().Type(SchemaValueType.String))) |
| 377 | + .Build(); |
| 378 | + |
| 379 | + // Serialization |
| 380 | + var mediaType = doc.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"]; |
| 381 | + |
| 382 | + var expectedMediaType = @"schema: |
| 383 | + type: object |
| 384 | + properties: |
| 385 | + prop1: |
| 386 | + type: string |
| 387 | + prop2: |
| 388 | + type: string |
| 389 | + prop3: |
| 390 | + type: string |
| 391 | + patternProperties: |
| 392 | + ^x-.*$: |
| 393 | + type: string"; |
| 394 | + |
| 395 | + var actualMediaType = mediaType.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1); |
| 396 | + |
| 397 | + // Assert |
| 398 | + actualSchema.Should().BeEquivalentTo(expectedSchema); |
| 399 | + actualMediaType.MakeLineBreaksEnvironmentNeutral().Should().BeEquivalentTo(expectedMediaType.MakeLineBreaksEnvironmentNeutral()); |
| 400 | + } |
355 | 401 | }
|
356 | 402 | }
|
0 commit comments