Skip to content

Commit

Permalink
Merge pull request #18 from LykkeCity/respect_required_attribute
Browse files Browse the repository at this point in the history
Preserve information about required attributes
  • Loading branch information
Mikhail Gerasimov committed Jun 18, 2018
2 parents 4113b7c + 9709e5a commit 1cecc1d
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 26 deletions.
53 changes: 31 additions & 22 deletions Lykke.Common.ApiLibrary.sln
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lykke.Common.ApiLibrary", "src\Lykke.Common.ApiLibrary\Lykke.Common.ApiLibrary.csproj", "{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lykke.Common.ApiLibrary", "src\Lykke.Common.ApiLibrary\Lykke.Common.ApiLibrary.csproj", "{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWebService", "test\TestWebService\TestWebService.csproj", "{27EA72E9-6B13-4E7E-BB43-678C09103334}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DF2AA57-4EE8-4E83-A2E1-420ECCEA8999}.Release|Any CPU.Build.0 = Release|Any CPU
{27EA72E9-6B13-4E7E-BB43-678C09103334}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{27EA72E9-6B13-4E7E-BB43-678C09103334}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27EA72E9-6B13-4E7E-BB43-678C09103334}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27EA72E9-6B13-4E7E-BB43-678C09103334}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {62E0D70F-9328-42B0-BB4F-FC9C4864AB10}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Lykke.Common.ApiLibrary/Lykke.Common.ApiLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.1.0" />
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.5.0" PrivateAssets="All" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
<PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.2" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Label="dotnet pack instructions">
<Content Include="$(OutputPath)$(AssemblyName).pdb">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Lykke.Common.ApiLibrary.Swagger.XmsEnum
{
public class ResponseValueTypesRequiredSchemaFilter : ISchemaFilter
/// <summary>
/// Warning. It affects not only responses but also requests
/// </summary>
[UsedImplicitly]
internal class ResponseValueTypesRequiredSchemaFilter : ISchemaFilter
{
public void Apply(Schema schema, SchemaFilterContext context)
{
Expand All @@ -26,7 +31,7 @@ public void Apply(Schema schema, SchemaFilterContext context)
.Select(p => p.Name);

schema.Required = schema.Properties.Keys
.Intersect(nonNulableValueTypedPropNames, StringComparer.OrdinalIgnoreCase)
.Union(nonNulableValueTypedPropNames, StringComparer.OrdinalIgnoreCase)
.ToList();

if (!schema.Required.Any())
Expand Down
23 changes: 23 additions & 0 deletions test/TestWebService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;

namespace TestWebService
{
internal static class Program
{
static Task Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();

return host.RunAsync();
}
}
}
89 changes: 89 additions & 0 deletions test/TestWebService/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Common.Log;
using Lykke.Common.ApiLibrary.Middleware;
using Lykke.Common.ApiLibrary.Swagger;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger;

namespace TestWebService
{
public class Startup
{

public IContainer ApplicationContainer { get; private set; }
public IConfigurationRoot Configuration { get; }
public ILog Log { get; private set; }

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddEnvironmentVariables();
Configuration = builder.Build();

}

public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new DefaultContractResolver();
});

services.AddSwaggerGen(options =>
{
options.DefaultLykkeConfiguration("v1", "ClientAccountRecovery API");
// options.SwaggerDoc(
// $"v1",
// new Info
// {
// Version = "v1",
// Title = "App title"
// });
});
services.Configure<MvcJsonOptions>(c =>
{
// Serialize all properties to camelCase by default
c.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});


var builder = new ContainerBuilder();
builder.Populate(services);
ApplicationContainer = builder.Build();

return new AutofacServiceProvider(ApplicationContainer);

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
});
app.UseSwaggerUI(x =>
{
x.RoutePrefix = "swagger/ui";
x.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});
app.UseStaticFiles();
}
}
}
80 changes: 80 additions & 0 deletions test/TestWebService/TestController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace TestWebService
{
[Route("api/[controller]")]
[Produces("application/json")]
[Consumes("application/json")]
public class TestController : Controller
{
[HttpPost("one")]
[SwaggerOperation("NonNullableEnum")]
[ProducesResponseType(typeof(ResponseModel), 200)]
public IActionResult One([FromBody] RequestModel parameter)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok(new ResponseModel
{
NonNullable = parameter.NonNullable,
Nullable = parameter.Nullable,
SomeDouble = parameter.SomeDouble,
SomeInt = parameter.SomeInt,
SomeString = parameter.SomeString
});
}

}

[BindRequired]
public class RequestModel
{
[Required]
public Enum NonNullable { get; set; }

[Required]
public Enum? Nullable { get; set; }

[Required]
public string SomeString { get; set; }

[Required]
public int SomeInt { get; set; }

[Required]
public double SomeDouble { get; set; }


}

public class ResponseModel
{
[Required]
public Enum NonNullable { get; set; }

[Required]
public Enum? Nullable { get; set; }

[Required]
public string SomeString { get; set; }

[Required]
public int SomeInt { get; set; }

[Required]
public double SomeDouble { get; set; }


}
public enum Enum
{
One,
Two,
Three
}
}
21 changes: 21 additions & 0 deletions test/TestWebService/TestWebService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Lykke.Common.ApiLibrary\Lykke.Common.ApiLibrary.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions test/TestWebService/TestWebService.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp71</s:String></wpf:ResourceDictionary>

0 comments on commit 1cecc1d

Please sign in to comment.