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:
@@ -1,23 +1,11 @@
|
||||
/*
|
||||
* Swagger Petstore ' \" =end
|
||||
* Swagger Petstore *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
*
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ ' \" =end
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end - -
|
||||
*
|
||||
* OpenAPI spec version: 1.0.0 ' \" =end
|
||||
* Contact: apiteam@swagger.io ' \" =end
|
||||
* OpenAPI spec version: 1.0.0 *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
* Contact: apiteam@swagger.io *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
* Generated by: https://github.com/swagger-api/swagger-codegen.git
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
@@ -70,20 +58,20 @@ namespace IO.Swagger.Test
|
||||
[Test]
|
||||
public void InstanceTest()
|
||||
{
|
||||
// test 'IsInstanceOfType' FakeApi
|
||||
Assert.IsInstanceOfType(typeof(FakeApi), instance, "instance is a FakeApi");
|
||||
// TODO uncomment below to test 'IsInstanceOfType' FakeApi
|
||||
//Assert.IsInstanceOfType(typeof(FakeApi), instance, "instance is a FakeApi");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test TestCodeInjectEnd
|
||||
/// Test TestCodeInjectEndRnNR
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestCodeInjectEndTest()
|
||||
public void TestCodeInjectEndRnNRTest()
|
||||
{
|
||||
// TODO uncomment below to test the method and replace null with proper value
|
||||
//string testCodeInjectEnd = null;
|
||||
//instance.TestCodeInjectEnd(testCodeInjectEnd);
|
||||
//string testCodeInjectEndRnNR = null;
|
||||
//instance.TestCodeInjectEndRnNR(testCodeInjectEndRnNR);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,12 @@ Contact: apiteam@swagger.io *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="JsonSubTypes">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>
|
||||
|
||||
@@ -1,23 +1,11 @@
|
||||
/*
|
||||
* Swagger Petstore ' \" =end
|
||||
* Swagger Petstore *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
*
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ ' \" =end
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end - -
|
||||
*
|
||||
* OpenAPI spec version: 1.0.0 ' \" =end
|
||||
* Contact: apiteam@swagger.io ' \" =end
|
||||
* OpenAPI spec version: 1.0.0 *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
* Contact: apiteam@swagger.io *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
* Generated by: https://github.com/swagger-api/swagger-codegen.git
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
@@ -31,6 +19,7 @@ using IO.Swagger.Api;
|
||||
using IO.Swagger.Model;
|
||||
using IO.Swagger.Client;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IO.Swagger.Test
|
||||
{
|
||||
@@ -76,6 +65,7 @@ namespace IO.Swagger.Test
|
||||
//Assert.IsInstanceOfType<ModelReturn> (instance, "variable 'instance' is a ModelReturn");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test the property '_Return'
|
||||
/// </summary>
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
<package id="NUnit" version="2.6.4" targetFramework="net45" />
|
||||
<package id="RestSharp" version="105.1.0" targetFramework="net45" developmentDependency="true" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" developmentDependency="true" />
|
||||
<package id="JsonSubTypes" version="1.1.3" targetFramework="net45" developmentDependency="true" />
|
||||
</packages>
|
||||
|
||||
@@ -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.GetTypeInfo().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.GenericTypeArguments[0];
|
||||
}
|
||||
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.GetTypeInfo().GetCustomAttributes<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.GetTypeInfo().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(IReadOnlyDictionary<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.GetTypeInfo().GetCustomAttributes<KnownSubTypeAttribute>().ToDictionary(x => x.AssociatedValue, x => x.SubType);
|
||||
}
|
||||
|
||||
private static object ConvertJsonValueToType(object objectType, Type targetlookupValueType)
|
||||
{
|
||||
if (targetlookupValueType.GetTypeInfo().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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,12 @@ Contact: apiteam@swagger.io *_/ ' \" =end - - \\r\\n \\n \\r
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="JsonSubTypes">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\JsonSubTypes.1.1.3\lib\net45\JsonSubTypes.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp">
|
||||
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>
|
||||
<HintPath Condition="Exists('..\packages')">..\packages\RestSharp.105.1.0\lib\net45\RestSharp.dll</HintPath>
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
<packages>
|
||||
<package id="RestSharp" version="105.1.0" targetFramework="net45" developmentDependency="true" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" developmentDependency="true" />
|
||||
<package id="JsonSubTypes" version="1.1.3" targetFramework="net45" developmentDependency="true" />
|
||||
</packages>
|
||||
|
||||
Reference in New Issue
Block a user