|
7 | 7 | using Microsoft.Extensions.DependencyInjection;
|
8 | 8 | using Microsoft.OpenApi.Any;
|
9 | 9 | using Microsoft.OpenApi.Models;
|
10 |
| -using Xunit.Sdk; |
11 | 10 |
|
12 | 11 | public class SchemaTransformerTests : OpenApiDocumentServiceTestBase
|
13 | 12 | {
|
@@ -425,4 +424,59 @@ await VerifyOpenApiDocument(builder, options, document =>
|
425 | 424 | Assert.Equal("modified-number-format", errorSchema.Properties["code"].Format);
|
426 | 425 | });
|
427 | 426 | }
|
| 427 | + |
| 428 | + [Fact] |
| 429 | + public async Task SchemaTransformers_CanImplementNotSchemaIndependently() |
| 430 | + { |
| 431 | + var builder = CreateBuilder(); |
| 432 | + |
| 433 | + builder.MapGet("/todo", () => new Todo(1, "Item1", false, DateTime.Now)); |
| 434 | + builder.MapPost("/shape", (Shape shape) => { }); |
| 435 | + |
| 436 | + var options = new OpenApiOptions(); |
| 437 | + options.UseSchemaTransformer((schema, context, cancellationToken) => |
| 438 | + { |
| 439 | + if (context.JsonTypeInfo.Type == typeof(Todo)) |
| 440 | + { |
| 441 | + schema.Not = new OpenApiSchema { Type = "string" }; |
| 442 | + } |
| 443 | + if (context.JsonTypeInfo.Type == typeof(Triangle)) |
| 444 | + { |
| 445 | + schema.Not = new OpenApiSchema { Type = "string" }; |
| 446 | + } |
| 447 | + return Task.CompletedTask; |
| 448 | + }); |
| 449 | + UseNotSchemaTransformer(options, (schema, context, cancellationToken) => |
| 450 | + { |
| 451 | + schema.Extensions["modified-by-not-schema-transformer"] = new OpenApiBoolean(true); |
| 452 | + return Task.CompletedTask; |
| 453 | + }); |
| 454 | + |
| 455 | + // Assert that not schemas have been modified for both `Todo` and `Triangle` types. |
| 456 | + await VerifyOpenApiDocument(builder, options, document => |
| 457 | + { |
| 458 | + var path = document.Paths["/todo"]; |
| 459 | + var getOperation = path.Operations[OperationType.Get]; |
| 460 | + var responseSchema = getOperation.Responses["200"].Content["application/json"].Schema.GetEffective(document); |
| 461 | + Assert.True(((OpenApiBoolean)responseSchema.Not.Extensions["modified-by-not-schema-transformer"]).Value); |
| 462 | + |
| 463 | + var shapePath = document.Paths["/shape"]; |
| 464 | + var shapeOperation = shapePath.Operations[OperationType.Post]; |
| 465 | + var shapeRequestSchema = shapeOperation.RequestBody.Content["application/json"].Schema.GetEffective(document); |
| 466 | + var triangleSchema = Assert.Single(shapeRequestSchema.AnyOf.Where(s => s.Reference.Id == "ShapeTriangle")).GetEffective(document); |
| 467 | + Assert.True(((OpenApiBoolean)triangleSchema.Not.Extensions["modified-by-not-schema-transformer"]).Value); |
| 468 | + }); |
| 469 | + |
| 470 | + static void UseNotSchemaTransformer(OpenApiOptions options, Func<OpenApiSchema, OpenApiSchemaTransformerContext, CancellationToken, Task> func) |
| 471 | + { |
| 472 | + options.UseSchemaTransformer(async (schema, context, cancellationToken) => |
| 473 | + { |
| 474 | + if (schema.Not != null) |
| 475 | + { |
| 476 | + await func(schema.Not, context, cancellationToken); |
| 477 | + } |
| 478 | + return; |
| 479 | + }); |
| 480 | + } |
| 481 | + } |
428 | 482 | }
|
0 commit comments