Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated aspnetcore samples #10165

Merged
merged 1 commit into from
Apr 9, 2020
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
@@ -1 +1 @@
3.0.19-SNAPSHOT
3.0.20-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
# Visual Studio 15
VisualStudioVersion = 15.0.26114.2
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.csproj", "{9E6DDA14-EB44-4AFA-AB45-99B9F4D594DA}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.csproj", "{2E40B6D4-6E18-49F1-9AB5-09A7632EE6CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9E6DDA14-EB44-4AFA-AB45-99B9F4D594DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E6DDA14-EB44-4AFA-AB45-99B9F4D594DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E6DDA14-EB44-4AFA-AB45-99B9F4D594DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E6DDA14-EB44-4AFA-AB45-99B9F4D594DA}.Release|Any CPU.Build.0 = Release|Any CPU
{2E40B6D4-6E18-49F1-9AB5-09A7632EE6CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E40B6D4-6E18-49F1-9AB5-09A7632EE6CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E40B6D4-6E18-49F1-9AB5-09A7632EE6CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E40B6D4-6E18-49F1-9AB5-09A7632EE6CA}.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
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build-env
WORKDIR /app

ENV DOTNET_CLI_TELEMETRY_OPTOUT 1
Expand All @@ -12,7 +12,7 @@ COPY . ./
RUN dotnet publish -c Release -o out

# build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "IO.Swagger.dll"]
ENTRYPOINT ["dotnet", "IO.Swagger.dll"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.RegularExpressions;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.OpenApi.Models;

namespace IO.Swagger.Filters
{
Expand All @@ -28,11 +29,11 @@ public BasePathFilter(string basePath)
/// <summary>
/// Apply the filter
/// </summary>
/// <param name="swaggerDoc">SwaggerDocument</param>
/// <param name="swaggerDoc">OpenApiDocument</param>
/// <param name="context">FilterContext</param>
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.BasePath = this.BasePath;
swaggerDoc.Servers.Add(new OpenApiServer() { Url = this.BasePath });

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

Expand All @@ -47,4 +48,4 @@ public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Controllers;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace IO.Swagger.Filters
Expand All @@ -16,85 +16,81 @@ public class GeneratePathParamsValidationFilter : IOperationFilter
/// </summary>
/// <param name="operation">Operation</param>
/// <param name="context">OperationFilterContext</param>
public void Apply(Operation operation, OperationFilterContext context)
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var pars = context.ApiDescription.ParameterDescriptions;

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

if (par.ParameterDescriptor != null && par.ParameterDescriptor is ControllerParameterDescriptor && ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo != null)
var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes;

if (attributes != null && attributes.Count() > 0 && swaggerParam != null)
{
var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes;
// Required - [Required]
var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute));
if (requiredAttr != null)
{
swaggerParam.Required = true;
}

if (attributes != null && attributes.Count() > 0 && swaggerParam != null)
// Regex Pattern [RegularExpression]
var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute));
if (regexAttr != null)
{
// Required - [Required]
var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute));
if (requiredAttr != null)
string regex = (string)regexAttr.ConstructorArguments[0].Value;
if (swaggerParam is OpenApiParameter)
{
swaggerParam.Required = true;
((OpenApiParameter)swaggerParam).Schema.Pattern = regex;
}
}

// Regex Pattern [RegularExpression]
var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute));
if (regexAttr != null)
// String Length [StringLength]
int? minLenght = null, maxLength = null;
var stringLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(StringLengthAttribute));
if (stringLengthAttr != null)
{
if (stringLengthAttr.NamedArguments.Count == 1)
{
string regex = (string)regexAttr.ConstructorArguments[0].Value;
if (swaggerParam is NonBodyParameter)
{
((NonBodyParameter)swaggerParam).Pattern = regex;
}
minLenght = (int)stringLengthAttr.NamedArguments.Single(p => p.MemberName == "MinimumLength").TypedValue.Value;
}
maxLength = (int)stringLengthAttr.ConstructorArguments[0].Value;
}

// String Length [StringLength]
int? minLenght = null, maxLength = null;
var stringLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(StringLengthAttribute));
if (stringLengthAttr != null)
{
if (stringLengthAttr.NamedArguments.Count == 1)
{
minLenght = (int)stringLengthAttr.NamedArguments.Single(p => p.MemberName == "MinimumLength").TypedValue.Value;
}
maxLength = (int)stringLengthAttr.ConstructorArguments[0].Value;
}
var minLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MinLengthAttribute));
if (minLengthAttr != null)
{
minLenght = (int)minLengthAttr.ConstructorArguments[0].Value;
}

var minLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MinLengthAttribute));
if (minLengthAttr != null)
{
minLenght = (int)minLengthAttr.ConstructorArguments[0].Value;
}
var maxLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MaxLengthAttribute));
if (maxLengthAttr != null)
{
maxLength = (int)maxLengthAttr.ConstructorArguments[0].Value;
}

var maxLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MaxLengthAttribute));
if (maxLengthAttr != null)
{
maxLength = (int)maxLengthAttr.ConstructorArguments[0].Value;
}
if (swaggerParam is OpenApiParameter)
{
((OpenApiParameter)swaggerParam).Schema.MinLength = minLenght;
((OpenApiParameter)swaggerParam).Schema.MaxLength = maxLength;
}

if (swaggerParam is NonBodyParameter)
{
((NonBodyParameter)swaggerParam).MinLength = minLenght;
((NonBodyParameter)swaggerParam).MaxLength = maxLength;
}
// Range [Range]
var rangeAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RangeAttribute));
if (rangeAttr != null)
{
int rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
int rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;

// Range [Range]
var rangeAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RangeAttribute));
if (rangeAttr != null)
if (swaggerParam is OpenApiParameter)
{
int rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
int rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;

if (swaggerParam is NonBodyParameter)
{
((NonBodyParameter)swaggerParam).Minimum = rangeMin;
((NonBodyParameter)swaggerParam).Maximum = rangeMax;
}
((OpenApiParameter)swaggerParam).Schema.Minimum = rangeMin;
((OpenApiParameter)swaggerParam).Schema.Maximum = rangeMax;
}
}
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
<PropertyGroup>
<Description>IO.Swagger</Description>
<Copyright>IO.Swagger</Copyright>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>IO.Swagger</AssemblyName>
<PackageId>IO.Swagger</PackageId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"/>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4"/>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4"/>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc4"/>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc4" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void Main(string[] args)
/// <returns>IWebHostBuilder</returns>
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http:https://localhost:8080");
.UseStartup<Startup>();
}
}
Loading