Skip to content

Commit

Permalink
Refactor REST API options and document filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchmidt committed Apr 15, 2024
1 parent 087d7cb commit 2d3ab2f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
20 changes: 10 additions & 10 deletions src/ProjectOrigin.Stamp.Server/Options/RestApiOptions.cs
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;
}
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);
}
}
}
2 changes: 1 addition & 1 deletion src/ProjectOrigin.Stamp.Test/Class1.cs
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
{
Expand Down

0 comments on commit 2d3ab2f

Please sign in to comment.