[csharp-netcore] Support .NET Core 3.0/3.1 and .NET Standard 2.1 (#5111)

* Add .NET Core 3.x / .NET Standard 2.1

* Fixed test project target framework

* Fixed missing sync property

* Update generator docs
This commit is contained in:
Tatsuro Shibamura
2020-01-29 17:50:06 +09:00
committed by GitHub
parent 8214460ec5
commit e27700cfee
4 changed files with 34 additions and 17 deletions

View File

@@ -25,7 +25,7 @@ sidebar_label: csharp-netcore
|returnICollection|Return ICollection<T> instead of the concrete type.| |false|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|sourceFolder|source folder for generated code| |src|
|targetFramework|The target .NET framework version.|<dl><dt>**netstandard1.3**</dt><dd>.NET Standard 1.3 compatible</dd><dt>**netstandard1.4**</dt><dd>.NET Standard 1.4 compatible</dd><dt>**netstandard1.5**</dt><dd>.NET Standard 1.5 compatible</dd><dt>**netstandard1.6**</dt><dd>.NET Standard 1.6 compatible</dd><dt>**netstandard2.0**</dt><dd>.NET Standard 2.0 compatible</dd><dt>**netcoreapp2.0**</dt><dd>.NET Core 2.0 compatible</dd><dl>|netstandard2.0|
|targetFramework|The target .NET framework version.|<dl><dt>**netstandard1.3**</dt><dd>.NET Standard 1.3 compatible</dd><dt>**netstandard1.4**</dt><dd>.NET Standard 1.4 compatible</dd><dt>**netstandard1.5**</dt><dd>.NET Standard 1.5 compatible</dd><dt>**netstandard1.6**</dt><dd>.NET Standard 1.6 compatible</dd><dt>**netstandard2.0**</dt><dd>.NET Standard 2.0 compatible</dd><dt>**netstandard2.1**</dt><dd>.NET Standard 2.1 compatible</dd><dt>**netcoreapp2.0**</dt><dd>.NET Core 2.0 compatible</dd><dt>**netcoreapp2.1**</dt><dd>.NET Core 2.1 compatible</dd><dt>**netcoreapp3.0**</dt><dd>.NET Core 3.0 compatible</dd><dt>**netcoreapp3.1**</dt><dd>.NET Core 3.1 compatible</dd><dl>|netstandard2.0|
|useCollection|Deserialize array types to Collection&lt;T&gt; instead of List&lt;T&gt;.| |false|
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
|validatable|Generates self-validatable models.| |true|

View File

@@ -56,7 +56,11 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
FrameworkStrategy.NETSTANDARD_1_5,
FrameworkStrategy.NETSTANDARD_1_6,
FrameworkStrategy.NETSTANDARD_2_0,
FrameworkStrategy.NETCOREAPP_2_0
FrameworkStrategy.NETSTANDARD_2_1,
FrameworkStrategy.NETCOREAPP_2_0,
FrameworkStrategy.NETCOREAPP_2_1,
FrameworkStrategy.NETCOREAPP_3_0,
FrameworkStrategy.NETCOREAPP_3_1
);
private static FrameworkStrategy defaultFramework = FrameworkStrategy.NETSTANDARD_2_0;
protected final Map<String, String> frameworks;
@@ -67,6 +71,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
// Defines TargetFrameworkVersion in csproj files
protected String targetFramework = defaultFramework.name;
protected String testTargetFramework = defaultFramework.testTargetFramework;
// Defines nuget identifiers for target framework
protected String targetFrameworkNuget = targetFramework;
@@ -548,6 +553,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
setTargetFrameworkNuget(strategy.getNugetFrameworkIdentifier());
setTargetFramework(strategy.name);
setTestTargetFramework(strategy.testTargetFramework);
if (strategy != FrameworkStrategy.NETSTANDARD_2_0) {
LOGGER.warn("If using built-in templates-RestSharp only supports netstandard 2.0 or later.");
@@ -571,6 +577,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
syncStringProperty(additionalProperties, CodegenConstants.MODEL_PACKAGE, this::setModelPackage, modelPackage);
syncStringProperty(additionalProperties, CodegenConstants.OPTIONAL_PROJECT_GUID, this::setPackageGuid, packageGuid);
syncStringProperty(additionalProperties, "targetFrameworkNuget", this::setTargetFrameworkNuget, this.targetFrameworkNuget);
syncStringProperty(additionalProperties, "testTargetFramework", this::setTestTargetFramework, this.testTargetFramework);
syncBooleanProperty(additionalProperties, "netStandard", this::setNetStandard, this.netStandard);
@@ -702,6 +709,10 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
LOGGER.info("Generating code for .NET Framework " + this.targetFramework);
}
public void setTestTargetFramework(String testTargetFramework) {
this.testTargetFramework = testTargetFramework;
}
public void setTargetFrameworkNuget(String targetFrameworkNuget) {
this.targetFrameworkNuget = targetFrameworkNuget;
}
@@ -854,34 +865,41 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
// https://docs.microsoft.com/en-us/dotnet/standard/net-standard
@SuppressWarnings("Duplicates")
private static abstract class FrameworkStrategy {
static FrameworkStrategy NETSTANDARD_1_3 = new FrameworkStrategy("netstandard1.3", ".NET Standard 1.3 compatible", "v4.6.1") {
static FrameworkStrategy NETSTANDARD_1_3 = new FrameworkStrategy("netstandard1.3", ".NET Standard 1.3 compatible", "netcoreapp2.0") {
};
static FrameworkStrategy NETSTANDARD_1_4 = new FrameworkStrategy("netstandard1.4", ".NET Standard 1.4 compatible", "v4.6.1") {
static FrameworkStrategy NETSTANDARD_1_4 = new FrameworkStrategy("netstandard1.4", ".NET Standard 1.4 compatible", "netcoreapp2.0") {
};
static FrameworkStrategy NETSTANDARD_1_5 = new FrameworkStrategy("netstandard1.5", ".NET Standard 1.5 compatible", "v4.6.1") {
static FrameworkStrategy NETSTANDARD_1_5 = new FrameworkStrategy("netstandard1.5", ".NET Standard 1.5 compatible", "netcoreapp2.0") {
};
static FrameworkStrategy NETSTANDARD_1_6 = new FrameworkStrategy("netstandard1.6", ".NET Standard 1.6 compatible", "v4.6.1") {
static FrameworkStrategy NETSTANDARD_1_6 = new FrameworkStrategy("netstandard1.6", ".NET Standard 1.6 compatible", "netcoreapp2.0") {
};
static FrameworkStrategy NETSTANDARD_2_0 = new FrameworkStrategy("netstandard2.0", ".NET Standard 2.0 compatible", "v4.6.1") {
static FrameworkStrategy NETSTANDARD_2_0 = new FrameworkStrategy("netstandard2.0", ".NET Standard 2.0 compatible", "netcoreapp2.0") {
};
static FrameworkStrategy NETCOREAPP_2_0 = new FrameworkStrategy("netcoreapp2.0", ".NET Core 2.0 compatible", "v4.6.1", Boolean.FALSE) {
static FrameworkStrategy NETSTANDARD_2_1 = new FrameworkStrategy("netstandard2.1", ".NET Standard 2.1 compatible", "netcoreapp3.0") {
};
static FrameworkStrategy NETCOREAPP_2_0 = new FrameworkStrategy("netcoreapp2.0", ".NET Core 2.0 compatible", "netcoreapp2.0", Boolean.FALSE) {
};
static FrameworkStrategy NETCOREAPP_2_1 = new FrameworkStrategy("netcoreapp2.1", ".NET Core 2.1 compatible", "netcoreapp2.1", Boolean.FALSE) {
};
static FrameworkStrategy NETCOREAPP_3_0 = new FrameworkStrategy("netcoreapp3.0", ".NET Core 3.0 compatible", "netcoreapp3.0", Boolean.FALSE) {
};
static FrameworkStrategy NETCOREAPP_3_1 = new FrameworkStrategy("netcoreapp3.1", ".NET Core 3.1 compatible", "netcoreapp3.1", Boolean.FALSE) {
};
protected String name;
protected String description;
protected String dotNetFrameworkVersion;
protected String testTargetFramework;
private Boolean isNetStandard = Boolean.TRUE;
FrameworkStrategy(String name, String description, String dotNetFrameworkVersion) {
FrameworkStrategy(String name, String description, String testTargetFramework) {
this.name = name;
this.description = description;
this.dotNetFrameworkVersion = dotNetFrameworkVersion;
this.testTargetFramework = testTargetFramework;
}
FrameworkStrategy(String name, String description, String dotNetFrameworkVersion, Boolean isNetStandard) {
FrameworkStrategy(String name, String description, String testTargetFramework, Boolean isNetStandard) {
this.name = name;
this.description = description;
this.dotNetFrameworkVersion = dotNetFrameworkVersion;
this.testTargetFramework = testTargetFramework;
this.isNetStandard = isNetStandard;
}

View File

@@ -17,8 +17,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>{{testPackageName}}</RootNamespace>
<AssemblyName>{{testPackageName}}</AssemblyName>
<!-- By Microsoft design, test project can only be netcoreapp2.0 (even if main project is .netstandard -->
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>{{testTargetFramework}}</TargetFramework>
<IsPackable>false</IsPackable>
<FileAlignment>512</FileAlignment>
</PropertyGroup>

View File

@@ -13,7 +13,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<RootNamespace>{{testPackageName}}</RootNamespace>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>{{testTargetFramework}}</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>