|
3 | 3 |
|
4 | 4 | using System.ComponentModel;
|
5 | 5 | using System.ComponentModel.DataAnnotations;
|
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
6 | 8 | using Microsoft.AspNetCore.Builder;
|
7 | 9 | using Microsoft.AspNetCore.Http;
|
8 | 10 | using Microsoft.AspNetCore.Mvc;
|
| 11 | +using Microsoft.AspNetCore.Routing; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
9 | 13 | using Microsoft.OpenApi.Any;
|
10 | 14 | using Microsoft.OpenApi.Models;
|
11 | 15 |
|
@@ -462,4 +466,70 @@ private class RouteParamsWithValidationsContainer
|
462 | 466 | [MaxLength(5)]
|
463 | 467 | public string Name { get; set; }
|
464 | 468 | }
|
| 469 | + |
| 470 | + [Fact] |
| 471 | + public async Task SupportsParametersWithTypeConverter() |
| 472 | + { |
| 473 | + // Arrange |
| 474 | + var serviceCollection = new ServiceCollection(); |
| 475 | + serviceCollection.ConfigureHttpJsonOptions(options => |
| 476 | + { |
| 477 | + options.SerializerOptions.Converters.Add(new CustomTypeConverter()); |
| 478 | + }); |
| 479 | + var builder = CreateBuilder(serviceCollection); |
| 480 | + |
| 481 | + // Act |
| 482 | + builder.MapPost("/api", (CustomType id) => { }); |
| 483 | + |
| 484 | + // Assert |
| 485 | + await VerifyOpenApiDocument(builder, document => |
| 486 | + { |
| 487 | + var operation = document.Paths["/api"].Operations[OperationType.Post]; |
| 488 | + Assert.NotNull(operation.RequestBody); |
| 489 | + Assert.NotNull(operation.RequestBody.Content); |
| 490 | + Assert.NotNull(operation.RequestBody.Content["application/json"]); |
| 491 | + Assert.NotNull(operation.RequestBody.Content["application/json"].Schema); |
| 492 | + // Type is null, it's up to the user to configure this via a custom schema |
| 493 | + // transformer for types with a converter. |
| 494 | + Assert.Null(operation.RequestBody.Content["application/json"].Schema.Type); |
| 495 | + }); |
| 496 | + } |
| 497 | + |
| 498 | + public struct CustomType { } |
| 499 | + |
| 500 | + public class CustomTypeConverter : JsonConverter<CustomType> |
| 501 | + { |
| 502 | + public override CustomType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 503 | + { |
| 504 | + return new CustomType(); |
| 505 | + } |
| 506 | + |
| 507 | + public override void Write(Utf8JsonWriter writer, CustomType value, JsonSerializerOptions options) |
| 508 | + { |
| 509 | + throw new NotImplementedException(); |
| 510 | + } |
| 511 | + } |
| 512 | + |
| 513 | + [Fact] |
| 514 | + public async Task SupportsParameterWithDynamicType() |
| 515 | + { |
| 516 | + // Arrange |
| 517 | + var builder = CreateBuilder(); |
| 518 | + |
| 519 | + // Act |
| 520 | + builder.MapPost("/api", (dynamic id) => { }); |
| 521 | + |
| 522 | + // Assert |
| 523 | + await VerifyOpenApiDocument(builder, document => |
| 524 | + { |
| 525 | + var operation = document.Paths["/api"].Operations[OperationType.Post]; |
| 526 | + Assert.NotNull(operation.RequestBody); |
| 527 | + Assert.NotNull(operation.RequestBody.Content); |
| 528 | + Assert.NotNull(operation.RequestBody.Content["application/json"]); |
| 529 | + Assert.NotNull(operation.RequestBody.Content["application/json"].Schema); |
| 530 | + // Type is null, it's up to the user to configure this via a custom schema |
| 531 | + // transformer for types with a converter. |
| 532 | + Assert.Null(operation.RequestBody.Content["application/json"].Schema.Type); |
| 533 | + }); |
| 534 | + } |
465 | 535 | }
|
0 commit comments