|
4 | 4 | using System.ComponentModel;
|
5 | 5 | using System.ComponentModel.DataAnnotations;
|
6 | 6 | using System.IO.Pipelines;
|
| 7 | +using System.Text.Json.Serialization.Metadata; |
7 | 8 | using Microsoft.AspNetCore.Builder;
|
8 | 9 | using Microsoft.AspNetCore.Mvc;
|
| 10 | +using Microsoft.Extensions.DependencyInjection; |
9 | 11 | using Microsoft.OpenApi.Any;
|
10 | 12 | using Microsoft.OpenApi.Models;
|
11 | 13 |
|
@@ -468,6 +470,51 @@ await VerifyOpenApiDocument(builder, document =>
|
468 | 470 | });
|
469 | 471 | }
|
470 | 472 |
|
| 473 | + [Fact] |
| 474 | + public async Task SupportsNestedTypes_WithNoAttributeProvider() |
| 475 | + { |
| 476 | + // Arrange: this test ensures that we can correctly handle the scenario |
| 477 | + // where the attribute provider is null and we need to patch the property mappings |
| 478 | + // that are created by the underlying JsonSchemaExporter. |
| 479 | + var serviceCollection = new ServiceCollection(); |
| 480 | + serviceCollection.ConfigureHttpJsonOptions(options => |
| 481 | + { |
| 482 | + options.SerializerOptions.TypeInfoResolver = options.SerializerOptions.TypeInfoResolver?.WithAddedModifier(jsonTypeInfo => |
| 483 | + { |
| 484 | + foreach (var propertyInfo in jsonTypeInfo.Properties) |
| 485 | + { |
| 486 | + propertyInfo.AttributeProvider = null; |
| 487 | + } |
| 488 | + |
| 489 | + }); |
| 490 | + }); |
| 491 | + var builder = CreateBuilder(serviceCollection); |
| 492 | + |
| 493 | + // Act |
| 494 | + builder.MapPost("/api", (NestedType type) => { }); |
| 495 | + |
| 496 | + // Assert |
| 497 | + await VerifyOpenApiDocument(builder, document => |
| 498 | + { |
| 499 | + var operation = document.Paths["/api"].Operations[OperationType.Post]; |
| 500 | + var requestBody = operation.RequestBody; |
| 501 | + var content = Assert.Single(requestBody.Content); |
| 502 | + Assert.Equal("NestedType", content.Value.Schema.Reference.Id); |
| 503 | + var schema = content.Value.Schema.GetEffective(document); |
| 504 | + Assert.Collection(schema.Properties, |
| 505 | + property => |
| 506 | + { |
| 507 | + Assert.Equal("name", property.Key); |
| 508 | + Assert.Equal("string", property.Value.Type); |
| 509 | + }, |
| 510 | + property => |
| 511 | + { |
| 512 | + Assert.Equal("nested", property.Key); |
| 513 | + Assert.Equal("NestedType", property.Value.Reference.Id); |
| 514 | + }); |
| 515 | + }); |
| 516 | + } |
| 517 | + |
471 | 518 | private class DescriptionTodo
|
472 | 519 | {
|
473 | 520 | [Description("The unique identifier for a todo item.")]
|
|
0 commit comments