Skip to content

Commit bef1764

Browse files
committed
Add WithVariable server extension method
1 parent 853ac9e commit bef1764

File tree

7 files changed

+122
-114
lines changed

7 files changed

+122
-114
lines changed

SampleApp9000/Controllers/WeatherForecastController.cs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.Extensions.Logging;
65

7-
namespace SampleApp9000.Controllers
6+
namespace SampleApp9000.Controllers;
7+
8+
[ApiController]
9+
[Route("[controller]")]
10+
public class WeatherForecastController : ControllerBase
811
{
9-
[ApiController]
10-
[Route("[controller]")]
11-
public class WeatherForecastController : ControllerBase
12+
private static readonly string[] Summaries =
1213
{
13-
private static readonly string[] Summaries = new[]
14-
{
15-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
16-
};
17-
18-
private readonly ILogger<WeatherForecastController> _logger;
14+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
15+
};
1916

20-
public WeatherForecastController(ILogger<WeatherForecastController> logger)
21-
{
22-
_logger = logger;
23-
}
24-
25-
[HttpGet]
26-
public IEnumerable<WeatherForecast> Get()
27-
{
28-
var rng = new Random();
29-
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
30-
{
31-
Date = DateTime.Now.AddDays(index),
32-
TemperatureC = rng.Next(-20, 55),
33-
Summary = Summaries[rng.Next(Summaries.Length)]
34-
})
35-
.ToArray();
36-
}
17+
[HttpGet]
18+
public IEnumerable<WeatherForecast> Get()
19+
{
20+
var rng = new Random();
21+
return
22+
Enumerable
23+
.Range(1, 5)
24+
.Select
25+
(
26+
index => new WeatherForecast
27+
{
28+
Date = DateTime.Now.AddDays(index),
29+
TemperatureC = rng.Next(-20, 55),
30+
Summary = Summaries[rng.Next(Summaries.Length)]
31+
}
32+
)
33+
.ToArray();
3734
}
38-
}
35+
}

SampleApp9000/Program.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using Microsoft.AspNetCore.Hosting;
22
using Microsoft.Extensions.Hosting;
33

4-
namespace SampleApp9000
4+
namespace SampleApp9000;
5+
6+
public class Program
57
{
6-
public class Program
8+
public static void Main(string[] args)
79
{
8-
public static void Main(string[] args)
9-
{
10-
CreateHostBuilder(args).Build().Run();
11-
}
12-
13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
14-
Host.CreateDefaultBuilder(args)
15-
.ConfigureWebHostDefaults(webBuilder =>
16-
{
17-
webBuilder.UseStartup<Startup>();
18-
});
10+
CreateHostBuilder(args).Build().Run();
1911
}
20-
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
19+
}

SampleApp9000/SampleApp9000.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

SampleApp9000/Startup.cs

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.Generic;
21
using Microsoft.AspNetCore.Builder;
32
using Microsoft.AspNetCore.Hosting;
43
using Microsoft.OpenApi.Models;
@@ -7,75 +6,72 @@
76
using Microsoft.Extensions.Hosting;
87
using Swashbuckle.Servers.Extension;
98

10-
namespace SampleApp9000
9+
namespace SampleApp9000;
10+
11+
public class Startup
1112
{
12-
public class Startup
13+
public Startup(IConfiguration configuration)
1314
{
14-
public Startup(IConfiguration configuration)
15-
{
16-
Configuration = configuration;
17-
}
15+
Configuration = configuration;
16+
}
1817

19-
public IConfiguration Configuration { get; }
18+
public IConfiguration Configuration { get; }
2019

21-
// This method gets called by the runtime. Use this method to add services to the container.
22-
public void ConfigureServices(IServiceCollection services)
23-
{
24-
services.AddControllers();
20+
// This method gets called by the runtime. Use this method to add services to the container.
21+
public void ConfigureServices(IServiceCollection services)
22+
{
23+
services.AddControllers();
2524

26-
services.AddSwaggerGen(c =>
27-
{
28-
c.SwaggerDoc
29-
(
30-
"sampleapp",
31-
new OpenApiInfo
32-
{
33-
Title = "Sample App 9000",
34-
Version = "1.0.0",
35-
Description = "Provides a simple example of the tool"
36-
}
37-
);
25+
services.AddSwaggerGen(c =>
26+
{
27+
c.SwaggerDoc
28+
(
29+
"sampleapp",
30+
new OpenApiInfo
31+
{
32+
Title = "Sample App 9000",
33+
Version = "1.0.0",
34+
Description = "Provides a simple example of the tool"
35+
}
36+
);
3837

39-
c.WithServers
40-
(
41-
new List<OpenApiServer>
42-
{
43-
new OpenApiServer { Url = "http://localhost:5000" },
44-
new OpenApiServer { Url = "https://www.yourcustomdomain.com" }
45-
}
46-
);
47-
});
48-
}
38+
c.WithServers
39+
(
40+
new[]
41+
{
42+
new OpenApiServer { Url = "http://localhost:5000" },
43+
new OpenApiServer { Url = "https://www.yourcustomdomain.com" }
44+
}
45+
);
46+
});
47+
}
4948

50-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
51-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
49+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
50+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
51+
{
52+
if (env.IsDevelopment())
5253
{
53-
if (env.IsDevelopment())
54-
{
55-
app.UseDeveloperExceptionPage();
56-
}
54+
app.UseDeveloperExceptionPage();
55+
}
5756

58-
app.UseHttpsRedirection();
57+
app.UseHttpsRedirection();
5958

60-
app.UseRouting();
59+
app.UseRouting();
6160

62-
app.UseAuthorization();
61+
app.UseAuthorization();
6362

64-
app.UseSwagger();
63+
app.UseSwagger();
6564

66-
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
67-
// specifying the Swagger JSON endpoint.
68-
app.UseSwaggerUI(x =>
69-
{
70-
x.DocumentTitle = "SampleApp9000";
71-
x.SwaggerEndpoint("/swagger/sampleapp/swagger.json", "Sample App 9000");
72-
x.RoutePrefix = string.Empty;
73-
});
65+
app.UseSwaggerUI(x =>
66+
{
67+
x.DocumentTitle = "SampleApp9000";
68+
x.SwaggerEndpoint("/swagger/sampleapp/swagger.json", "Sample App 9000");
69+
x.RoutePrefix = string.Empty;
70+
});
7471

75-
app.UseEndpoints(endpoints =>
76-
{
77-
endpoints.MapControllers();
78-
});
79-
}
72+
app.UseEndpoints(endpoints =>
73+
{
74+
endpoints.MapControllers();
75+
});
8076
}
81-
}
77+
}

SampleApp9000/WeatherForecast.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22

3-
namespace SampleApp9000
3+
namespace SampleApp9000;
4+
5+
public class WeatherForecast
46
{
5-
public class WeatherForecast
6-
{
7-
public DateTime Date { get; set; }
7+
public DateTime Date { get; set; }
88

9-
public int TemperatureC { get; set; }
9+
public int TemperatureC { get; set; }
1010

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

13-
public string Summary { get; set; }
14-
}
15-
}
13+
public string Summary { get; set; }
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.OpenApi.Models;
2+
3+
namespace Swashbuckle.Servers.Extension.Extensions
4+
{
5+
public static class OpenApiServerExtensions
6+
{
7+
public static OpenApiServer WithVariable(this OpenApiServer server, string key, OpenApiServerVariable value)
8+
{
9+
server.Variables.Add(key, value);
10+
11+
return server;
12+
}
13+
}
14+
}

Swashbuckle.Servers.Extension/Swashbuckle.Servers.Extension.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
<RepositoryUrl>https://github.com/waxtell/Swashbuckle.Servers.Extension.git</RepositoryUrl>
99
<RepositoryType>git</RepositoryType>
1010
<PackageTags>OAS Swashbuckle Servers</PackageTags>
11-
<PackageReleaseNotes>1.0.2- Now supports multiple, concatenating invocations
12-
1.0.1- Initial release</PackageReleaseNotes>
13-
<Version>1.0.2</Version>
11+
<PackageReleaseNotes>
12+
1.0.3 - Add WithVariable extension
13+
1.0.2 - Now supports multiple, concatenating invocations
14+
1.0.1 - Initial release
15+
</PackageReleaseNotes>
16+
<Version>1.0.3</Version>
1417
</PropertyGroup>
1518

1619
<ItemGroup>

0 commit comments

Comments
 (0)