Skip to content

Commit

Permalink
EES-5663 Remove version path parameters from OpenAPI documents
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsim committed Nov 18, 2024
1 parent a9b0df1 commit 5c64ab6
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System.Text.Json;
using GovUk.Education.ExploreEducationStatistics.Public.Data.Api.Swagger;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace GovUk.Education.ExploreEducationStatistics.Public.Data.Api.Tests.Swagger;

public class VersionedPathsDocumentFilterTests
{
[Fact]
public void VersionsInlinedIntoPaths()
{
var document = new OpenApiDocument
{
Info = new OpenApiInfo { Version = "1" },
Paths = new OpenApiPaths
{
{"/v{version}/endpoint-1", new OpenApiPathItem()},
{"/v{version}/endpoint-2/{id}", new OpenApiPathItem()}
}
};

var filter = new VersionedPathsDocumentFilter();
var context = CreateDocumentFilterContext();

filter.Apply(document, context);

Assert.Equal(2, document.Paths.Count);
Assert.Contains("/v1/endpoint-1", document.Paths.Keys);
Assert.Contains("/v1/endpoint-2/{id}", document.Paths.Keys);
}

[Fact]
public void VersionParametersRemoved()
{
var document = new OpenApiDocument
{
Info = new OpenApiInfo { Version = "1" },
Paths = new OpenApiPaths
{
{
"/v{version}/endpoint-1",
new OpenApiPathItem
{
Parameters =
[
new OpenApiParameter { Name = "version" }
],
Operations =
{
{
OperationType.Get,
new OpenApiOperation
{
Parameters =
[
new OpenApiParameter { Name = "version" }
]
}
}

}
}
},
{
"/v{version}/endpoint-2/{id}",
new OpenApiPathItem
{
Parameters =
[
new OpenApiParameter { Name = "version" },
new OpenApiParameter { Name = "id" }
],
Operations =
{
{
OperationType.Get,
new OpenApiOperation
{
Parameters =
[
new OpenApiParameter { Name = "version" },
new OpenApiParameter { Name = "id" }
]
}
}

}
}
}
}
};

var filter = new VersionedPathsDocumentFilter();
var context = CreateDocumentFilterContext();

filter.Apply(document, context);

Assert.Equal(2, document.Paths.Count);

var endpoint1Paths = document.Paths["/v1/endpoint-1"];

Assert.Empty(endpoint1Paths.Parameters);
Assert.Empty(endpoint1Paths.Operations[OperationType.Get].Parameters);

var endpoint2Paths = document.Paths["/v1/endpoint-2/{id}"];

Assert.Single(endpoint2Paths.Parameters);
Assert.Equal("id", endpoint2Paths.Parameters[0].Name);

Assert.Single(endpoint2Paths.Operations[OperationType.Get].Parameters);
Assert.Equal("id", endpoint2Paths.Parameters[0].Name);
}

private static DocumentFilterContext CreateDocumentFilterContext()
{
var schemaGenerator = new SchemaGenerator(
new SchemaGeneratorOptions(),
new JsonSerializerDataContractResolver(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}
)
);
var schemaRepository = new SchemaRepository();

return new DocumentFilterContext([], schemaGenerator, schemaRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public class SwaggerConfig(
{
public void Configure(SwaggerGenOptions options)
{
options.DocumentFilter<VersionedPathsDocumentFilter>();

options.OperationFilter<DefaultValuesOperationFilter>();
options.OperationFilter<GeneralResponseOperationFilter>();

options.SchemaFilter<JsonConverterSchemaFilter>();
options.SchemaFilter<RequiredPropertySchemaFilter>();
options.SchemaFilter<SwaggerEnumSchemaFilter>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace GovUk.Education.ExploreEducationStatistics.Public.Data.Api.Swagger;

public class VersionedPathsDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument document, DocumentFilterContext context)
{
var newPaths = new OpenApiPaths();

foreach (var path in document.Paths)
{
var versionedPath = path.Key.Replace("{version}", document.Info.Version);

newPaths[versionedPath] = path.Value;

path.Value.Parameters = path.Value.Parameters
.Where(p => p.Name != "version")
.ToList();

foreach (var operation in path.Value.Operations.Values)
{
operation.Parameters = operation.Parameters
.Where(p => p.Name != "version")
.ToList();
}
}

document.Paths = newPaths;
}
}

0 comments on commit 5c64ab6

Please sign in to comment.