Skip to content

Commit ade06df

Browse files
committed
updated samples
1 parent f1a363a commit ade06df

File tree

24 files changed

+190
-154
lines changed

24 files changed

+190
-154
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.6-SNAPSHOT
1+
2.4.14-SNAPSHOT

samples/server/petstore/aspnetcore-interface-controller/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# IO.Swagger - ASP.NET Core 2.0 Server
1+
# IO.Swagger - ASP.NET Core 3.0 Server
22

33
This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
44

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Controllers/IPetApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public interface IPetApiController
9393

9494
/// <param name="petId">ID of pet to update</param>
9595
/// <param name="additionalMetadata">Additional data to pass to server</param>
96-
/// <param name="file">file to upload</param>
96+
/// <param name="_file">file to upload</param>
9797
/// <response code="200">successful operation</response>
98-
IActionResult UploadFile([FromRoute][Required]long? petId, [FromForm]string additionalMetadata, [FromForm]System.IO.Stream file);
98+
IActionResult UploadFile([FromRoute][Required]long? petId, [FromForm]string additionalMetadata, [FromForm]System.IO.Stream _file);
9999
}
100100
}

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Controllers/PetApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ public virtual IActionResult UpdatePetWithForm([FromRoute][Required]long? petId,
219219

220220
/// <param name="petId">ID of pet to update</param>
221221
/// <param name="additionalMetadata">Additional data to pass to server</param>
222-
/// <param name="file">file to upload</param>
222+
/// <param name="_file">file to upload</param>
223223
/// <response code="200">successful operation</response>
224224
[HttpPost]
225225
[Route("/v2/pet/{petId}/uploadImage")]
226226
[ValidateModelState]
227227
[SwaggerOperation("UploadFile")]
228228
[SwaggerResponse(statusCode: 200, type: typeof(ApiResponse), description: "successful operation")]
229-
public virtual IActionResult UploadFile([FromRoute][Required]long? petId, [FromForm]string additionalMetadata, [FromForm]System.IO.Stream file)
229+
public virtual IActionResult UploadFile([FromRoute][Required]long? petId, [FromForm]string additionalMetadata, [FromForm]System.IO.Stream _file)
230230
{
231231
//TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
232232
// return StatusCode(200, default(ApiResponse));

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Controllers/UserApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
using Newtonsoft.Json;
1717
using System.ComponentModel.DataAnnotations;
1818
using IO.Swagger.Attributes;
19-
using IO.Swagger.Security;
19+
2020
using Microsoft.AspNetCore.Authorization;
2121
using IO.Swagger.Models;
2222

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM microsoft/aspnetcore-build:2.0 AS build-env
1+
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build-env
22
WORKDIR /app
33

44
ENV DOTNET_CLI_TELEMETRY_OPTOUT 1
@@ -12,7 +12,7 @@ COPY . ./
1212
RUN dotnet publish -c Release -o out
1313

1414
# build runtime image
15-
FROM microsoft/aspnetcore:2.0
15+
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
1616
WORKDIR /app
1717
COPY --from=build-env /app/out .
1818
ENTRYPOINT ["dotnet", "IO.Swagger.dll"]

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Filters/BasePathFilter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.RegularExpressions;
33
using Swashbuckle.AspNetCore.Swagger;
44
using Swashbuckle.AspNetCore.SwaggerGen;
5+
using Microsoft.OpenApi.Models;
56

67
namespace IO.Swagger.Filters
78
{
@@ -28,11 +29,11 @@ public BasePathFilter(string basePath)
2829
/// <summary>
2930
/// Apply the filter
3031
/// </summary>
31-
/// <param name="swaggerDoc">SwaggerDocument</param>
32+
/// <param name="swaggerDoc">OpenApiDocument</param>
3233
/// <param name="context">FilterContext</param>
33-
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
34+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
3435
{
35-
swaggerDoc.BasePath = this.BasePath;
36+
swaggerDoc.Servers.Add(new OpenApiServer() { Url = this.BasePath });
3637

3738
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(this.BasePath)).ToList();
3839

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Filters/GeneratePathParamsValidationFilter.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Mvc.Controllers;
44
using Swashbuckle.AspNetCore.Swagger;
55
using Swashbuckle.AspNetCore.SwaggerGen;
6+
using Microsoft.OpenApi.Models;
67

78
namespace IO.Swagger.Filters
89
{
@@ -16,7 +17,7 @@ public class GeneratePathParamsValidationFilter : IOperationFilter
1617
/// </summary>
1718
/// <param name="operation">Operation</param>
1819
/// <param name="context">OperationFilterContext</param>
19-
public void Apply(Operation operation, OperationFilterContext context)
20+
public void Apply(OpenApiOperation operation, OperationFilterContext context)
2021
{
2122
var pars = context.ApiDescription.ParameterDescriptions;
2223

@@ -40,9 +41,9 @@ public void Apply(Operation operation, OperationFilterContext context)
4041
if (regexAttr != null)
4142
{
4243
string regex = (string)regexAttr.ConstructorArguments[0].Value;
43-
if (swaggerParam is NonBodyParameter)
44+
if (swaggerParam is OpenApiParameter)
4445
{
45-
((NonBodyParameter)swaggerParam).Pattern = regex;
46+
((OpenApiParameter)swaggerParam).Schema.Pattern = regex;
4647
}
4748
}
4849

@@ -70,10 +71,10 @@ public void Apply(Operation operation, OperationFilterContext context)
7071
maxLength = (int)maxLengthAttr.ConstructorArguments[0].Value;
7172
}
7273

73-
if (swaggerParam is NonBodyParameter)
74+
if (swaggerParam is OpenApiParameter)
7475
{
75-
((NonBodyParameter)swaggerParam).MinLength = minLenght;
76-
((NonBodyParameter)swaggerParam).MaxLength = maxLength;
76+
((OpenApiParameter)swaggerParam).Schema.MinLength = minLenght;
77+
((OpenApiParameter)swaggerParam).Schema.MaxLength = maxLength;
7778
}
7879

7980
// Range [Range]
@@ -83,10 +84,10 @@ public void Apply(Operation operation, OperationFilterContext context)
8384
int rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
8485
int rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;
8586

86-
if (swaggerParam is NonBodyParameter)
87+
if (swaggerParam is OpenApiParameter)
8788
{
88-
((NonBodyParameter)swaggerParam).Minimum = rangeMin;
89-
((NonBodyParameter)swaggerParam).Maximum = rangeMax;
89+
((OpenApiParameter)swaggerParam).Schema.Minimum = rangeMin;
90+
((OpenApiParameter)swaggerParam).Schema.Maximum = rangeMax;
9091
}
9192
}
9293
}

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/IO.Swagger.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
<PropertyGroup>
33
<Description>IO.Swagger</Description>
44
<Copyright>IO.Swagger</Copyright>
5-
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<PreserveCompilationContext>true</PreserveCompilationContext>
88
<AssemblyName>IO.Swagger</AssemblyName>
99
<PackageId>IO.Swagger</PackageId>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.AspNetCore.App" />
13-
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"/>
14-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
12+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4"/>
13+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4"/>
14+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc4"/>
15+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc4" />
1516
</ItemGroup>
1617
<ItemGroup>
17-
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
18+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
1819
</ItemGroup>
1920
</Project>

samples/server/petstore/aspnetcore-interface-controller/src/IO.Swagger/Startup.cs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,29 @@
1010

1111
using System;
1212
using System.IO;
13+
using Microsoft.AspNetCore.Authentication;
1314
using Microsoft.AspNetCore.Builder;
1415
using Microsoft.AspNetCore.Hosting;
1516
using Microsoft.Extensions.Configuration;
1617
using Microsoft.Extensions.DependencyInjection;
18+
using Microsoft.Extensions.Hosting;
1719
using Microsoft.Extensions.Logging;
20+
using Microsoft.OpenApi.Models;
1821
using Newtonsoft.Json.Converters;
1922
using Newtonsoft.Json.Serialization;
2023
using Swashbuckle.AspNetCore.Swagger;
2124
using Swashbuckle.AspNetCore.SwaggerGen;
2225
using IO.Swagger.Filters;
2326
using IO.Swagger.Security;
2427

25-
using Microsoft.AspNetCore.Authentication;
26-
2728
namespace IO.Swagger
2829
{
2930
/// <summary>
3031
/// Startup
3132
/// </summary>
3233
public class Startup
3334
{
34-
private readonly IHostingEnvironment _hostingEnv;
35+
private readonly IWebHostEnvironment _hostingEnv;
3536

3637
private IConfiguration Configuration { get; }
3738

@@ -40,7 +41,7 @@ public class Startup
4041
/// </summary>
4142
/// <param name="env"></param>
4243
/// <param name="configuration"></param>
43-
public Startup(IHostingEnvironment env, IConfiguration configuration)
44+
public Startup(IWebHostEnvironment env, IConfiguration configuration)
4445
{
4546
_hostingEnv = env;
4647
Configuration = configuration;
@@ -54,13 +55,15 @@ public void ConfigureServices(IServiceCollection services)
5455
{
5556
// Add framework services.
5657
services
57-
.AddMvc()
58-
.AddJsonOptions(opts =>
58+
.AddMvc(options =>
59+
{
60+
options.InputFormatters.RemoveType<Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter>();
61+
options.OutputFormatters.RemoveType<Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter>();
62+
})
63+
.AddNewtonsoftJson(opts =>
5964
{
6065
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
61-
opts.SerializerSettings.Converters.Add(new StringEnumConverter {
62-
CamelCaseText = true
63-
});
66+
opts.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));
6467
})
6568
.AddXmlSerializerFormatters();
6669

@@ -71,21 +74,20 @@ public void ConfigureServices(IServiceCollection services)
7174
services
7275
.AddSwaggerGen(c =>
7376
{
74-
c.SwaggerDoc("1.0.0", new Info
77+
c.SwaggerDoc("1.0.0", new OpenApiInfo
7578
{
7679
Version = "1.0.0",
7780
Title = "Swagger Petstore",
78-
Description = "Swagger Petstore (ASP.NET Core 2.0)",
79-
Contact = new Contact()
81+
Description = "Swagger Petstore (ASP.NET Core 3.0)",
82+
Contact = new OpenApiContact()
8083
{
8184
Name = "Swagger Codegen Contributors",
82-
Url = "https://github.com/swagger-api/swagger-codegen",
85+
Url = new Uri("https://github.com/swagger-api/swagger-codegen"),
8386
8487
},
85-
TermsOfService = "http://swagger.io/terms/"
88+
TermsOfService = new Uri("http://swagger.io/terms/")
8689
});
87-
c.CustomSchemaIds(type => type.FriendlyId(true));
88-
c.DescribeAllEnumsAsStrings();
90+
c.CustomSchemaIds(type => type.FullName);
8991
c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{_hostingEnv.ApplicationName}.xml");
9092
// Sets the basePath property in the Swagger document generated
9193
c.DocumentFilter<BasePathFilter>("/v2");
@@ -102,21 +104,32 @@ public void ConfigureServices(IServiceCollection services)
102104
/// <param name="app"></param>
103105
/// <param name="env"></param>
104106
/// <param name="loggerFactory"></param>
105-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
107+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
106108
{
107-
app
108-
.UseMvc()
109-
.UseDefaultFiles()
110-
.UseStaticFiles()
111-
.UseSwagger()
112-
.UseSwaggerUI(c =>
113-
{
114-
//TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
115-
c.SwaggerEndpoint("/swagger/1.0.0/swagger.json", "Swagger Petstore");
109+
app.UseRouting();
116110

117-
//TODO: Or alternatively use the original Swagger contract that's included in the static files
118-
// c.SwaggerEndpoint("/swagger-original.json", "Swagger Petstore Original");
119-
});
111+
//TODO: Uncomment this if you need wwwroot folder
112+
// app.UseStaticFiles();
113+
114+
app.UseAuthorization();
115+
116+
app.UseSwagger();
117+
app.UseSwaggerUI(c =>
118+
{
119+
//TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
120+
c.SwaggerEndpoint("/swagger/1.0.0/swagger.json", "Swagger Petstore");
121+
122+
//TODO: Or alternatively use the original Swagger contract that's included in the static files
123+
// c.SwaggerEndpoint("/swagger-original.json", "Swagger Petstore Original");
124+
});
125+
126+
//TODO: Use Https Redirection
127+
// app.UseHttpsRedirection();
128+
129+
app.UseEndpoints(endpoints =>
130+
{
131+
endpoints.MapControllers();
132+
});
120133

121134
if (env.IsDevelopment())
122135
{
@@ -125,7 +138,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
125138
else
126139
{
127140
//TODO: Enable production exception handling (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling)
128-
// app.UseExceptionHandler("/Home/Error");
141+
app.UseExceptionHandler("/Error");
142+
143+
app.UseHsts();
129144
}
130145
}
131146
}

0 commit comments

Comments
 (0)