Skip to content

Commit

Permalink
Merge pull request #58 from NSwag/master
Browse files Browse the repository at this point in the history
Release v2.6
  • Loading branch information
RicoSuter committed Apr 12, 2016
2 parents 6e9aef1 + 593868b commit a1a26e0
Show file tree
Hide file tree
Showing 33 changed files with 899 additions and 852 deletions.
110 changes: 58 additions & 52 deletions src/NSwag.Annotations/NSwag.Annotations.csproj
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CA084154-E758-4A44-938D-7806AF2DD886}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NSwag.Annotations</RootNamespace>
<AssemblyName>NSwag.Annotations</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile328</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\NSwag.Annotations.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<None Include="NSwag.Annotations.nuspec" />
</ItemGroup>
<ItemGroup>
<Compile Include="ResponseTypeAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CA084154-E758-4A44-938D-7806AF2DD886}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NSwag.Annotations</RootNamespace>
<AssemblyName>NSwag.Annotations</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile328</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\NSwag.Annotations.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>../NSwag.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<None Include="NSwag.Annotations.nuspec" />
</ItemGroup>
<ItemGroup>
<Compile Include="ResponseTypeAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
2 changes: 1 addition & 1 deletion src/NSwag.Annotations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[assembly: AssemblyCompany("Rico Suter")]
[assembly: AssemblyProduct("NSwag.Annotations")]
[assembly: AssemblyCopyright("Copyright © Rico Suter, 2015")]
[assembly: AssemblyVersion("2.5.*")]
[assembly: AssemblyVersion("2.6.*")]
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=2.4.5940.38662, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.4.5940.38662\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=2.6.5946.13963, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.6.5946.13963\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.4.5940.38663, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.4.5940.38663\lib\net45\NJsonSchema.CodeGeneration.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.6.5946.13963, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.6.5946.13963\lib\net45\NJsonSchema.CodeGeneration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("2.5.*")]
// [assembly: AssemblyVersion("2.6.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace NSwag.CodeGeneration.Tests.WebApiToSwaggerGenerator.Attributes
{
// See https://github.com/NSwag/NSwag/issues/48

//[TestClass]
[TestClass]
public class RouteInheritanceTests
{
//[TestMethod]
public void When_route_is_on_inherited_parent_class_then_it_is_used_for_swagger_generation()
[TestMethod]
public void When_route_is_on_inherited_parent_class_and_route_prefix_on_class_then_it_is_used_for_swagger_generation()
{
//// Arrange
var generator = new SwaggerGenerators.WebApi.WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
Expand All @@ -37,5 +37,32 @@ public string Bar()
return "foo";
}
}

[TestMethod]
public void When_route_is_on_inherited_parent_class_then_it_is_used_for_swagger_generation()
{
//// Arrange
var generator = new SwaggerGenerators.WebApi.WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

//// Act
var service = generator.GenerateForController<MyController2>();

//// Assert
Assert.AreEqual("Foo", service.Operations.First().Path);
}

public class MyController2 : BaseController2
{

}

public class BaseController2 : ApiController
{
[Route("Foo")]
public string Bar()
{
return "foo";
}
}
}
}
4 changes: 2 additions & 2 deletions src/NSwag.CodeGeneration.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NJsonSchema" version="2.4.5940.38662" targetFramework="net45" />
<package id="NJsonSchema.CodeGeneration" version="2.4.5940.38663" targetFramework="net45" />
<package id="NJsonSchema" version="2.6.5946.13963" targetFramework="net45" />
<package id="NJsonSchema.CodeGeneration" version="2.6.5946.13963" targetFramework="net45" />
</packages>
12 changes: 4 additions & 8 deletions src/NSwag.CodeGeneration/NSwag.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=2.4.5940.38662, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.4.5940.38662\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=2.6.5946.13963, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.6.5946.13963\lib\portable-net45+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.4.5940.38663, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.4.5940.38663\lib\net45\NJsonSchema.CodeGeneration.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.6.5946.13963, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.6.5946.13963\lib\net45\NJsonSchema.CodeGeneration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down Expand Up @@ -80,18 +80,14 @@
<Compile Include="CodeGenerators\TypeScript\SwaggerToTypeScriptClientGenerator.cs" />
<Compile Include="Infrastructure\AppDomainIsolation.cs" />
<Compile Include="Infrastructure\AssemblyConfigurationFileTransformer.cs" />
<Compile Include="Infrastructure\ReflectionExtensions.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpWebApiControllerGenerator.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpWebApiControllerGeneratorSettings.cs" />
<Compile Include="SwaggerGenerators\AssemblyLoader.cs" />
<Compile Include="SwaggerGenerators\RootTypeJsonSchemaGenerator.cs" />
<Compile Include="SwaggerGenerators\WebApi\AssemblyTypeToSwaggerGenerator.cs" />
<Compile Include="SwaggerGenerators\WebApi\AssemblyTypeToSwaggerGeneratorSettings.cs" />
<Compile Include="SwaggerGenerators\WebApi\WebApiAssemblyToSwaggerGenerator.cs" />
<Compile Include="SwaggerGenerators\WebApi\WebApiAssemblyToSwaggerGeneratorSettings.cs" />
<Compile Include="SwaggerGenerators\WebApi\WebApiToSwaggerGenerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SwaggerGenerators\WebApi\WebApiToSwaggerGeneratorSettings.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[assembly: AssemblyCompany("Rico Suter")]
[assembly: AssemblyProduct("NSwag.CodeGeneration")]
[assembly: AssemblyCopyright("Copyright © Rico Suter, 2015")]
[assembly: AssemblyVersion("2.5.*")]
[assembly: AssemblyVersion("2.6.*")]
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal string[] GetControllerClasses(string assemblyPath, IEnumerable<string>
RegisterReferencePaths(referencePaths);
var assembly = Assembly.LoadFrom(assemblyPath);
return assembly.ExportedTypes
.Where(t => t.InheritsFrom("ApiController") || t.InheritsFrom("Controller"))
.Where(t => t.Name.EndsWith("Controller") || t.InheritsFrom("ApiController") || t.InheritsFrom("Controller"))
.Select(t => t.FullName)
.ToArray();
}
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.CodeGeneration/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<packages>
<package id="Antlr4.StringTemplate" version="4.0.6.9004" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NJsonSchema" version="2.4.5940.38662" targetFramework="net45" />
<package id="NJsonSchema.CodeGeneration" version="2.4.5940.38663" targetFramework="net45" />
<package id="NJsonSchema" version="2.6.5946.13963" targetFramework="net45" />
<package id="NJsonSchema.CodeGeneration" version="2.6.5946.13963" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
//-----------------------------------------------------------------------
// <copyright file="ReflectionExtensions.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using System;

