forked from loafle/openapi-generator-original
[csharp] Update JsonSubTypes to 1.1.3 and use nuget dependency (#6969)
* the result I want to obtain * add csharp-petstore-net-40.bat for windows * just ran bin\windows\csharp-petstore-all.bat * Removed those directories: - samples\client\petstore\csharp - samples\client\petstore\csharp-dotnet2 then ran : bin\windows\csharp-petstore-all.bat * - update JsonSubTypes to 1.1.3 by using nuget dependency (the package is compatible with net40) - allign all version of Newtonsoft.Json to 10.0.3 * the result of bin\windows\csharp-petstore-all.bat * ran bin\security\windows\csharp-petstore.bat
This commit is contained in:
@@ -359,8 +359,6 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
|
||||
clientPackageDir, "ExceptionFactory.cs"));
|
||||
supportingFiles.add(new SupportingFile("SwaggerDateConverter.mustache",
|
||||
clientPackageDir, "SwaggerDateConverter.cs"));
|
||||
supportingFiles.add(new SupportingFile("JsonSubTypes.mustache",
|
||||
clientPackageDir, "JsonSubTypes.cs"));
|
||||
|
||||
if (NET40.equals(this.targetFramework)) {
|
||||
// .net 4.0 doesn't include ReadOnlyDictionary…
|
||||
|
||||
@@ -22,7 +22,8 @@
|
||||
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
|
||||
"Microsoft.EntityFrameworkCore": "1.0.0",
|
||||
"Swashbuckle.AspNetCore": "1.0.0-rc3",
|
||||
"Newtonsoft.Json": "9.0.1"
|
||||
"Newtonsoft.Json": "10.0.3",
|
||||
"JsonSubTypes": "1.1.3"
|
||||
},
|
||||
|
||||
"tools": {
|
||||
|
||||
@@ -1,272 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace JsonSubTypes
|
||||
{
|
||||
// Copied from project https://github.com/manuc66/JsonSubTypes
|
||||
// https://raw.githubusercontent.com/manuc66/JsonSubTypes/07403192ea3f4959f6d42f5966ac56ceb0d6095b/JsonSubTypes/JsonSubtypes.cs
|
||||
|
||||
public class JsonSubtypes : JsonConverter
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
|
||||
public class KnownSubTypeAttribute : Attribute
|
||||
{
|
||||
public Type SubType { get; private set; }
|
||||
public object AssociatedValue { get; private set; }
|
||||
|
||||
public KnownSubTypeAttribute(Type subType, object associatedValue)
|
||||
{
|
||||
SubType = subType;
|
||||
AssociatedValue = associatedValue;
|
||||
}
|
||||
}
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
|
||||
public class KnownSubTypeWithPropertyAttribute : Attribute
|
||||
{
|
||||
public Type SubType { get; private set; }
|
||||
public string PropertyName { get; private set; }
|
||||
|
||||
public KnownSubTypeWithPropertyAttribute(Type subType, string propertyName)
|
||||
{
|
||||
SubType = subType;
|
||||
PropertyName = propertyName;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly string _typeMappingPropertyName;
|
||||
|
||||
private bool _isInsideRead;
|
||||
private JsonReader _reader;
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_isInsideRead)
|
||||
return true;
|
||||
|
||||
return !string.IsNullOrEmpty(_reader.Path);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override bool CanWrite
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public JsonSubtypes()
|
||||
{
|
||||
}
|
||||
|
||||
public JsonSubtypes(string typeMappingPropertyName)
|
||||
{
|
||||
_typeMappingPropertyName = typeMappingPropertyName;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return _typeMappingPropertyName != null;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Comment)
|
||||
reader.Read();
|
||||
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.Null:
|
||||
return null;
|
||||
case JsonToken.StartArray:
|
||||
return ReadArray(reader, objectType, serializer);
|
||||
case JsonToken.StartObject:
|
||||
return ReadObject(reader, objectType, serializer);
|
||||
default:
|
||||
throw new Exception("Array: Unrecognized token: " + reader.TokenType);
|
||||
}
|
||||
}
|
||||
|
||||
private IList ReadArray(JsonReader reader, Type targetType, JsonSerializer serializer)
|
||||
{
|
||||
var elementType = GetElementType(targetType);
|
||||
|
||||
var list = CreateCompatibleList(targetType, elementType);
|
||||
|
||||
while (reader.TokenType != JsonToken.EndArray && reader.Read())
|
||||
{
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.Null:
|
||||
list.Add(reader.Value);
|
||||
break;
|
||||
case JsonToken.Comment:
|
||||
break;
|
||||
case JsonToken.StartObject:
|
||||
list.Add(ReadObject(reader, elementType, serializer));
|
||||
break;
|
||||
case JsonToken.EndArray:
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Array: Unrecognized token: " + reader.TokenType);
|
||||
}
|
||||
}
|
||||
if (targetType.IsArray)
|
||||
{
|
||||
var array = Array.CreateInstance(targetType.GetElementType(), list.Count);
|
||||
list.CopyTo(array, 0);
|
||||
list = array;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static IList CreateCompatibleList(Type targetContainerType, Type elementType)
|
||||
{
|
||||
IList list;
|
||||
if (targetContainerType.IsArray || targetContainerType{{^isNet40}}.GetTypeInfo(){{/isNet40}}.IsAbstract)
|
||||
{
|
||||
list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
|
||||
}
|
||||
else
|
||||
{
|
||||
list = (IList)Activator.CreateInstance(targetContainerType);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static Type GetElementType(Type arrayOrGenericContainer)
|
||||
{
|
||||
Type elementType;
|
||||
if (arrayOrGenericContainer.IsArray)
|
||||
{
|
||||
elementType = arrayOrGenericContainer.GetElementType();
|
||||
}
|
||||
else
|
||||
{
|
||||
elementType = arrayOrGenericContainer{{#isNet40}}.GetGenericArguments().FirstOrDefault(){{/isNet40}}{{^isNet40}}.GenericTypeArguments[0]{{/isNet40}};
|
||||
}
|
||||
return elementType;
|
||||
}
|
||||
|
||||
private object ReadObject(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
{
|
||||
var jObject = JObject.Load(reader);
|
||||
|
||||
var targetType = GetType(jObject, objectType) ?? objectType;
|
||||
|
||||
return _ReadJson(CreateAnotherReader(jObject, reader), targetType, null, serializer);
|
||||
}
|
||||
|
||||
private static JsonReader CreateAnotherReader(JObject jObject, JsonReader reader)
|
||||
{
|
||||
var jObjectReader = jObject.CreateReader();
|
||||
jObjectReader.Culture = reader.Culture;
|
||||
jObjectReader.CloseInput = reader.CloseInput;
|
||||
jObjectReader.SupportMultipleContent = reader.SupportMultipleContent;
|
||||
jObjectReader.DateTimeZoneHandling = reader.DateTimeZoneHandling;
|
||||
jObjectReader.FloatParseHandling = reader.FloatParseHandling;
|
||||
jObjectReader.DateFormatString = reader.DateFormatString;
|
||||
jObjectReader.DateParseHandling = reader.DateParseHandling;
|
||||
return jObjectReader;
|
||||
}
|
||||
|
||||
public Type GetType(JObject jObject, Type parentType)
|
||||
{
|
||||
if (_typeMappingPropertyName == null)
|
||||
{
|
||||
return GetTypeByPropertyPresence(jObject, parentType);
|
||||
}
|
||||
return GetTypeFromDiscriminatorValue(jObject, parentType);
|
||||
}
|
||||
|
||||
private static Type GetTypeByPropertyPresence(JObject jObject, Type parentType)
|
||||
{
|
||||
foreach (var type in parentType{{#isNet40}}.GetCustomAttributes(false).OfType{{/isNet40}}{{^isNet40}}.GetTypeInfo().GetCustomAttributes{{/isNet40}}<KnownSubTypeWithPropertyAttribute>())
|
||||
{
|
||||
JToken ignore;
|
||||
if (jObject.TryGetValue(type.PropertyName, out ignore))
|
||||
{
|
||||
return type.SubType;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Type GetTypeFromDiscriminatorValue(JObject jObject, Type parentType)
|
||||
{
|
||||
JToken jToken;
|
||||
if (!jObject.TryGetValue(_typeMappingPropertyName, out jToken)) return null;
|
||||
|
||||
var discriminatorValue = jToken.ToObject<object>();
|
||||
if (discriminatorValue == null) return null;
|
||||
|
||||
var typeMapping = GetSubTypeMapping(parentType);
|
||||
if (typeMapping.Any())
|
||||
{
|
||||
return GetTypeFromMapping(typeMapping, discriminatorValue);
|
||||
}
|
||||
return GetTypeByName(discriminatorValue as string, parentType);
|
||||
}
|
||||
|
||||
private static Type GetTypeByName(string typeName, Type parentType)
|
||||
{
|
||||
if (typeName == null)
|
||||
return null;
|
||||
|
||||
var insideAssembly = parentType{{^isNet40}}.GetTypeInfo(){{/isNet40}}.Assembly;
|
||||
|
||||
var typeByName = insideAssembly.GetType(typeName);
|
||||
if (typeByName == null)
|
||||
{
|
||||
var searchLocation = parentType.FullName.Substring(0, parentType.FullName.Length - parentType.Name.Length);
|
||||
typeByName = insideAssembly.GetType(searchLocation + typeName, false, true);
|
||||
}
|
||||
return typeByName;
|
||||
}
|
||||
|
||||
private static Type GetTypeFromMapping(I{{^isNet40}}ReadOnly{{/isNet40}}Dictionary<object, Type> typeMapping, object discriminatorValue)
|
||||
{
|
||||
var targetlookupValueType = typeMapping.First().Key.GetType();
|
||||
var lookupValue = ConvertJsonValueToType(discriminatorValue, targetlookupValueType);
|
||||
|
||||
Type targetType;
|
||||
return typeMapping.TryGetValue(lookupValue, out targetType) ? targetType : null;
|
||||
}
|
||||
|
||||
private static Dictionary<object, Type> GetSubTypeMapping(Type type)
|
||||
{
|
||||
return type{{#isNet40}}.GetCustomAttributes(false).OfType{{/isNet40}}{{^isNet40}}.GetTypeInfo().GetCustomAttributes{{/isNet40}}<KnownSubTypeAttribute>().ToDictionary(x => x.AssociatedValue, x => x.SubType);
|
||||
}
|
||||
|
||||
private static object ConvertJsonValueToType(object objectType, Type targetlookupValueType)
|
||||
{
|
||||
if (targetlookupValueType{{^isNet40}}.GetTypeInfo(){{/isNet40}}.IsEnum)
|
||||
return Enum.ToObject(targetlookupValueType, objectType);
|
||||
|
||||
return Convert.ChangeType(objectType, targetlookupValueType);
|
||||
}
|
||||
|
||||
protected object _ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
_reader = reader;
|
||||
_isInsideRead = true;
|
||||
try
|
||||
{
|
||||
return serializer.Deserialize(reader, objectType);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isInsideRead = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,12 @@
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\Newtonsoft.Json.10.0.3\lib\{{targetFrameworkNuget}}\Newtonsoft.Json.dll</HintPath>
|
||||
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\Newtonsoft.Json.10.0.3\lib\{{targetFrameworkNuget}}\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="JsonSubTypes">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\RestSharp.105.1.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{targetFrameworkNuget}}{{/isNet40}}\RestSharp.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\RestSharp.105.1.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{targetFrameworkNuget}}{{/isNet40}}\RestSharp.dll</HintPath>
|
||||
|
||||
@@ -39,7 +39,7 @@ This C# SDK is automatically generated by the [Swagger Codegen](https://github.c
|
||||
{{#netStandard}}
|
||||
- FubarCoder.RestSharp.Portable.Core >=4.0.7
|
||||
- FubarCoder.RestSharp.Portable.HttpClient >=4.0.7
|
||||
- Newtonsoft.Json >=9.0.1
|
||||
- Newtonsoft.Json >=10.0.3
|
||||
{{/netStandard}}
|
||||
{{^netStandard}}
|
||||
- [RestSharp](https://www.nuget.org/packages/RestSharp) - 105.1.0 or later
|
||||
|
||||
@@ -64,6 +64,12 @@
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\Newtonsoft.Json.10.0.3\lib\{{targetFrameworkNuget}}\Newtonsoft.Json.dll</HintPath>
|
||||
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\Newtonsoft.Json.10.0.3\lib\{{targetFrameworkNuget}}\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="JsonSubTypes">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('{{binRelativePath}}')">{{binRelativePath}}\JsonSubTypes.1.1.3\lib\{{targetFrameworkNuget}}\JsonSubTypes.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\RestSharp.105.1.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{targetFrameworkNuget}}{{/isNet40}}\RestSharp.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\RestSharp.105.1.0\lib\{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{targetFrameworkNuget}}{{/isNet40}}\RestSharp.dll</HintPath>
|
||||
|
||||
@@ -25,7 +25,8 @@
|
||||
{{^netStandard}}
|
||||
<PackageReference Include="RestSharp" Version="105.1.0" />
|
||||
{{/netStandard}}
|
||||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="JsonSubTypes" Version="1.1.3" />
|
||||
</ItemGroup>
|
||||
|
||||
{{^netStandard}}
|
||||
|
||||
@@ -24,7 +24,8 @@
|
||||
{{^netStandard}}
|
||||
<PackageReference Include="RestSharp" Version="105.1.0" />
|
||||
{{/netStandard}}
|
||||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
<PackageReference Include="JsonSubTypes" Version="1.1.3" />
|
||||
</ItemGroup>
|
||||
|
||||
{{^netStandard}}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<packages>
|
||||
<package id="RestSharp" version="105.1.0" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{targetFrameworkNuget}}{{/isNet40}}" developmentDependency="true" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
|
||||
<package id="JsonSubTypes" version="1.1.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
|
||||
{{#generatePropertyChanged}}
|
||||
<package id="Fody" version="1.29.4" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
|
||||
<package id="PropertyChanged.Fody" version="1.51.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
<package id="NUnit" version="2.6.4" targetFramework="{{targetFrameworkNuget}}" />
|
||||
<package id="RestSharp" version="105.1.0" targetFramework="{{#isNet40}}net4{{/isNet40}}{{^isNet40}}{{targetFrameworkNuget}}{{/isNet40}}" developmentDependency="true" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
|
||||
<package id="JsonSubTypes" version="1.1.3" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
|
||||
</packages>
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
"dependencies": {
|
||||
"FubarCoder.RestSharp.Portable.Core": "4.0.7",
|
||||
"FubarCoder.RestSharp.Portable.HttpClient": "4.0.7",
|
||||
"Newtonsoft.Json": "9.0.1"
|
||||
"Newtonsoft.Json": "10.0.3",
|
||||
"JsonSubTypes": "1.1.3"
|
||||
},
|
||||
"frameworks": {
|
||||
"{{targetFrameworkNuget}}": {}
|
||||
|
||||
Reference in New Issue
Block a user