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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.AspNetCore.Mvc;

namespace HideEndpointInOpenApi.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries =
[
"Freezing",
"Bracing",
"Chilly",
"Cool",
"Mild",
"Warm",
"Balmy",
"Hot",
"Sweltering",
"Scorching"
];

[HttpGet("GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return GetWeatherForecastData();
}

[HttpGet("GetMethodOne")]
[NonAction]
public IEnumerable<WeatherForecast> GetMethodOne()
{
return GetWeatherForecastData();
}

[HttpGet("GetMethodTwo")]
[ApiExplorerSettings(IgnoreApi = true)]
public IEnumerable<WeatherForecast> GetMethodTwo()
{
return GetWeatherForecastData();
}

[HttpGet("GetMethodThree")]
public IEnumerable<WeatherForecast> GetMethodThree()
{
return GetWeatherForecastData();
}

[HttpGet("GetMethodFour")]
public IEnumerable<WeatherForecast> GetMethodFour()
{
return GetWeatherForecastData();
}

private IEnumerable<WeatherForecast> GetWeatherForecastData()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Mvc.ApplicationModels;

namespace HideEndpointInOpenApi.Conventions;

public class HideActionConvention : IActionModelConvention
{
public void Apply(ActionModel action)
{
if (action.ActionName == "GetMethodThree")
{
action.ApiExplorer.IsVisible = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using HideEndpointInOpenApi.Conventions;
using HideEndpointInOpenApi.Transformers;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers(s =>
s.Conventions.Add(new HideActionConvention()
));

builder.Services.AddOpenApi(options =>
{
options.ShouldInclude = apiDesc => apiDesc.RelativePath != "WeatherForecast/GetWeatherForecast";
options.AddDocumentTransformer<HideEndpointDocumentTransformer>();
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.MapGet("/internal", () => "internal").ExcludeFromDescription();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi;

namespace HideEndpointInOpenApi.Transformers;

public class HideEndpointDocumentTransformer : IOpenApiDocumentTransformer
{
public Task TransformAsync(OpenApiDocument document,
OpenApiDocumentTransformerContext context,
CancellationToken cancellationToken)
{
document.Paths.Remove("/WeatherForecast/GetMethodFour");

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace HideEndpointInOpenApi;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HideEndpointInSwagger", "Hi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HideEndpointInSwaggerTests", "HideEndpointInSwaggerTests\HideEndpointInSwaggerTests.csproj", "{997C22B1-9AA6-44C1-82F0-96A9FB3DD6E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HideEndpointInOpenApi", "HideEndpointInOpenApi\HideEndpointInOpenApi.csproj", "{6B2FE7B7-D288-4F8C-85D3-2F0E8906B397}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{997C22B1-9AA6-44C1-82F0-96A9FB3DD6E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{997C22B1-9AA6-44C1-82F0-96A9FB3DD6E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{997C22B1-9AA6-44C1-82F0-96A9FB3DD6E5}.Release|Any CPU.Build.0 = Release|Any CPU
{6B2FE7B7-D288-4F8C-85D3-2F0E8906B397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B2FE7B7-D288-4F8C-85D3-2F0E8906B397}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B2FE7B7-D288-4F8C-85D3-2F0E8906B397}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B2FE7B7-D288-4F8C-85D3-2F0E8906B397}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace HideEndpointInSwagger.Controllers;
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries =
{
[
"Freezing",
"Bracing",
"Chilly",
Expand All @@ -18,7 +18,7 @@ public class WeatherForecastController : ControllerBase
"Hot",
"Sweltering",
"Scorching"
};
];

[HttpGet("GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace HideEndpointInSwagger.Filters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
using HideEndpointInSwagger.Conventions;
using HideEndpointInSwagger.Filters;

namespace HideEndpointInSwagger;
var builder = WebApplication.CreateBuilder(args);

public class Program
// Add services to the container.
builder.Services.AddControllers(s =>
s.Conventions.Add(new HideControllerConvention()
));

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddSwaggerGen(c =>
{
public static void Main(string[] args)
c.DocumentFilter<SwaggerDocumentFilter>();
c.DocInclusionPredicate((docName, apiDesc) =>
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers(s =>
s.Conventions.Add(new HideControllerConvention()
));

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
var routeTemplate = apiDesc.RelativePath;

builder.Services.AddSwaggerGen(c =>
{
c.DocumentFilter<SwaggerDocumentFilter>();
c.DocInclusionPredicate((docName, apiDesc) =>
{
var routeTemplate = apiDesc.RelativePath;
if (routeTemplate == "WeatherForecast/GetWeatherForecast")
return false;

if (routeTemplate == "WeatherForecast/GetWeatherForecast")
return false;
return true;
});
});

return true;
});
});
var app = builder.Build();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseHttpsRedirection();

app.UseAuthorization();
app.UseAuthorization();

app.MapControllers();
app.MapControllers();

app.Run();
}
}
app.Run();
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading