Skip to content

Commit

Permalink
Updated NJS, added OpenAPI components
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Mar 12, 2018
1 parent 3dcc7df commit ef1d3d1
Show file tree
Hide file tree
Showing 36 changed files with 313 additions and 125 deletions.
2 changes: 1 addition & 1 deletion src/NSwag.AspNet.Owin/NSwag.AspNet.Owin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Owin" Version="3.0.1" />
<PackageReference Include="Microsoft.Owin.StaticFiles" Version="3.0.1" />
<PackageReference Include="NJsonSchema" Version="9.10.30" />
<PackageReference Include="NJsonSchema" Version="9.10.31" />
<PackageReference Include="Owin" Version="1.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AspNet.WebApi/NSwag.AspNet.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="9.10.30" />
<PackageReference Include="NJsonSchema" Version="9.10.31" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.3" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<DefineConstants>TRACE;RELEASE;FullNet;NET451</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="9.10.30" />
<PackageReference Include="NJsonSchema" Version="9.10.31" />
<PackageReference Include="System.Net.Http" Version="4.3.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.30\lib\net45\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.31\lib\net45\NJsonSchema.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.CSharp.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net462" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="NJsonSchema" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema" version="9.10.31" targetFramework="net462" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<EmbeddedResource Include="Templates\JsonExceptionConverter.liquid" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.10.30" />
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.10.31" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net451'">
<Reference Include="Microsoft.CSharp" />
Expand Down
16 changes: 8 additions & 8 deletions src/NSwag.CodeGeneration.Tests/NSwag.CodeGeneration.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.30\lib\net45\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.31\lib\net45\NJsonSchema.dll</HintPath>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.9.10.30\lib\net451\NJsonSchema.CodeGeneration.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.9.10.31\lib\net451\NJsonSchema.CodeGeneration.dll</HintPath>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration.CSharp, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.CSharp.9.10.30\lib\net451\NJsonSchema.CodeGeneration.CSharp.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration.CSharp, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.CSharp.9.10.31\lib\net451\NJsonSchema.CodeGeneration.CSharp.dll</HintPath>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration.TypeScript, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.TypeScript.9.10.30\lib\net451\NJsonSchema.CodeGeneration.TypeScript.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration.TypeScript, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.TypeScript.9.10.31\lib\net451\NJsonSchema.CodeGeneration.TypeScript.dll</HintPath>
</Reference>
<Reference Include="Swashbuckle.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd1bb07a5ac7c7bc, processorArchitecture=MSIL">
<HintPath>..\packages\Swashbuckle.Core.5.5.3\lib\net40\Swashbuckle.Core.dll</HintPath>
Expand Down
8 changes: 4 additions & 4 deletions src/NSwag.CodeGeneration.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net46" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="NJsonSchema" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration.CSharp" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration.TypeScript" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema" version="9.10.31" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration" version="9.10.31" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration.CSharp" version="9.10.31" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration.TypeScript" version="9.10.31" targetFramework="net462" />
<package id="Swashbuckle.Core" version="5.5.3" targetFramework="net45" />
<package id="System.AppContext" version="4.3.0" targetFramework="net46" />
<package id="System.Collections" version="4.3.0" targetFramework="net46" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.30\lib\net45\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.31\lib\net45\NJsonSchema.dll</HintPath>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.9.10.30\lib\net451\NJsonSchema.CodeGeneration.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.9.10.31\lib\net451\NJsonSchema.CodeGeneration.dll</HintPath>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration.TypeScript, Version=9.10.30.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.TypeScript.9.10.30\lib\net451\NJsonSchema.CodeGeneration.TypeScript.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration.TypeScript, Version=9.10.31.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.TypeScript.9.10.31\lib\net451\NJsonSchema.CodeGeneration.TypeScript.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
Expand Down
6 changes: 3 additions & 3 deletions src/NSwag.CodeGeneration.TypeScript.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net46" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="NJsonSchema" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration.TypeScript" version="9.10.30" targetFramework="net462" />
<package id="NJsonSchema" version="9.10.31" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration" version="9.10.31" targetFramework="net462" />
<package id="NJsonSchema.CodeGeneration.TypeScript" version="9.10.31" targetFramework="net462" />
<package id="System.AppContext" version="4.3.0" targetFramework="net46" />
<package id="System.Collections" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net46" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<EmbeddedResource Include="Templates\JQueryPromisesClient.liquid" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.TypeScript" Version="9.10.30" />
<PackageReference Include="NJsonSchema.CodeGeneration.TypeScript" Version="9.10.31" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net451'">
<Reference Include="Microsoft.CSharp" />
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.CodeGeneration/NSwag.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="NJsonSchema" Version="9.10.30" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="9.10.30" />
<PackageReference Include="NJsonSchema" Version="9.10.31" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="9.10.31" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net451'">
<Reference Include="Microsoft.CSharp" />
Expand Down
8 changes: 4 additions & 4 deletions src/NSwag.Commands/NSwag.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="NConsole" Version="3.9.6519.30868" />
<PackageReference Include="NJsonSchema" Version="9.10.30" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="9.10.30" />
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.10.30" />
<PackageReference Include="NJsonSchema.CodeGeneration.TypeScript" Version="9.10.30" />
<PackageReference Include="NJsonSchema" Version="9.10.31" />
<PackageReference Include="NJsonSchema.CodeGeneration" Version="9.10.31" />
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.10.31" />
<PackageReference Include="NJsonSchema.CodeGeneration.TypeScript" Version="9.10.31" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net451'">
<Reference Include="Microsoft.CSharp" />
Expand Down
67 changes: 67 additions & 0 deletions src/NSwag.Core.Tests/ComponentsSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Threading.Tasks;
using NJsonSchema;
using Xunit;

