|
4 | 4 | using System.ComponentModel;
|
5 | 5 | using System.ComponentModel.DataAnnotations;
|
6 | 6 | using System.IO.Pipelines;
|
| 7 | +using System.Text.Json.Serialization; |
7 | 8 | using System.Text.Json.Serialization.Metadata;
|
8 | 9 | using Microsoft.AspNetCore.Builder;
|
9 | 10 | using Microsoft.AspNetCore.Mvc;
|
@@ -594,4 +595,68 @@ await VerifyOpenApiDocument(builder, document =>
|
594 | 595 | });
|
595 | 596 | });
|
596 | 597 | }
|
| 598 | + |
| 599 | + [Fact] |
| 600 | + public async Task SupportsClassWithJsonUnmappedMemberHandlingDisallowed() |
| 601 | + { |
| 602 | + // Arrange |
| 603 | + var builder = CreateBuilder(); |
| 604 | + |
| 605 | + // Act |
| 606 | + builder.MapPost("/api", (ExampleWithDisallowedUnmappedMembers type) => { }); |
| 607 | + |
| 608 | + // Assert |
| 609 | + await VerifyOpenApiDocument(builder, document => |
| 610 | + { |
| 611 | + var operation = document.Paths["/api"].Operations[OperationType.Post]; |
| 612 | + var requestBody = operation.RequestBody; |
| 613 | + var content = Assert.Single(requestBody.Content); |
| 614 | + var schema = content.Value.Schema.GetEffective(document); |
| 615 | + Assert.Collection(schema.Properties, |
| 616 | + property => |
| 617 | + { |
| 618 | + Assert.Equal("number", property.Key); |
| 619 | + Assert.Equal("integer", property.Value.Type); |
| 620 | + }); |
| 621 | + Assert.False(schema.AdditionalPropertiesAllowed); |
| 622 | + }); |
| 623 | + } |
| 624 | + |
| 625 | + [Fact] |
| 626 | + public async Task SupportsClassWithJsonUnmappedMemberHandlingSkipped() |
| 627 | + { |
| 628 | + // Arrange |
| 629 | + var builder = CreateBuilder(); |
| 630 | + |
| 631 | + // Act |
| 632 | + builder.MapPost("/api", (ExampleWithSkippedUnmappedMembers type) => { }); |
| 633 | + |
| 634 | + // Assert |
| 635 | + await VerifyOpenApiDocument(builder, document => |
| 636 | + { |
| 637 | + var operation = document.Paths["/api"].Operations[OperationType.Post]; |
| 638 | + var requestBody = operation.RequestBody; |
| 639 | + var content = Assert.Single(requestBody.Content); |
| 640 | + var schema = content.Value.Schema.GetEffective(document); |
| 641 | + Assert.Collection(schema.Properties, |
| 642 | + property => |
| 643 | + { |
| 644 | + Assert.Equal("number", property.Key); |
| 645 | + Assert.Equal("integer", property.Value.Type); |
| 646 | + }); |
| 647 | + Assert.True(schema.AdditionalPropertiesAllowed); |
| 648 | + }); |
| 649 | + } |
| 650 | + |
| 651 | + [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)] |
| 652 | + private class ExampleWithDisallowedUnmappedMembers |
| 653 | + { |
| 654 | + public int Number { get; init; } |
| 655 | + } |
| 656 | + |
| 657 | + [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)] |
| 658 | + private class ExampleWithSkippedUnmappedMembers |
| 659 | + { |
| 660 | + public int Number { get; init; } |
| 661 | + } |
597 | 662 | }
|
0 commit comments