diff --git a/Nancy.Metadata.Swagger.DemoApplication/Nancy.Metadata.Swagger.DemoApplication.csproj b/Nancy.Metadata.Swagger.DemoApplication/Nancy.Metadata.Swagger.DemoApplication.csproj index 08be2e0..671c716 100644 --- a/Nancy.Metadata.Swagger.DemoApplication/Nancy.Metadata.Swagger.DemoApplication.csproj +++ b/Nancy.Metadata.Swagger.DemoApplication/Nancy.Metadata.Swagger.DemoApplication.csproj @@ -38,24 +38,24 @@ - - ..\packages\Nancy.1.2.0\lib\net40\Nancy.dll + + ..\packages\Nancy.1.4.3\lib\net40\Nancy.dll True - - ..\packages\Nancy.Hosting.Self.1.2.0\lib\net40\Nancy.Hosting.Self.dll + + ..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll True - - ..\packages\Nancy.Metadata.Modules.1.2.0\lib\net40\Nancy.Metadata.Modules.dll + + ..\packages\Nancy.Metadata.Modules.1.4.1\lib\net40\Nancy.Metadata.Modules.dll True - - ..\packages\Nancy.Serialization.JsonNet.1.2.0\lib\net40\Nancy.Serialization.JsonNet.dll + + ..\packages\Nancy.Serialization.JsonNet.1.4.1\lib\net40\Nancy.Serialization.JsonNet.dll True - - ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll True diff --git a/Nancy.Metadata.Swagger.DemoApplication/app.config b/Nancy.Metadata.Swagger.DemoApplication/app.config index 5316136..c593f53 100644 --- a/Nancy.Metadata.Swagger.DemoApplication/app.config +++ b/Nancy.Metadata.Swagger.DemoApplication/app.config @@ -5,7 +5,7 @@ - + diff --git a/Nancy.Metadata.Swagger.DemoApplication/packages.config b/Nancy.Metadata.Swagger.DemoApplication/packages.config index c253cb1..417cd16 100644 --- a/Nancy.Metadata.Swagger.DemoApplication/packages.config +++ b/Nancy.Metadata.Swagger.DemoApplication/packages.config @@ -1,8 +1,8 @@  - - - - - + + + + + \ No newline at end of file diff --git a/Nancy.Metadata.Swagger/Core/CustomJsonConverter.cs b/Nancy.Metadata.Swagger/Core/CustomJsonConverter.cs new file mode 100644 index 0000000..9fa74a8 --- /dev/null +++ b/Nancy.Metadata.Swagger/Core/CustomJsonConverter.cs @@ -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); + } + + 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)) + { + 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); + } + } +} diff --git a/Nancy.Metadata.Swagger/Core/LicenseInstsaller.cs b/Nancy.Metadata.Swagger/Core/LicenseInstsaller.cs deleted file mode 100644 index 04e7620..0000000 --- a/Nancy.Metadata.Swagger/Core/LicenseInstsaller.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Newtonsoft.Json.Schema; - -namespace Nancy.Metadata.Swagger.Core -{ - public class LicenseInstsaller - { - public static void SetJsonSchemaLicense(string licenseKey) - { - License.RegisterLicense(licenseKey); - } - } -} \ No newline at end of file diff --git a/Nancy.Metadata.Swagger/Core/SchemaCache.cs b/Nancy.Metadata.Swagger/Core/SchemaCache.cs index 4dc6aa8..12f6ee8 100644 --- a/Nancy.Metadata.Swagger/Core/SchemaCache.cs +++ b/Nancy.Metadata.Swagger/Core/SchemaCache.cs @@ -5,6 +5,6 @@ namespace Nancy.Metadata.Swagger.Core { public static class SchemaCache { - public static Dictionary Cache = new Dictionary(); + public static Dictionary Cache = new Dictionary(); } } \ No newline at end of file diff --git a/Nancy.Metadata.Swagger/Core/TypeNameGenerator.cs b/Nancy.Metadata.Swagger/Core/TypeNameGenerator.cs new file mode 100644 index 0000000..f463ebf --- /dev/null +++ b/Nancy.Metadata.Swagger/Core/TypeNameGenerator.cs @@ -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 reservedTypeNames) + { + return typeNameHint; + } + } +} diff --git a/Nancy.Metadata.Swagger/Fluent/SwaggerEndpointInfoExtensions.cs b/Nancy.Metadata.Swagger/Fluent/SwaggerEndpointInfoExtensions.cs index 945136b..23415ea 100644 --- a/Nancy.Metadata.Swagger/Fluent/SwaggerEndpointInfoExtensions.cs +++ b/Nancy.Metadata.Swagger/Fluent/SwaggerEndpointInfoExtensions.cs @@ -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 { @@ -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; } diff --git a/Nancy.Metadata.Swagger/Model/SwaggerSpecification.cs b/Nancy.Metadata.Swagger/Model/SwaggerSpecification.cs index c57ce56..1aee249 100644 --- a/Nancy.Metadata.Swagger/Model/SwaggerSpecification.cs +++ b/Nancy.Metadata.Swagger/Model/SwaggerSpecification.cs @@ -24,7 +24,7 @@ public class SwaggerSpecification [JsonProperty("paths")] public Dictionary> PathInfos { get; set; } - [JsonProperty("definitions")] - public Dictionary ModelDefinitions { get; set; } + [JsonProperty("definitions"), JsonConverter(typeof(Core.CustomJsonConverter))] + public Dictionary ModelDefinitions { get; set; } } } \ No newline at end of file diff --git a/Nancy.Metadata.Swagger/Modules/SwaggerDocsModuleBase.cs b/Nancy.Metadata.Swagger/Modules/SwaggerDocsModuleBase.cs index c173049..d3b0e6b 100644 --- a/Nancy.Metadata.Swagger/Modules/SwaggerDocsModuleBase.cs +++ b/Nancy.Metadata.Swagger/Modules/SwaggerDocsModuleBase.cs @@ -90,7 +90,7 @@ private void GenerateSpecification() // add definitions if (swaggerSpecification.ModelDefinitions == null) { - swaggerSpecification.ModelDefinitions = new Dictionary(); + swaggerSpecification.ModelDefinitions = new Dictionary(); } foreach (string key in SchemaCache.Cache.Keys) diff --git a/Nancy.Metadata.Swagger/Nancy.Metadata.Swagger.csproj b/Nancy.Metadata.Swagger/Nancy.Metadata.Swagger.csproj index 980e4a1..80610e6 100644 --- a/Nancy.Metadata.Swagger/Nancy.Metadata.Swagger.csproj +++ b/Nancy.Metadata.Swagger/Nancy.Metadata.Swagger.csproj @@ -35,16 +35,16 @@ false - - ..\packages\Nancy.1.2.0\lib\net40\Nancy.dll + + ..\packages\Nancy.1.4.3\lib\net40\Nancy.dll True - - ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll True - - ..\packages\Newtonsoft.Json.Schema.2.0.2\lib\net45\Newtonsoft.Json.Schema.dll + + ..\packages\NJsonSchema.9.1.6\lib\net45\NJsonSchema.dll True @@ -55,9 +55,10 @@ - + + diff --git a/Nancy.Metadata.Swagger/app.config b/Nancy.Metadata.Swagger/app.config index 9ca82da..d8020dc 100644 --- a/Nancy.Metadata.Swagger/app.config +++ b/Nancy.Metadata.Swagger/app.config @@ -4,7 +4,7 @@ - + diff --git a/Nancy.Metadata.Swagger/packages.config b/Nancy.Metadata.Swagger/packages.config index a1c8ebb..3382686 100644 --- a/Nancy.Metadata.Swagger/packages.config +++ b/Nancy.Metadata.Swagger/packages.config @@ -1,6 +1,6 @@  - - - + + + \ No newline at end of file