[csharp-netcore] Remove use of dynamic type and optimize reference (#5206)

* Change unnecessary `dynamic` to `object`

* Optimize package reference

* Fixed JsonSubTypes reference

* Update sample project

* Remove redundant method calls

* Update sample project

* Update README template
This commit is contained in:
Tatsuro Shibamura
2020-02-05 11:51:48 +09:00
committed by GitHub
parent 5bf501af40
commit 7e54196967
16 changed files with 37 additions and 50 deletions

View File

@@ -114,7 +114,7 @@ namespace {{packageName}}.Client
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{
return ClientUtils.ConvertType(response.Content, type);
return Convert.ChangeType(response.Content, type);
}
// at this point, it must be a model (json)

View File

@@ -19,7 +19,7 @@ namespace {{packageName}}.Client
/// Gets or sets the error content (body json object)
/// </summary>
/// <value>The error content (Http response body).</value>
public {{#supportsAsync}}dynamic{{/supportsAsync}}{{^supportsAsync}}object{{/supportsAsync}} ErrorContent { get; private set; }
public object ErrorContent { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
@@ -42,7 +42,7 @@ namespace {{packageName}}.Client
/// <param name="errorCode">HTTP status code.</param>
/// <param name="message">Error message.</param>
/// <param name="errorContent">Error content.</param>
public ApiException(int errorCode, string message, {{#supportsAsync}}dynamic{{/supportsAsync}}{{^supportsAsync}}object{{/supportsAsync}} errorContent = null) : base(message)
public ApiException(int errorCode, string message, object errorContent = null) : base(message)
{
this.ErrorCode = errorCode;
this.ErrorContent = errorContent;

View File

@@ -159,22 +159,6 @@ namespace {{packageName}}.Client
}
}
/// <summary>
/// Dynamically cast the object into target type.
/// </summary>
/// <param name="fromObject">Object to be casted</param>
/// <param name="toObject">Target type</param>
/// <returns>Casted object</returns>
{{#supportsAsync}}
public static dynamic ConvertType(dynamic fromObject, Type toObject)
{{/supportsAsync}}
{{^supportsAsync}}
public static object ConvertType<T>(T fromObject, Type toObject) where T : class
{{/supportsAsync}}
{
return Convert.ChangeType(fromObject, toObject);
}
/// <summary>
/// Select the Content-Type header's value from the given content-type array:
/// if JSON type exists in the given array, use it;

View File

@@ -36,10 +36,14 @@
</PropertyGroup>
<ItemGroup>
{{#useCompareNetObjects}}
<PackageReference Include="CompareNETObjects" Version="4.57.0" />
{{/useCompareNetObjects}}
<PackageReference Include="JsonSubTypes" Version="1.5.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="RestSharp" Version="106.10.1" />
{{#validatable}}
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
{{/validatable}}
</ItemGroup>
</Project>

View File

@@ -30,16 +30,24 @@ This C# SDK is automatically generated by the [OpenAPI Generator](https://openap
- [RestSharp](https://www.nuget.org/packages/RestSharp) - 106.10.1 or later
- [Json.NET](https://www.nuget.org/packages/Newtonsoft.Json/) - 12.0.1 or later
- [JsonSubTypes](https://www.nuget.org/packages/JsonSubTypes/) - 1.5.2 or later
{{#useCompareNetObjects}}
- [CompareNETObjects](https://www.nuget.org/packages/CompareNETObjects) - 4.57.0 or later
{{/useCompareNetObjects}}
{{#validatable}}
- [System.ComponentModel.Annotations](https://www.nuget.org/packages/System.ComponentModel.Annotations) - 4.5.0 or later
{{/validatable}}
The DLLs included in the package may not be the latest version. We recommend using [NuGet](https://docs.nuget.org/consume/installing-nuget) to obtain the latest version of the packages:
```
Install-Package RestSharp
Install-Package Newtonsoft.Json
Install-Package JsonSubTypes
{{#validatable}}
Install-Package System.ComponentModel.Annotations
{{/validatable}}
{{#useCompareNetObjects}}
Install-Package CompareNETObjects
{{/useCompareNetObjects}}
```
NOTE: RestSharp versions greater than 105.1.0 have a bug which causes file uploads to fail. See [RestSharp#742](https://github.com/restsharp/RestSharp/issues/742)

View File

@@ -22,11 +22,15 @@
</PropertyGroup>
<ItemGroup>
{{#useCompareNetObjects}}
<PackageReference Include="CompareNETObjects" Version="4.57.0" />
{{/useCompareNetObjects}}
<PackageReference Include="JsonSubTypes" Version="1.5.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="RestSharp" Version="106.10.1" />
{{#validatable}}
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
{{/validatable}}
</ItemGroup>
</Project>

View File

@@ -30,12 +30,15 @@
<!-- Dependencies are automatically installed when the package is installed -->
<dependencies>
<dependency id="NewtonSoft.Json" version="12.0.1" />
<dependency id="JsonSubTypes" version="1.5.1" />
<dependency id="Newtonsoft.Json" version="12.0.1" />
<dependency id="RestSharp" version="106.10.1" />
{{#useCompareNetObjects}}
<dependency id="CompareNETObjects" version="4.57.0" />
{{/useCompareNetObjects}}
<dependency id="JsonSubTypes" version="1.5.2" />
{{#validatable}}
<dependency id="System.ComponentModel.Annotations" version="4.5.0" />
{{/validatable}}
</dependencies>
</metadata>

View File

@@ -5,6 +5,8 @@
{{/useCompareNetObjects}}
<package id="RestSharp" version="106.10.1" targetFramework="{{^netStandard}}net452{{/netStandard}}{{#netStandard}}netstandard2.0{{/netStandard}}" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
<package id="JsonSubTypes" version="1.5.1" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
<package id="JsonSubTypes" version="1.5.2" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
{{#validatable}}
<package id="System.ComponentModel.Annotations" version="4.5.0" targetFramework="{{targetFrameworkNuget}}" developmentDependency="true" />
{{/validatable}}
</packages>

View File

@@ -5,6 +5,9 @@
{{#useCompareNetObjects}}
"CompareNETObjects": "4.57.0",
{{/useCompareNetObjects}}
{{#validatable}}
"System.ComponentModel.Annotations": "4.5.0",
{{/validatable}}
"JsonSubTypes": "1.5.2",
"RestSharp": "106.10.1"
},

View File

@@ -118,7 +118,7 @@ namespace Org.OpenAPITools.Client
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{
return ClientUtils.ConvertType(response.Content, type);
return Convert.ChangeType(response.Content, type);
}
// at this point, it must be a model (json)

View File

@@ -28,7 +28,7 @@ namespace Org.OpenAPITools.Client
/// Gets or sets the error content (body json object)
/// </summary>
/// <value>The error content (Http response body).</value>
public dynamic ErrorContent { get; private set; }
public object ErrorContent { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
@@ -51,7 +51,7 @@ namespace Org.OpenAPITools.Client
/// <param name="errorCode">HTTP status code.</param>
/// <param name="message">Error message.</param>
/// <param name="errorContent">Error content.</param>
public ApiException(int errorCode, string message, dynamic errorContent = null) : base(message)
public ApiException(int errorCode, string message, object errorContent = null) : base(message)
{
this.ErrorCode = errorCode;
this.ErrorContent = errorContent;

View File

@@ -164,17 +164,6 @@ namespace Org.OpenAPITools.Client
}
}
/// <summary>
/// Dynamically cast the object into target type.
/// </summary>
/// <param name="fromObject">Object to be casted</param>
/// <param name="toObject">Target type</param>
/// <returns>Casted object</returns>
public static dynamic ConvertType(dynamic fromObject, Type toObject)
{
return Convert.ChangeType(fromObject, toObject);
}
/// <summary>
/// Select the Content-Type header's value from the given content-type array:
/// if JSON type exists in the given array, use it;

View File

@@ -3,6 +3,7 @@
"dependencies": {
"Newtonsoft.Json": "12.0.1",
"CompareNETObjects": "4.57.0",
"System.ComponentModel.Annotations": "4.5.0",
"JsonSubTypes": "1.5.2",
"RestSharp": "106.10.1"
},

View File

@@ -119,7 +119,7 @@ namespace Org.OpenAPITools.Client
if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type
{
return ClientUtils.ConvertType(response.Content, type);
return Convert.ChangeType(response.Content, type);
}
// at this point, it must be a model (json)

View File

@@ -28,7 +28,7 @@ namespace Org.OpenAPITools.Client
/// Gets or sets the error content (body json object)
/// </summary>
/// <value>The error content (Http response body).</value>
public dynamic ErrorContent { get; private set; }
public object ErrorContent { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
@@ -51,7 +51,7 @@ namespace Org.OpenAPITools.Client
/// <param name="errorCode">HTTP status code.</param>
/// <param name="message">Error message.</param>
/// <param name="errorContent">Error content.</param>
public ApiException(int errorCode, string message, dynamic errorContent = null) : base(message)
public ApiException(int errorCode, string message, object errorContent = null) : base(message)
{
this.ErrorCode = errorCode;
this.ErrorContent = errorContent;

View File

@@ -164,17 +164,6 @@ namespace Org.OpenAPITools.Client
}
}
/// <summary>
/// Dynamically cast the object into target type.
/// </summary>
/// <param name="fromObject">Object to be casted</param>
/// <param name="toObject">Target type</param>
/// <returns>Casted object</returns>
public static dynamic ConvertType(dynamic fromObject, Type toObject)
{
return Convert.ChangeType(fromObject, toObject);
}
/// <summary>
/// Select the Content-Type header's value from the given content-type array:
/// if JSON type exists in the given array, use it;