namespace NSwag.Core.Tests
{
public class ComponentsSerializationTests
{
[Fact]
public async Task When_schema_is_added_to_definitions_then_it_is_serialized_correctly_in_Swagger()
{
//// Arrange
var document = CreateDocument();

//// Act
var json = document.ToJson(SchemaType.Swagger2);
document = await SwaggerDocument.FromJsonAsync(json, schemaType: SchemaType.OpenApi3);

//// Assert
Assert.Contains("definitions", json);
Assert.DoesNotContain("components", json);

Assert.True(document.Definitions.ContainsKey("Foo"));
}

[Fact]
public async Task When_schema_is_added_to_definitions_then_it_is_serialized_correctly_in_OpenApi()
{
//// Arrange
var document = CreateDocument();

//// Act
var json = document.ToJson(SchemaType.OpenApi3);
document = await SwaggerDocument.FromJsonAsync(json, schemaType: SchemaType.OpenApi3);

//// Assert
Assert.Contains("components", json);
Assert.Contains("schemas", json);
Assert.DoesNotContain("#/definitions/Foo", json);
Assert.DoesNotContain("definitions", json);

Assert.True(document.Definitions.ContainsKey("Foo"));
}

private static SwaggerDocument CreateDocument()
{
var schema = new JsonSchema4
{
Type = JsonObjectType.String
};

var document = new SwaggerDocument();
document.Definitions["Foo"] = schema;
document.Definitions["Bar"] = new JsonSchema4
{
Type = JsonObjectType.Object,
Properties =
{
{ "Foo", new JsonProperty { Reference = schema } }
}
};

return document;
}
}
}
20 changes: 20 additions & 0 deletions src/NSwag.Core.Tests/NSwag.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NSwag.Core\NSwag.Core.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/NSwag.Core/NSwag.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="NJsonSchema" Version="9.10.30" />
<PackageReference Include="NJsonSchema" Version="9.10.31" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Reference Include="Microsoft.CSharp" />
Expand Down
58 changes: 58 additions & 0 deletions src/NSwag.Core/SwaggerComponents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using NJsonSchema;
using NSwag.Collections;

namespace NSwag
{
/// <summary>Container for reusable components (OpenAPI only).</summary>
public class SwaggerComponents
{
/// <summary></summary>
/// <param name="document"></param>
public SwaggerComponents(SwaggerDocument document)
{
SecuritySchemes = new Dictionary<string, SwaggerSecurityScheme>();

var definitions = new ObservableDictionary<string, JsonSchema4>();
definitions.CollectionChanged += (sender, args) =>
{
foreach (var path in Schemas.Values)
path.Parent = document;
};
Schemas = definitions;

var parameters = new ObservableDictionary<string, SwaggerParameter>();
parameters.CollectionChanged += (sender, args) =>
{
foreach (var path in Parameters.Values)
path.Parent = document;
};
Parameters = parameters;

var responses = new ObservableDictionary<string, SwaggerResponse>();
responses.CollectionChanged += (sender, args) =>
{
foreach (var path in Responses.Values)
path.Parent = document;
};
Responses = responses;
}

/// <summary>Gets or sets the types.</summary>
[JsonProperty(PropertyName = "schemas", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, JsonSchema4> Schemas { get; }

/// <summary>Gets or sets the parameters which can be used for all operations.</summary>
[JsonProperty(PropertyName = "parameters", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, SwaggerParameter> Parameters { get; }

/// <summary>Gets or sets the responses which can be used for all operations.</summary>
[JsonProperty(PropertyName = "responses", DefaultValueHandling = DefaultValueHandling.Ignore)]
public IDictionary<string, SwaggerResponse> Responses { get; }

/// <summary>Gets or sets the security definitions.</summary>
[JsonProperty(PropertyName = "securitySchemes", DefaultValueHandling = DefaultValueHandling.Ignore)]
public Dictionary<string, SwaggerSecurityScheme> SecuritySchemes { get; }
}
}
35 changes: 35 additions & 0 deletions src/NSwag.Core/SwaggerDocument.Serialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using NJsonSchema;
using NJsonSchema.Infrastructure;

namespace NSwag
{
public partial class SwaggerDocument
{
/// <summary>Creates the serializer contract resolver based on the <see cref="SchemaType"/>.</summary>
/// <param name="schemaType">The schema type.</param>
/// <returns>The settings.</returns>
public static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSerializerContractResolver(SchemaType schemaType)
{
var resolver = JsonSchema4.CreateJsonSerializerContractResolver(schemaType);

if (schemaType == SchemaType.OpenApi3)
{
resolver.IgnoreProperty(typeof(SwaggerDocument), "swagger");

resolver.IgnoreProperty(typeof(SwaggerDocument), "definitions");
resolver.IgnoreProperty(typeof(SwaggerDocument), "parameters");
resolver.IgnoreProperty(typeof(SwaggerDocument), "responses");
resolver.IgnoreProperty(typeof(SwaggerDocument), "securityDefinitions");
}
else if (schemaType == SchemaType.Swagger2)
{
resolver.IgnoreProperty(typeof(SwaggerDocument), "openapi");
resolver.IgnoreProperty(typeof(SwaggerParameter), "title");

resolver.IgnoreProperty(typeof(SwaggerDocument), "components");
}

return resolver;
}
}
}
Loading

0 comments on commit ef1d3d1

Please sign in to comment.