namespace NSwag.CodeGeneration.Infrastructure
{
internal static class ReflectionExtensions
{
public static bool InheritsFrom(this Type type, string typeName)
{
var baseType = type.BaseType;
while (baseType != null)
{
if (baseType.Name == typeName)
return true;
baseType = baseType.BaseType;
}
return false;
}
}
}
//-----------------------------------------------------------------------
// <copyright file="ReflectionExtensions.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using System;
using System.Reflection;

namespace NSwag.CodeGeneration.Infrastructure
{
/// <summary>Provides extension methods for reflection.</summary>
public static class ReflectionExtensions
{
/// <summary>Checks whether the given type inherits from the given type name.</summary>
/// <param name="type">The type.</param>
/// <param name="typeName">Name of the type.</param>
/// <returns>true if the type inherits from typeName.</returns>
public static bool InheritsFrom(this Type type, string typeName)
{
var baseType = type.GetTypeInfo().BaseType;
while (baseType != null)
{
if (baseType.Name == typeName)
return true;
baseType = baseType.GetTypeInfo().BaseType;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ public SwaggerService GenerateForControllers(IEnumerable<Type> controllerTypes,
/// <exception cref="InvalidOperationException">The operation has more than one body parameter.</exception>
private void GenerateForController(SwaggerService service, Type controllerType, string excludedMethodName, SchemaResolver schemaResolver)
{
var methods = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
foreach (var method in methods.Where(m => m.Name != excludedMethodName))
var methods = controllerType.GetRuntimeMethods().Where(m => m.IsPublic);
foreach (var method in methods.Where(m => m.Name != excludedMethodName &&
m.DeclaringType != null &&
m.DeclaringType != typeof(object) &&
m.DeclaringType.FullName != "System.Web.Http.ApiController" &&
m.DeclaringType.FullName != "Microsoft.AspNet.Mvc.Controller" && // .NET Core (Web API & MVC)
m.DeclaringType.FullName != "System.Web.Mvc.Controller"))
{
var parameters = method.GetParameters().ToList();
var methodName = method.Name;
Expand Down Expand Up @@ -160,7 +165,7 @@ private string GetHttpPath(SwaggerService service, SwaggerOperation operation, T

if (routeAttribute != null)
{
dynamic routePrefixAttribute = controllerType.GetCustomAttributes()
dynamic routePrefixAttribute = controllerType.GetTypeInfo().GetCustomAttributes()
.SingleOrDefault(a => a.GetType().Name == "RoutePrefixAttribute");

if (routePrefixAttribute != null)
Expand Down Expand Up @@ -378,7 +383,7 @@ private bool IsParameterRequired(ParameterInfo parameter)
if (isNullable)
return false;

return parameter.ParameterType.IsValueType;
return parameter.ParameterType.GetTypeInfo().IsValueType;
}

private SwaggerParameter CreatePrimitiveParameter(SwaggerService service, string name, string description,
Expand All @@ -393,7 +398,7 @@ private SwaggerParameter CreatePrimitiveParameter(SwaggerService service, string
var operationParameter = new SwaggerParameter();
typeDescription.ApplyType(operationParameter);

if (parameterType.IsEnum)
if (parameterType.GetTypeInfo().IsEnum)
operationParameter.SchemaReference = schemaGenerator.Generate<JsonSchema4>(parameterType, null, parentAttributes, schemaDefinitionAppender, schemaResolver);
else
schemaGenerator.ApplyPropertyAnnotations(operationParameter, parentAttributes, typeDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace NSwag.CodeGeneration.SwaggerGenerators.WebApi
{
/// <summary>Settings for the <see cref="WebApiAssemblyToSwaggerGenerator"/>.</summary>
/// <summary>Settings for the <see cref="WebApiToSwaggerGenerator"/>.</summary>
public class WebApiToSwaggerGeneratorSettings : JsonSchemaGeneratorSettings
{
/// <summary>Initializes a new instance of the <see cref="WebApiToSwaggerGeneratorSettings"/> class.</summary>
Expand Down
Loading

0 comments on commit a1a26e0

Please sign in to comment.