-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor REST API options and document filter
- Loading branch information
1 parent
087d7cb
commit 2d3ab2f
Showing
3 changed files
with
39 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace ProjectOrigin.Stamp.Server.Options; | ||
|
||
public class RestApiOptions | ||
{ | ||
[Required(AllowEmptyStrings = false)] | ||
public PathString PathBase { get; set; } = string.Empty; | ||
} | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace ProjectOrigin.Stamp.Server.Options; | ||
|
||
public class RestApiOptions | ||
{ | ||
[Required(AllowEmptyStrings = false)] | ||
public PathString PathBase { get; set; } = string.Empty; | ||
} |
56 changes: 28 additions & 28 deletions
56
src/ProjectOrigin.Stamp.Server/Services/REST/PathBaseDocumentFilter.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 |
---|---|---|
@@ -1,28 +1,28 @@ | ||
using System.Linq; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Models; | ||
using ProjectOrigin.Stamp.Server.Options; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace ProjectOrigin.Stamp.Server.Services.REST; | ||
|
||
public class PathBaseDocumentFilter : IDocumentFilter | ||
{ | ||
private readonly IOptions<RestApiOptions> _options; | ||
|
||
public PathBaseDocumentFilter(IOptions<RestApiOptions> options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
var paths = swaggerDoc.Paths.ToArray(); | ||
foreach (var path in paths) | ||
{ | ||
swaggerDoc.Paths.Remove(path.Key); | ||
var keyWithBasePath = $"{_options.Value.PathBase}{path.Key}"; | ||
swaggerDoc.Paths.Add(keyWithBasePath, path.Value); | ||
} | ||
} | ||
} | ||
using System.Linq; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Models; | ||
using ProjectOrigin.Stamp.Server.Options; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace ProjectOrigin.Stamp.Server.Services.REST; | ||
|
||
public class PathBaseDocumentFilter : IDocumentFilter | ||
{ | ||
private readonly IOptions<RestApiOptions> _options; | ||
|
||
public PathBaseDocumentFilter(IOptions<RestApiOptions> options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
var paths = swaggerDoc.Paths.ToArray(); | ||
foreach (var path in paths) | ||
{ | ||
swaggerDoc.Paths.Remove(path.Key); | ||
var keyWithBasePath = $"{_options.Value.PathBase}{path.Key}"; | ||
swaggerDoc.Paths.Add(keyWithBasePath, path.Value); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ProjectOrigin.Stamp.Test | ||
namespace ProjectOrigin.Stamp.Test | ||
{ | ||
public class Class1 | ||
{ | ||
|