Skip to content
Open
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
Expand Up @@ -38,24 +38,24 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Nancy, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.1.2.0\lib\net40\Nancy.dll</HintPath>
<Reference Include="Nancy, Version=1.4.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.1.4.3\lib\net40\Nancy.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Hosting.Self, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Hosting.Self.1.2.0\lib\net40\Nancy.Hosting.Self.dll</HintPath>
<Reference Include="Nancy.Hosting.Self, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Metadata.Modules, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Metadata.Modules.1.2.0\lib\net40\Nancy.Metadata.Modules.dll</HintPath>
<Reference Include="Nancy.Metadata.Modules, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Metadata.Modules.1.4.1\lib\net40\Nancy.Metadata.Modules.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Serialization.JsonNet, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Serialization.JsonNet.1.2.0\lib\net40\Nancy.Serialization.JsonNet.dll</HintPath>
<Reference Include="Nancy.Serialization.JsonNet, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Serialization.JsonNet.1.4.1\lib\net40\Nancy.Serialization.JsonNet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Nancy.Metadata.Swagger.DemoApplication/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
10 changes: 5 additions & 5 deletions Nancy.Metadata.Swagger.DemoApplication/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nancy" version="1.2.0" targetFramework="net45" />
<package id="Nancy.Hosting.Self" version="1.2.0" targetFramework="net45" />
<package id="Nancy.Metadata.Modules" version="1.2.0" targetFramework="net45" />
<package id="Nancy.Serialization.JsonNet" version="1.2.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
<package id="Nancy" version="1.4.3" targetFramework="net45" />
<package id="Nancy.Hosting.Self" version="1.4.1" targetFramework="net45" />
<package id="Nancy.Metadata.Modules" version="1.4.1" targetFramework="net45" />
<package id="Nancy.Serialization.JsonNet" version="1.4.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
</packages>
55 changes: 55 additions & 0 deletions Nancy.Metadata.Swagger/Core/CustomJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nancy.Metadata.Swagger.Core
{
public class CustomJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Dictionary<string, NJsonSchema.JsonSchema4>);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JObject j = new JObject();

// Rather crude hack to have all necessary type definitions on one level.
// The good thing is that it's save: as we use Type.FullName as
// schema type name, there shouldn't be any conflicts, if two requests have
// same submodel of Namespace.SpecificType type, they are guaranteed to be the
// same type
foreach (var pair in (value as Dictionary<string, NJsonSchema.JsonSchema4>))
{
var el = JObject.Parse(pair.Value.ToJson());

var defs = el.GetValue("definitions");

if (defs != null)
{
foreach (JProperty content in el.GetValue("definitions"))
{
j.Remove(content.Name);
j.Add(content.Name, content.Value);
}
}

el.Remove("definitions");

j.Add(pair.Key, el);
}

j.WriteTo(writer);
}
}
}
12 changes: 0 additions & 12 deletions Nancy.Metadata.Swagger/Core/LicenseInstsaller.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Nancy.Metadata.Swagger/Core/SchemaCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Nancy.Metadata.Swagger.Core
{
public static class SchemaCache
{
public static Dictionary<string, JSchema> Cache = new Dictionary<string, JSchema>();
public static Dictionary<string, NJsonSchema.JsonSchema4> Cache = new Dictionary<string, NJsonSchema.JsonSchema4>();
}
}
27 changes: 27 additions & 0 deletions Nancy.Metadata.Swagger/Core/TypeNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NJsonSchema;

namespace Nancy.Metadata.Swagger.Core
{
public class TypeNameGenerator : NJsonSchema.ITypeNameGenerator, NJsonSchema.ISchemaNameGenerator
{
public string Generate(Type type)
{
return type.FullName;
}

public string Generate(NJsonSchema.JsonSchema4 schema, string typeNameHint)
{
return typeNameHint;
}

public string Generate(JsonSchema4 schema, string typeNameHint, ICollection<string> reservedTypeNames)
{
return typeNameHint;
}
}
}
25 changes: 10 additions & 15 deletions Nancy.Metadata.Swagger/Fluent/SwaggerEndpointInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Nancy.Metadata.Swagger.Core;
using Nancy.Metadata.Swagger.Model;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;

namespace Nancy.Metadata.Swagger.Fluent
{
Expand Down Expand Up @@ -134,22 +133,18 @@ private static string GetOrSaveSchemaReference(Type type)
{
return key;
}

var task = NJsonSchema.JsonSchema4.FromTypeAsync(type, new NJsonSchema.Generation.JsonSchemaGeneratorSettings{
NullHandling = NJsonSchema.NullHandling.Swagger,
TypeNameGenerator = new TypeNameGenerator(),
SchemaNameGenerator = new TypeNameGenerator()
});

JSchemaGenerator generator = new JSchemaGenerator
{
SchemaIdGenerationHandling = SchemaIdGenerationHandling.FullTypeName,
SchemaReferenceHandling = SchemaReferenceHandling.None
};

JSchema schema = generator.Generate(type);

// I didn't find the way how to disallow JSchemaGenerator to use nullable types, swagger doesn't work with them

string tmp = schema.ToString();
string s = @"\""type\"":[\s\n\r]*\[[\s\n\r]*\""(\w+)\"",[\s\n\r]*\""null\""[\s\n\r]*\]";
tmp = Regex.Replace(tmp, s, "\"type\": \"$1\"");
task.Wait();

SchemaCache.Cache[key] = JSchema.Parse(tmp);
var schema = task.Result;

SchemaCache.Cache[key] = schema;

return key;
}
Expand Down
4 changes: 2 additions & 2 deletions Nancy.Metadata.Swagger/Model/SwaggerSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SwaggerSpecification
[JsonProperty("paths")]
public Dictionary<string, Dictionary<string, SwaggerEndpointInfo>> PathInfos { get; set; }

[JsonProperty("definitions")]
public Dictionary<string, JSchema> ModelDefinitions { get; set; }
[JsonProperty("definitions"), JsonConverter(typeof(Core.CustomJsonConverter))]
public Dictionary<string, NJsonSchema.JsonSchema4> ModelDefinitions { get; set; }
}
}
2 changes: 1 addition & 1 deletion Nancy.Metadata.Swagger/Modules/SwaggerDocsModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void GenerateSpecification()
// add definitions
if (swaggerSpecification.ModelDefinitions == null)
{
swaggerSpecification.ModelDefinitions = new Dictionary<string, JSchema>();
swaggerSpecification.ModelDefinitions = new Dictionary<string, NJsonSchema.JsonSchema4>();
}

foreach (string key in SchemaCache.Cache.Keys)
Expand Down
15 changes: 8 additions & 7 deletions Nancy.Metadata.Swagger/Nancy.Metadata.Swagger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Nancy, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.1.2.0\lib\net40\Nancy.dll</HintPath>
<Reference Include="Nancy, Version=1.4.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.1.4.3\lib\net40\Nancy.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json.Schema, Version=2.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.Schema.2.0.2\lib\net45\Newtonsoft.Json.Schema.dll</HintPath>
<Reference Include="NJsonSchema, Version=9.1.6.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.1.6\lib\net45\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -55,9 +55,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\LicenseInstsaller.cs" />
<Compile Include="Core\CustomJsonConverter.cs" />
<Compile Include="Core\SchemaCache.cs" />
<Compile Include="Core\SwaggerRouteMetadata.cs" />
<Compile Include="Core\TypeNameGenerator.cs" />
<Compile Include="Fluent\SwaggerEndpointInfoExtensions.cs" />
<Compile Include="Fluent\SwaggerRouteMetadataExtensions.cs" />
<Compile Include="Model\SwaggerApiInfo.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Nancy.Metadata.Swagger/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
6 changes: 3 additions & 3 deletions Nancy.Metadata.Swagger/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nancy" version="1.2.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json.Schema" version="2.0.2" targetFramework="net45" />
<package id="Nancy" version="1.4.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
<package id="NJsonSchema" version="9.1.6" targetFramework="net45" />
</packages>