Skip to content

Commit

Permalink
[Aspnetcore] use the term openapi (#7540)
Browse files Browse the repository at this point in the history
* use openapi in aspnetcore

* move back to other

* add back file
  • Loading branch information
wing328 committed Sep 29, 2020
1 parent 2d30714 commit f7570df
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 86 deletions.
1 change: 1 addition & 0 deletions bin/configs/other/aspnetcore-3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ templateDir: modules/openapi-generator/src/main/resources/aspnetcore/3.0
additionalProperties:
packageGuid: '{3C799344-F285-4669-8FD5-7ED9B795D5C5}'
aspnetCoreVersion: "3.1"
userSecretsGuid: "76e9e993-9159-441c-9c5b-fe95e7f4f020"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Swashbuckle.AspNetCore.SwaggerGen;
namespace {{packageName}}.Filters
{
/// <summary>
/// BasePath Document Filter sets BasePath property of Swagger and removes it from the individual URL paths
/// BasePath Document Filter sets BasePath property of OpenAPI and removes it from the individual URL paths
/// </summary>
public class BasePathFilter : IDocumentFilter
{
Expand All @@ -20,29 +20,29 @@ namespace {{packageName}}.Filters
}

/// <summary>
/// Gets the BasePath of the Swagger Doc
/// Gets the BasePath of the OpenAPI Doc
/// </summary>
/// <returns>The BasePath of the Swagger Doc</returns>
/// <returns>The BasePath of the OpenAPI Doc</returns>
public string BasePath { get; }

/// <summary>
/// Apply the filter
/// </summary>
/// <param name="swaggerDoc">OpenApiDocument</param>
/// <param name="openapiDoc">OpenApiDocument</param>
/// <param name="context">FilterContext</param>
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
public void Apply(OpenApiDocument openapiDoc, DocumentFilterContext context)
{
//swaggerDoc.BasePath = BasePath;
//openapiDoc.BasePath = BasePath;
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(BasePath)).ToList();
var pathsToModify = openapiDoc.Paths.Where(p => p.Key.StartsWith(BasePath)).ToList();
foreach (var (key, value) in pathsToModify)
{
if (key.StartsWith(BasePath))
{
var newKey = Regex.Replace(key, $"^{BasePath}", string.Empty);
swaggerDoc.Paths.Remove(key);
swaggerDoc.Paths.Add(newKey, value);
openapiDoc.Paths.Remove(key);
openapiDoc.Paths.Add(newKey, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ namespace {{packageName}}.Filters
foreach (var par in pars)
{
var swaggerParam = operation.Parameters.SingleOrDefault(p => p.Name == par.Name);
var openapiParam = operation.Parameters.SingleOrDefault(p => p.Name == par.Name);
var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes.ToList();
// See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1147
// and https://mikeralphson.github.io/openapi/2017/03/15/openapi3.0.0-rc0
// Basically OpenAPI v3 body parameters are split out into RequestBody and the properties have moved to schema
if (attributes.Any() && swaggerParam != null)
if (attributes.Any() && openapiParam != null)
{
// Required - [Required]
var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute));
if (requiredAttr != null)
{
swaggerParam.Required = true;
openapiParam.Required = true;
}

// Regex Pattern [RegularExpression]
var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute));
if (regexAttr != null)
{
var regex = (string)regexAttr.ConstructorArguments[0].Value;
swaggerParam.Schema.Pattern = regex;
openapiParam.Schema.Pattern = regex;
}

// String Length [StringLength]
Expand Down Expand Up @@ -72,12 +72,12 @@ namespace {{packageName}}.Filters

if (minLength != null)
{
swaggerParam.Schema.MinLength = minLength;
openapiParam.Schema.MinLength = minLength;
}

if (maxLength != null)
{
swaggerParam.Schema.MaxLength = maxLength;
openapiParam.Schema.MaxLength = maxLength;
}

// Range [Range]
Expand All @@ -87,8 +87,8 @@ namespace {{packageName}}.Filters
var rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
var rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;
swaggerParam.Schema.MinLength = rangeMin;
swaggerParam.Schema.MaxLength = rangeMax;
openapiParam.Schema.MinLength = rangeMin;
openapiParam.Schema.MaxLength = rangeMax;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"launchUrl": "openapi",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"OpenAPI": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"launchUrl": "openapi",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -29,9 +29,9 @@
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/openapi",
"publishAllPorts": true,
"useSSL": true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ namespace {{packageName}}
c.CustomSchemaIds(type => type.FriendlyId(true));
c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{Assembly.GetEntryAssembly().GetName().Name}.xml");
{{#basePathWithoutHost}}
// Sets the basePath property in the Swagger document generated
// Sets the basePath property in the OpenAPI document generated
c.DocumentFilter<BasePathFilter>("{{{basePathWithoutHost}}}");
{{/basePathWithoutHost}}

// Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..)
// Include DataAnnotation attributes on Controller Action parameters as OpenAPI validation rules (e.g required, pattern, ..)
// Use [ValidateModelState] on Actions to actually validate it in C# as well!
c.OperationFilter<GeneratePathParamsValidationFilter>();
});
Expand Down Expand Up @@ -141,14 +141,16 @@ namespace {{packageName}}
{{#useSwashbuckle}}
app.UseSwagger(c =>
{
c.RouteTemplate = "swagger/{documentName}/openapi.json";
c.RouteTemplate = "openapi/{documentName}/openapi.json";
})
.UseSwaggerUI(c =>
{
//TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
c.SwaggerEndpoint("/swagger/{{#version}}{{{version}}}{{/version}}{{^version}}v1{{/version}}/openapi.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}}");
// set route prefix to openapi, e.g. http://localhost:8080/openapi/index.html
c.RoutePrefix = "openapi";
//TODO: Either use the SwaggerGen generated OpenAPI contract (generated from C# classes)
c.SwaggerEndpoint("/openapi/{{#version}}{{{version}}}{{/version}}{{^version}}v1{{/version}}/openapi.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}}");
//TODO: Or alternatively use the original Swagger contract that's included in the static files
//TODO: Or alternatively use the original OpenAPI contract that's included in the static files
// c.SwaggerEndpoint("/openapi-original.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}} Original");
}){{/useSwashbuckle}};
app.UseRouting();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<meta http-equiv="refresh" content="0;URL='./swagger/'" />
<meta http-equiv="refresh" content="0;URL='./openapi/'" />
29 changes: 29 additions & 0 deletions samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Org.OpenAPITools.sln
README.md
build.bat
build.sh
src/Org.OpenAPITools/.gitignore
src/Org.OpenAPITools/Attributes/ValidateModelStateAttribute.cs
src/Org.OpenAPITools/Authentication/ApiAuthentication.cs
src/Org.OpenAPITools/Controllers/PetApi.cs
src/Org.OpenAPITools/Controllers/StoreApi.cs
src/Org.OpenAPITools/Controllers/UserApi.cs
src/Org.OpenAPITools/Converters/CustomEnumConverter.cs
src/Org.OpenAPITools/Dockerfile
src/Org.OpenAPITools/Filters/BasePathFilter.cs
src/Org.OpenAPITools/Filters/GeneratePathParamsValidationFilter.cs
src/Org.OpenAPITools/Models/ApiResponse.cs
src/Org.OpenAPITools/Models/Category.cs
src/Org.OpenAPITools/Models/Order.cs
src/Org.OpenAPITools/Models/Pet.cs
src/Org.OpenAPITools/Models/Tag.cs
src/Org.OpenAPITools/Models/User.cs
src/Org.OpenAPITools/OpenApi/TypeExtensions.cs
src/Org.OpenAPITools/Org.OpenAPITools.csproj
src/Org.OpenAPITools/Program.cs
src/Org.OpenAPITools/Properties/launchSettings.json
src/Org.OpenAPITools/Startup.cs
src/Org.OpenAPITools/appsettings.json
src/Org.OpenAPITools/wwwroot/README.md
src/Org.OpenAPITools/wwwroot/index.html
src/Org.OpenAPITools/wwwroot/openapi-original.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Org.OpenAPITools.Filters
{
/// <summary>
/// BasePath Document Filter sets BasePath property of Swagger and removes it from the individual URL paths
/// BasePath Document Filter sets BasePath property of OpenAPI and removes it from the individual URL paths
/// </summary>
public class BasePathFilter : IDocumentFilter
{
Expand All @@ -20,29 +20,29 @@ public BasePathFilter(string basePath)
}

/// <summary>
/// Gets the BasePath of the Swagger Doc
/// Gets the BasePath of the OpenAPI Doc
/// </summary>
/// <returns>The BasePath of the Swagger Doc</returns>
/// <returns>The BasePath of the OpenAPI Doc</returns>
public string BasePath { get; }

/// <summary>
/// Apply the filter
/// </summary>
/// <param name="swaggerDoc">OpenApiDocument</param>
/// <param name="openapiDoc">OpenApiDocument</param>
/// <param name="context">FilterContext</param>
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
public void Apply(OpenApiDocument openapiDoc, DocumentFilterContext context)
{
//swaggerDoc.BasePath = BasePath;
//openapiDoc.BasePath = BasePath;

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

foreach (var (key, value) in pathsToModify)
{
if (key.StartsWith(BasePath))
{
var newKey = Regex.Replace(key, $"^{BasePath}", string.Empty);
swaggerDoc.Paths.Remove(key);
swaggerDoc.Paths.Add(newKey, value);
openapiDoc.Paths.Remove(key);
openapiDoc.Paths.Add(newKey, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)

foreach (var par in pars)
{
var swaggerParam = operation.Parameters.SingleOrDefault(p => p.Name == par.Name);
var openapiParam = operation.Parameters.SingleOrDefault(p => p.Name == par.Name);

var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes.ToList();

// See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1147
// and https://mikeralphson.github.io/openapi/2017/03/15/openapi3.0.0-rc0
// Basically OpenAPI v3 body parameters are split out into RequestBody and the properties have moved to schema
if (attributes.Any() && swaggerParam != null)
if (attributes.Any() && openapiParam != null)
{
// Required - [Required]
var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute));
if (requiredAttr != null)
{
swaggerParam.Required = true;
openapiParam.Required = true;
}

// Regex Pattern [RegularExpression]
var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute));
if (regexAttr != null)
{
var regex = (string)regexAttr.ConstructorArguments[0].Value;
swaggerParam.Schema.Pattern = regex;
openapiParam.Schema.Pattern = regex;
}

// String Length [StringLength]
Expand Down Expand Up @@ -72,12 +72,12 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)

if (minLength != null)
{
swaggerParam.Schema.MinLength = minLength;
openapiParam.Schema.MinLength = minLength;
}

if (maxLength != null)
{
swaggerParam.Schema.MaxLength = maxLength;
openapiParam.Schema.MaxLength = maxLength;
}

// Range [Range]
Expand All @@ -87,8 +87,8 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
var rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
var rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;

swaggerParam.Schema.MinLength = rangeMin;
swaggerParam.Schema.MaxLength = rangeMax;
openapiParam.Schema.MinLength = rangeMin;
openapiParam.Schema.MaxLength = rangeMax;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>Org.OpenAPITools</AssemblyName>
<PackageId>Org.OpenAPITools</PackageId>
<UserSecretsId>5437bedb-0880-4b79-b60e-a06e28fd9ff2</UserSecretsId>
<UserSecretsId>76e9e993-9159-441c-9c5b-fe95e7f4f020</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"launchUrl": "openapi",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"OpenAPI": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"launchUrl": "openapi",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -29,9 +29,9 @@
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/openapi",
"publishAllPorts": true,
"useSSL": true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public void ConfigureServices(IServiceCollection services)
});
c.CustomSchemaIds(type => type.FriendlyId(true));
c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{Assembly.GetEntryAssembly().GetName().Name}.xml");
// Sets the basePath property in the Swagger document generated
// Sets the basePath property in the OpenAPI document generated
c.DocumentFilter<BasePathFilter>("/v2");
// Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..)
// Include DataAnnotation attributes on Controller Action parameters as OpenAPI validation rules (e.g required, pattern, ..)
// Use [ValidateModelState] on Actions to actually validate it in C# as well!
c.OperationFilter<GeneratePathParamsValidationFilter>();
});
Expand Down Expand Up @@ -129,14 +129,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseStaticFiles();
app.UseSwagger(c =>
{
c.RouteTemplate = "swagger/{documentName}/openapi.json";
c.RouteTemplate = "openapi/{documentName}/openapi.json";
})
.UseSwaggerUI(c =>
{
//TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes)
c.SwaggerEndpoint("/swagger/1.0.0/openapi.json", "OpenAPI Petstore");
// set route prefix to openapi, e.g. http://localhost:8080/openapi/index.html
c.RoutePrefix = "openapi";
//TODO: Either use the SwaggerGen generated OpenAPI contract (generated from C# classes)
c.SwaggerEndpoint("/openapi/1.0.0/openapi.json", "OpenAPI Petstore");
//TODO: Or alternatively use the original Swagger contract that's included in the static files
//TODO: Or alternatively use the original OpenAPI contract that's included in the static files
// c.SwaggerEndpoint("/openapi-original.json", "OpenAPI Petstore Original");
});
app.UseRouting();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<meta http-equiv="refresh" content="0;URL='./swagger/'" />
<meta http-equiv="refresh" content="0;URL='./openapi/'" />
Loading

0 comments on commit f7570df

Please sign in to comment.