Skip to content

Commit c44268b

Browse files
committed
Add test for tranformers on unsupported subschemas
1 parent 48b59c3 commit c44268b

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

src/OpenApi/test/Transformers/SchemaTransformerTests.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.OpenApi.Any;
99
using Microsoft.OpenApi.Models;
10-
using Xunit.Sdk;
1110

1211
public class SchemaTransformerTests : OpenApiDocumentServiceTestBase
1312
{
@@ -425,4 +424,59 @@ await VerifyOpenApiDocument(builder, options, document =>
425424
Assert.Equal("modified-number-format", errorSchema.Properties["code"].Format);
426425
});
427426
}
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+
}
428482
}

0 commit comments

Comments
 (0)