-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5396 from dfe-analytical-services/ees-5663
EES-5663 Tweaks to public API versioning
- Loading branch information
Showing
15 changed files
with
181 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...oreEducationStatistics.Public.Data.Api.Tests/Swagger/VersionedPathsDocumentFilterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...cation.ExploreEducationStatistics.Public.Data.Api/Swagger/VersionedPathsDocumentFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters