From fc5c1044a8ebb6a43f7b8d5e51c76153376530af Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 18 Feb 2018 12:23:24 +1300 Subject: [PATCH] -Fixed compatibility with latest Newtonsoft.Json -Removed netstandard2.0 build --- Build/build.ps1 | 2 +- .../Generation/JSchemaGeneratorInternal.cs | 14 +++++++------- .../Generation/StringEnumGenerationProvider.cs | 11 +++++------ .../Newtonsoft.Json.Schema.csproj | 10 +++++++--- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Build/build.ps1 b/Build/build.ps1 index ea348391..e8726b4e 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -33,7 +33,7 @@ $builds = @( @{Framework = "netstandard1.3"; TestsFunction = "NetCliTests"; TestFramework = "netcoreapp1.1"; Enabled=$true} - @{Framework = "netstandard2.0"; TestsFunction = "NetCliTests"; TestFramework = "netcoreapp2.0"; Enabled=$true} + #@{Framework = "netstandard2.0"; TestsFunction = "NetCliTests"; TestFramework = "netcoreapp2.0"; Enabled=$true} @{Framework = "net45"; TestsFunction = "NUnitTests"; TestFramework = "net46"; NUnitFramework="net-4.0"; Enabled=$true}, @{Framework = "net40"; TestsFunction = "NUnitTests"; NUnitFramework="net-4.0"; Enabled=$true}, @{Framework = "net35"; TestsFunction = "NUnitTests"; NUnitFramework="net-2.0"; Enabled=$true}, diff --git a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs index dc66fe6d..72abbdcf 100644 --- a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs +++ b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs @@ -537,10 +537,10 @@ private void PopulatePrimativeSchema(JSchema schema, JsonContract contract, Json schema.Enum.Add(JValue.CreateNull()); } - IList> enumValues = EnumUtils.GetNamesAndValues(contract.NonNullableUnderlyingType); - foreach (EnumValue enumValue in enumValues) + EnumInfo enumValues = EnumUtils.GetEnumValuesAndNames(contract.NonNullableUnderlyingType); + for (int i = 0; i < enumValues.Values.Length; i++) { - JToken value = JToken.FromObject(enumValue.Value); + JToken value = JToken.FromObject(Enum.ToObject(contract.NonNullableUnderlyingType, enumValues.Values[i])); schema.Enum.Add(value); } @@ -549,12 +549,12 @@ private void PopulatePrimativeSchema(JSchema schema, JsonContract contract, Json Type enumDataType = AttributeHelpers.GetEnumDataType(memberProperty); if (enumDataType != null && CollectionUtils.IsNullOrEmpty(schema._enum)) { - IList> enumValues = EnumUtils.GetNamesAndValues(enumDataType); - foreach (EnumValue enumValue in enumValues) + EnumInfo enumValues = EnumUtils.GetEnumValuesAndNames(enumDataType); + for (int i = 0; i < enumValues.Values.Length; i++) { JToken value = (JSchemaTypeHelpers.HasFlag(type, JSchemaType.String)) - ? enumValue.Name - : JToken.FromObject(enumValue.Value); + ? enumValues.ResolvedNames[i] + : JToken.FromObject(enumValues.Values[i]); schema.Enum.Add(value); } diff --git a/Src/Newtonsoft.Json.Schema/Generation/StringEnumGenerationProvider.cs b/Src/Newtonsoft.Json.Schema/Generation/StringEnumGenerationProvider.cs index 1f144e19..614a6a31 100644 --- a/Src/Newtonsoft.Json.Schema/Generation/StringEnumGenerationProvider.cs +++ b/Src/Newtonsoft.Json.Schema/Generation/StringEnumGenerationProvider.cs @@ -46,16 +46,15 @@ public override JSchema GetSchema(JSchemaTypeGenerationContext context) object defaultValue = context.MemberProperty?.DefaultValue; if (defaultValue != null) { - string finalName = EnumUtils.ToEnumName(t, defaultValue.ToString(), CamelCaseText); + EnumUtils.TryToString(t, defaultValue, CamelCaseText, out string finalName); - schema.Default = JToken.FromObject(finalName); + schema.Default = JToken.FromObject(finalName ?? defaultValue.ToString()); } - string[] names = Enum.GetNames(t); - - foreach (string name in names) + EnumInfo enumValues = EnumUtils.GetEnumValuesAndNames(t); + for (int i = 0; i < enumValues.Values.Length; i++) { - string finalName = EnumUtils.ToEnumName(t, name, CamelCaseText); + EnumUtils.TryToString(t, enumValues.Values[i], CamelCaseText, out string finalName); schema.Enum.Add(JValue.CreateString(finalName)); } diff --git a/Src/Newtonsoft.Json.Schema/Newtonsoft.Json.Schema.csproj b/Src/Newtonsoft.Json.Schema/Newtonsoft.Json.Schema.csproj index f8f43496..3dbc535b 100644 --- a/Src/Newtonsoft.Json.Schema/Newtonsoft.Json.Schema.csproj +++ b/Src/Newtonsoft.Json.Schema/Newtonsoft.Json.Schema.csproj @@ -1,6 +1,6 @@  - net45;net40;net35;netstandard1.3;netstandard2.0;portable-net45+win8+wpa81+wp8 + net45;net40;net35;netstandard1.3;portable-net45+win8+wpa81+wp8 $(LibraryFrameworks) 2.0.0.0 2.0.1 @@ -29,7 +29,7 @@ 2.12 - + @@ -45,9 +45,13 @@ Json.NET Schema .NET 3.5 NET35;$(AdditionalConstants) - + + + + + Json.NET Schema .NET Standard 1.3 NETSTANDARD1_3;PORTABLE;$(AdditionalConstants)