Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Vogen/GenerateCodeForOpenApiSchemaCustomization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ private static void WriteSchemaFilter(SourceProductionContext context, VogenKnow
return;
}

var openApiVersion = OpenApiSchemaUtils.DetermineOpenApiVersionBeingUsed(knownSymbols);
var openApiSchemaReference = openApiVersion switch
{
OpenApiVersionBeingUsed.One => "global::Microsoft.OpenApi.Models.OpenApiSchema",
OpenApiVersionBeingUsed.TwoPlus => "global::Microsoft.OpenApi.IOpenApiSchema",
_ => throw new ArgumentOutOfRangeException(nameof(openApiVersion), "Unknown or unimplemented schema version")
};


string source =
$$"""

Expand All @@ -64,7 +73,7 @@ public class VogenSchemaFilter{{inAppendage}} : global::Swashbuckle.AspNetCore.S
{
private const BindingFlags _flags = BindingFlags.Public | BindingFlags.Instance;

public void Apply(global::Microsoft.OpenApi.Models.OpenApiSchema schema, global::Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
public void Apply({{openApiSchemaReference}} schema, global::Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
{
if (context.Type.GetCustomAttribute<Vogen.ValueObjectAttribute>() is not { } attribute)
return;
Expand Down
25 changes: 25 additions & 0 deletions tests/SnapshotTests/OpenApi/SwashbuckleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ public partial class MyVo { }
.CustomizeSettings(s => s.UseHashedParameters(@namespace))
.RunOn(TargetFramework.AspNetCore8_0);
}

[Theory]
[InlineData("")]
[InlineData("namespace MyNamespace;")]
[InlineData("namespace @double;")]
public async Task Generates_filter_code_net10(string @namespace)
{
var source =
$$"""
using System;
using Vogen;

[assembly: VogenDefaults(openApiSchemaCustomizations: OpenApiSchemaCustomizations.GenerateSwashbuckleSchemaFilter)]

{{@namespace}}

[ValueObject<int>]
public partial class MyVo { }
""";

await new SnapshotRunner<ValueObjectGenerator>()
.WithSource(source)
.CustomizeSettings(s => s.UseHashedParameters(@namespace))
.RunOn(TargetFramework.AspNetCore10_0);
}

[Theory]
[InlineData("")]
Expand Down
Loading