[aspnetcore] Make the use of Swashbuckle optional (#110)

* Respect the sourceFolder option correctly

* Add an option to disable the use of Swashbuckle (useSwashbuckle)
This commit is contained in:
Matthias Baer 2018-05-20 15:28:24 +02:00 committed by Jim Schubert
parent cd4640dab6
commit d9d6530161
23 changed files with 70 additions and 148 deletions

View File

@ -34,16 +34,19 @@ import static java.util.UUID.randomUUID;
public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
public static final String USE_SWASHBUCKLE = "useSwashbuckle";
private String packageGuid = "{" + randomUUID().toString().toUpperCase() + "}"; private String packageGuid = "{" + randomUUID().toString().toUpperCase() + "}";
@SuppressWarnings("hiding") @SuppressWarnings("hiding")
protected Logger LOGGER = LoggerFactory.getLogger(AspNetCoreServerCodegen.class); protected Logger LOGGER = LoggerFactory.getLogger(AspNetCoreServerCodegen.class);
private boolean useSwashbuckle = true;
public AspNetCoreServerCodegen() { public AspNetCoreServerCodegen() {
super(); super();
setSourceFolder("src"); outputFolder = "generated-code" + File.separator + getName();
outputFolder = "generated-code" + File.separator + this.getName();
modelTemplateFiles.put("model.mustache", ".cs"); modelTemplateFiles.put("model.mustache", ".cs");
apiTemplateFiles.put("controller.mustache", ".cs"); apiTemplateFiles.put("controller.mustache", ".cs");
@ -59,11 +62,11 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
// CLI options // CLI options
addOption(CodegenConstants.PACKAGE_NAME, addOption(CodegenConstants.PACKAGE_NAME,
"C# package name (convention: Title.Case).", "C# package name (convention: Title.Case).",
this.packageName); packageName);
addOption(CodegenConstants.PACKAGE_VERSION, addOption(CodegenConstants.PACKAGE_VERSION,
"C# package version.", "C# package version.",
this.packageVersion); packageVersion);
addOption(CodegenConstants.OPTIONAL_PROJECT_GUID, addOption(CodegenConstants.OPTIONAL_PROJECT_GUID,
CodegenConstants.OPTIONAL_PROJECT_GUID_DESC, CodegenConstants.OPTIONAL_PROJECT_GUID_DESC,
@ -76,19 +79,23 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
// CLI Switches // CLI Switches
addSwitch(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, addSwitch(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG,
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC,
this.sortParamsByRequiredFlag); sortParamsByRequiredFlag);
addSwitch(CodegenConstants.USE_DATETIME_OFFSET, addSwitch(CodegenConstants.USE_DATETIME_OFFSET,
CodegenConstants.USE_DATETIME_OFFSET_DESC, CodegenConstants.USE_DATETIME_OFFSET_DESC,
this.useDateTimeOffsetFlag); useDateTimeOffsetFlag);
addSwitch(CodegenConstants.USE_COLLECTION, addSwitch(CodegenConstants.USE_COLLECTION,
CodegenConstants.USE_COLLECTION_DESC, CodegenConstants.USE_COLLECTION_DESC,
this.useCollection); useCollection);
addSwitch(CodegenConstants.RETURN_ICOLLECTION, addSwitch(CodegenConstants.RETURN_ICOLLECTION,
CodegenConstants.RETURN_ICOLLECTION_DESC, CodegenConstants.RETURN_ICOLLECTION_DESC,
this.returnICollection); returnICollection);
addSwitch(USE_SWASHBUCKLE,
"Uses the Swashbuckle.AspNetCore NuGet package for documentation.",
useSwashbuckle);
} }
@Override @Override
@ -115,7 +122,13 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
} }
additionalProperties.put("packageGuid", packageGuid); additionalProperties.put("packageGuid", packageGuid);
additionalProperties.put("dockerTag", this.packageName.toLowerCase()); if (additionalProperties.containsKey(USE_SWASHBUCKLE)) {
useSwashbuckle = convertPropertyToBooleanAndWriteBack(USE_SWASHBUCKLE);
} else {
additionalProperties.put(USE_SWASHBUCKLE, useSwashbuckle);
}
additionalProperties.put("dockerTag", packageName.toLowerCase());
apiPackage = packageName + ".Controllers"; apiPackage = packageName + ".Controllers";
modelPackage = packageName + ".Models"; modelPackage = packageName + ".Models";
@ -125,7 +138,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh")); supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh"));
supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat")); supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("Solution.mustache", "", this.packageName + ".sln")); supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile")); supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore")); supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore"));
supportingFiles.add(new SupportingFile("appsettings.json", packageFolder, "appsettings.json")); supportingFiles.add(new SupportingFile("appsettings.json", packageFolder, "appsettings.json"));
@ -135,12 +148,14 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs")); supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs"));
supportingFiles.add(new SupportingFile("web.config", packageFolder, "web.config")); supportingFiles.add(new SupportingFile("web.config", packageFolder, "web.config"));
supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, this.packageName + ".csproj")); supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj"));
supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", packageFolder + File.separator + "Properties", "launchSettings.json")); supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", packageFolder + File.separator + "Properties", "launchSettings.json"));
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs")); if (useSwashbuckle) {
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs")); supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
}
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md")); supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md"));
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html")); supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html"));
@ -149,19 +164,6 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache", packageFolder + File.separator + "wwwroot", "openapi-original.json")); supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache", packageFolder + File.separator + "wwwroot", "openapi-original.json"));
} }
@Override
public void setSourceFolder(final String sourceFolder) {
if (sourceFolder == null) {
LOGGER.warn("No sourceFolder specified, using default");
this.sourceFolder = "src" + File.separator + this.packageName;
} else if (!sourceFolder.equals("src") && !sourceFolder.startsWith("src")) {
LOGGER.warn("ASP.NET Core requires source code exists under src. Adjusting.");
this.sourceFolder = "src" + File.separator + sourceFolder;
} else {
this.sourceFolder = sourceFolder;
}
}
public void setPackageGuid(String packageGuid) { public void setPackageGuid(String packageGuid) {
this.packageGuid = packageGuid; this.packageGuid = packageGuid;
} }
@ -176,14 +178,12 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
return outputFolder + File.separator + sourceFolder + File.separator + packageName + File.separator + "Models"; return outputFolder + File.separator + sourceFolder + File.separator + packageName + File.separator + "Models";
} }
@Override @Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) { public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
generateJSONSpecFile(objs); generateJSONSpecFile(objs);
return super.postProcessSupportingFileData(objs); return super.postProcessSupportingFileData(objs);
} }
@Override @Override
protected void processOperation(CodegenOperation operation) { protected void processOperation(CodegenOperation operation) {
super.processOperation(operation); super.processOperation(operation);

View File

@ -32,15 +32,15 @@ namespace {{packageName}}.Filters
/// <param name="context">FilterContext</param> /// <param name="context">FilterContext</param>
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context) public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{ {
swaggerDoc.BasePath = this.BasePath; swaggerDoc.BasePath = BasePath;
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(this.BasePath)).ToList(); var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(BasePath)).ToList();
foreach (var path in pathsToModify) foreach (var path in pathsToModify)
{ {
if (path.Key.StartsWith(this.BasePath)) if (path.Key.StartsWith(BasePath))
{ {
string newKey = Regex.Replace(path.Key, $"^{this.BasePath}", string.Empty); string newKey = Regex.Replace(path.Key, $"^{BasePath}", string.Empty);
swaggerDoc.Paths.Remove(path.Key); swaggerDoc.Paths.Remove(path.Key);
swaggerDoc.Paths.Add(newKey, path.Value); swaggerDoc.Paths.Add(newKey, path.Value);
} }

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
namespace {{packageName}} namespace {{packageName}}

View File

@ -9,8 +9,8 @@
<PackageId>{{packageName}}</PackageId> <PackageId>{{packageName}}</PackageId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.7" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.7" />{{#useSwashbuckle}}
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="2.4.0" />{{/useSwashbuckle}}
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
</ItemGroup> </ItemGroup>

View File

@ -21,7 +21,7 @@ build.bat
## Run in Docker ## Run in Docker
``` ```
cd src/{{packageName}} cd {{sourceFolder}}/{{packageName}}
docker build -t {{dockerTag}} . docker build -t {{dockerTag}} .
docker run -p 5000:5000 {{dockerTag}} docker run -p 5000:5000 {{dockerTag}}
``` ```

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.27428.2043 VisualStudioVersion = 15.0.27428.2043
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "src\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "{{sourceFolder}}\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -5,12 +5,11 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;{{#useSwashbuckle}}
using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using {{packageName}}.Filters; using {{packageName}}.Filters;{{/useSwashbuckle}}
namespace {{packageName}} namespace {{packageName}}
{ {
@ -20,8 +19,7 @@ namespace {{packageName}}
public class Startup public class Startup
{ {
private readonly IHostingEnvironment _hostingEnv; private readonly IHostingEnvironment _hostingEnv;
private readonly IConfiguration _configuration;
private IConfiguration Configuration { get; }
/// <summary> /// <summary>
/// Constructor /// Constructor
@ -31,7 +29,7 @@ namespace {{packageName}}
public Startup(IHostingEnvironment env, IConfiguration configuration) public Startup(IHostingEnvironment env, IConfiguration configuration)
{ {
_hostingEnv = env; _hostingEnv = env;
Configuration = configuration; _configuration = configuration;
} }
/// <summary> /// <summary>
@ -46,10 +44,11 @@ namespace {{packageName}}
.AddJsonOptions(opts => .AddJsonOptions(opts =>
{ {
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
opts.SerializerSettings.Converters.Add(new StringEnumConverter { opts.SerializerSettings.Converters.Add(new StringEnumConverter
{
CamelCaseText = true CamelCaseText = true
}); });
}); });{{#useSwashbuckle}}
services services
.AddSwaggerGen(c => .AddSwaggerGen(c =>
@ -78,21 +77,19 @@ namespace {{packageName}}
// Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..) // Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..)
// Use [ValidateModelState] on Actions to actually validate it in C# as well! // Use [ValidateModelState] on Actions to actually validate it in C# as well!
c.OperationFilter<GeneratePathParamsValidationFilter>(); c.OperationFilter<GeneratePathParamsValidationFilter>();
}); });{{/useSwashbuckle}}
} }
/// <summary> /// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary> /// </summary>
/// <param name="app"></param> /// <param name="app"></param>
/// <param name="env"></param> public void Configure(IApplicationBuilder app)
/// <param name="loggerFactory"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{ {
app app
.UseMvc() .UseMvc()
.UseDefaultFiles() .UseDefaultFiles()
.UseStaticFiles() .UseStaticFiles(){{#useSwashbuckle}}
.UseSwagger(c => .UseSwagger(c =>
{ {
c.RouteTemplate = "swagger/{documentName}/openapi.json"; c.RouteTemplate = "swagger/{documentName}/openapi.json";
@ -104,9 +101,9 @@ namespace {{packageName}}
//TODO: Or alternatively use the original Swagger contract that's included in the static files //TODO: Or alternatively use the original Swagger contract that's included in the static files
// c.SwaggerEndpoint("/openapi-original.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}} Original"); // c.SwaggerEndpoint("/openapi-original.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}} Original");
}); }){{/useSwashbuckle}};
if (env.IsDevelopment()) if (_hostingEnv.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }

View File

@ -3,7 +3,7 @@
@echo off @echo off
dotnet restore src\{{packageName}} dotnet restore {{sourceFolder}}\{{packageName}}
dotnet build src\{{packageName}} dotnet build {{sourceFolder}}\{{packageName}}
echo Now, run the following to start the project: dotnet run -p src\{{packageName}}\{{packageName}}.csproj --launch-profile web. echo Now, run the following to start the project: dotnet run -p {{sourceFolder}}\{{packageName}}\{{packageName}}.csproj --launch-profile web.
echo. echo.

View File

@ -3,6 +3,6 @@
# Generated by: https://openapi-generator.tech # Generated by: https://openapi-generator.tech
# #
dotnet restore src/{{packageName}}/ && \ dotnet restore {{sourceFolder}}/{{packageName}}/ && \
dotnet build src/{{packageName}}/ && \ dotnet build {{sourceFolder}}/{{packageName}}/ && \
echo "Now, run the following to start the project: dotnet run -p src/{{packageName}}/{{packageName}}.csproj --launch-profile web" echo "Now, run the following to start the project: dotnet run -p {{sourceFolder}}/{{packageName}}/{{packageName}}.csproj --launch-profile web"

View File

@ -1,15 +1,8 @@
{{>partial_header}} {{>partial_header}}
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using Microsoft.AspNetCore.Mvc;{{#useSwashbuckle}}
using System.Net; using Swashbuckle.AspNetCore.SwaggerGen;{{/useSwashbuckle}}
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Swashbuckle.AspNetCore.SwaggerGen;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using {{packageName}}.Attributes; using {{packageName}}.Attributes;
@ -25,15 +18,15 @@ namespace {{packageName}}.Controllers
{ {{#operation}} { {{#operation}}
/// <summary> /// <summary>
/// {{#summary}}{{summary}}{{/summary}} /// {{#summary}}{{summary}}{{/summary}}
/// </summary> /// </summary>{{#notes}}
{{#notes}}/// <remarks>{{notes}}</remarks>{{/notes}}{{#allParams}} /// <remarks>{{notes}}</remarks>{{/notes}}{{#allParams}}
/// <param name="{{paramName}}">{{description}}</param>{{/allParams}}{{#responses}} /// <param name="{{paramName}}">{{description}}</param>{{/allParams}}{{#responses}}
/// <response code="{{code}}">{{message}}</response>{{/responses}} /// <response code="{{code}}">{{message}}</response>{{/responses}}
[{{httpMethod}}] [{{httpMethod}}]
[Route("{{{basePathWithoutHost}}}{{{path}}}")] [Route("{{{basePathWithoutHost}}}{{{path}}}")]
[ValidateModelState] [ValidateModelState]{{#useSwashbuckle}}
[SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}} [SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}}
[SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}} [SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}}
public virtual IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) public virtual IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{ {{#responses}} { {{#responses}}
{{#dataType}} {{#dataType}}

View File

@ -1,11 +1,8 @@
{{>partial_header}} {{>partial_header}}
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -10,14 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -34,7 +27,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Add a new pet to the store /// Add a new pet to the store
/// </summary> /// </summary>
/// <param name="pet">Pet object that needs to be added to the store</param> /// <param name="pet">Pet object that needs to be added to the store</param>
/// <response code="405">Invalid input</response> /// <response code="405">Invalid input</response>
[HttpPost] [HttpPost]
@ -53,7 +45,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Deletes a pet /// Deletes a pet
/// </summary> /// </summary>
/// <param name="petId">Pet id to delete</param> /// <param name="petId">Pet id to delete</param>
/// <param name="apiKey"></param> /// <param name="apiKey"></param>
/// <response code="400">Invalid pet value</response> /// <response code="400">Invalid pet value</response>
@ -170,7 +161,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Update an existing pet /// Update an existing pet
/// </summary> /// </summary>
/// <param name="pet">Pet object that needs to be added to the store</param> /// <param name="pet">Pet object that needs to be added to the store</param>
/// <response code="400">Invalid ID supplied</response> /// <response code="400">Invalid ID supplied</response>
/// <response code="404">Pet not found</response> /// <response code="404">Pet not found</response>
@ -197,7 +187,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Updates a pet in the store with form data /// Updates a pet in the store with form data
/// </summary> /// </summary>
/// <param name="petId">ID of pet that needs to be updated</param> /// <param name="petId">ID of pet that needs to be updated</param>
/// <param name="name">Updated name of the pet</param> /// <param name="name">Updated name of the pet</param>
/// <param name="status">Updated status of the pet</param> /// <param name="status">Updated status of the pet</param>
@ -218,7 +207,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// uploads an image /// uploads an image
/// </summary> /// </summary>
/// <param name="petId">ID of pet to update</param> /// <param name="petId">ID of pet to update</param>
/// <param name="additionalMetadata">Additional data to pass to server</param> /// <param name="additionalMetadata">Additional data to pass to server</param>
/// <param name="file">file to upload</param> /// <param name="file">file to upload</param>

View File

@ -10,14 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -116,7 +109,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Place an order for a pet /// Place an order for a pet
/// </summary> /// </summary>
/// <param name="order">order placed for purchasing the pet</param> /// <param name="order">order placed for purchasing the pet</param>
/// <response code="200">successful operation</response> /// <response code="200">successful operation</response>
/// <response code="400">Invalid Order</response> /// <response code="400">Invalid Order</response>

View File

@ -10,14 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -53,7 +46,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Creates list of users with given input array /// Creates list of users with given input array
/// </summary> /// </summary>
/// <param name="user">List of user object</param> /// <param name="user">List of user object</param>
/// <response code="0">successful operation</response> /// <response code="0">successful operation</response>
[HttpPost] [HttpPost]
@ -72,7 +64,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Creates list of users with given input array /// Creates list of users with given input array
/// </summary> /// </summary>
/// <param name="user">List of user object</param> /// <param name="user">List of user object</param>
/// <response code="0">successful operation</response> /// <response code="0">successful operation</response>
[HttpPost] [HttpPost]
@ -114,7 +105,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Get user by user name /// Get user by user name
/// </summary> /// </summary>
/// <param name="username">The name that needs to be fetched. Use user1 for testing.</param> /// <param name="username">The name that needs to be fetched. Use user1 for testing.</param>
/// <response code="200">successful operation</response> /// <response code="200">successful operation</response>
/// <response code="400">Invalid username supplied</response> /// <response code="400">Invalid username supplied</response>
@ -149,7 +139,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Logs user into the system /// Logs user into the system
/// </summary> /// </summary>
/// <param name="username">The user name for login</param> /// <param name="username">The user name for login</param>
/// <param name="password">The password for login in clear text</param> /// <param name="password">The password for login in clear text</param>
/// <response code="200">successful operation</response> /// <response code="200">successful operation</response>
@ -179,7 +168,6 @@ namespace Org.OpenAPITools.Controllers
/// <summary> /// <summary>
/// Logs out current logged in user session /// Logs out current logged in user session
/// </summary> /// </summary>
/// <response code="0">successful operation</response> /// <response code="0">successful operation</response>
[HttpGet] [HttpGet]
[Route("/v2/user/logout")] [Route("/v2/user/logout")]

View File

@ -32,15 +32,15 @@ namespace Org.OpenAPITools.Filters
/// <param name="context">FilterContext</param> /// <param name="context">FilterContext</param>
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context) public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{ {
swaggerDoc.BasePath = this.BasePath; swaggerDoc.BasePath = BasePath;
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(this.BasePath)).ToList(); var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(BasePath)).ToList();
foreach (var path in pathsToModify) foreach (var path in pathsToModify)
{ {
if (path.Key.StartsWith(this.BasePath)) if (path.Key.StartsWith(BasePath))
{ {
string newKey = Regex.Replace(path.Key, $"^{this.BasePath}", string.Empty); string newKey = Regex.Replace(path.Key, $"^{BasePath}", string.Empty);
swaggerDoc.Paths.Remove(path.Key); swaggerDoc.Paths.Remove(path.Key);
swaggerDoc.Paths.Add(newKey, path.Value); swaggerDoc.Paths.Add(newKey, path.Value);
} }

View File

@ -10,11 +10,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -10,11 +10,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -10,11 +10,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -10,11 +10,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -10,11 +10,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -10,11 +10,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.IO;
using System.Text; using System.Text;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Newtonsoft.Json; using Newtonsoft.Json;

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore; using Microsoft.AspNetCore;
namespace Org.OpenAPITools namespace Org.OpenAPITools

View File

@ -14,7 +14,6 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.Swagger;
@ -29,8 +28,7 @@ namespace Org.OpenAPITools
public class Startup public class Startup
{ {
private readonly IHostingEnvironment _hostingEnv; private readonly IHostingEnvironment _hostingEnv;
private readonly IConfiguration _configuration;
private IConfiguration Configuration { get; }
/// <summary> /// <summary>
/// Constructor /// Constructor
@ -40,7 +38,7 @@ namespace Org.OpenAPITools
public Startup(IHostingEnvironment env, IConfiguration configuration) public Startup(IHostingEnvironment env, IConfiguration configuration)
{ {
_hostingEnv = env; _hostingEnv = env;
Configuration = configuration; _configuration = configuration;
} }
/// <summary> /// <summary>
@ -55,7 +53,8 @@ namespace Org.OpenAPITools
.AddJsonOptions(opts => .AddJsonOptions(opts =>
{ {
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
opts.SerializerSettings.Converters.Add(new StringEnumConverter { opts.SerializerSettings.Converters.Add(new StringEnumConverter
{
CamelCaseText = true CamelCaseText = true
}); });
}); });
@ -92,9 +91,7 @@ namespace Org.OpenAPITools
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary> /// </summary>
/// <param name="app"></param> /// <param name="app"></param>
/// <param name="env"></param> public void Configure(IApplicationBuilder app)
/// <param name="loggerFactory"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{ {
app app
.UseMvc() .UseMvc()
@ -113,7 +110,7 @@ namespace Org.OpenAPITools
// c.SwaggerEndpoint("/openapi-original.json", "OpenAPI Petstore Original"); // c.SwaggerEndpoint("/openapi-original.json", "OpenAPI Petstore Original");
}); });
if (env.IsDevelopment()) if (_hostingEnv.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }