diff --git a/docs/generators/aspnetcore.md b/docs/generators/aspnetcore.md index 14d54f778d4..d7e282c8279 100644 --- a/docs/generators/aspnetcore.md +++ b/docs/generators/aspnetcore.md @@ -19,7 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |licenseUrl|The URL of the license| |http://localhost| |modelClassModifier|Model Class Modifier can be nothing or partial| |partial| |newtonsoftVersion|Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+| |3.0.0| -|nullableReferenceTypes|Annotate Project with <Nullable>annotations</Nullable> and use ? annotation on all nullable attributes. Only supported on C# 8 / ASP.NET Core 3.0 or newer.| |false| +|nullableReferenceTypes|Use nullable annotations in the project. Only supported on C# 8 / ASP.NET Core 3.0 or newer.| |false| |operationIsAsync|Set methods to async or sync (default).| |false| |operationModifier|Operation Modifier can be virtual or abstract|
**virtual**
Keep method virtual
**abstract**
Make method abstract
|virtual| |operationResultTask|Set methods result to Task<>.| |false| diff --git a/docs/generators/csharp-netcore.md b/docs/generators/csharp-netcore.md index 0887dcbfeca..e7b92383494 100644 --- a/docs/generators/csharp-netcore.md +++ b/docs/generators/csharp-netcore.md @@ -17,6 +17,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |PascalCase| |netCoreProjectFile|Use the new format (.NET Core) for .NET project files (.csproj).| |false| |nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.| |false| +|nullableReferenceTypes|Use nullable annotations in the project. Only supported on C# 8 / ASP.NET Core 3.0 or newer.| |false| |optionalAssemblyInfo|Generate AssemblyInfo.cs.| |true| |optionalEmitDefaultValues|Set DataMember's EmitDefaultValue.| |false| |optionalMethodArgument|C# Optional method argument, e.g. void square(int x=10) (.net 4.0+ only).| |true| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index b8caef27e9f..72a3d57ce0f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -211,6 +211,9 @@ public class CodegenConstants { public static final String DOTNET_FRAMEWORK = "targetFramework"; public static final String DOTNET_FRAMEWORK_DESC = "The target .NET framework version. To target multiple frameworks, use `;` as the separator, e.g. `netstandard2.1;netcoreapp3.0`"; + public static final String NULLABLE_REFERENCE_TYPES = "nullableReferenceTypes"; + public static final String NULLABLE_REFERENCE_TYPES_DESC = "Use nullable annotations in the project. Only supported on C# 8 / ASP.NET Core 3.0 or newer."; + public static final String TEMPLATING_ENGINE = "templatingEngine"; public static final String TEMPLATING_ENGINE_DESC = "The templating engine plugin to use: \"mustache\" (default) or \"handlebars\" (beta)"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index 883f6cb078b..b939826b1ff 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -47,6 +47,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co protected boolean useCollection = false; protected boolean returnICollection = false; protected boolean netCoreProjectFileFlag = false; + protected boolean nullReferenceTypesFlag = false; protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.PascalCase.name(); @@ -356,6 +357,12 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co additionalProperties.put(CodegenConstants.NETCORE_PROJECT_FILE, netCoreProjectFileFlag); } + if (additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES)) { + setNullableReferenceTypes(convertPropertyToBooleanAndWriteBack(CodegenConstants.NULLABLE_REFERENCE_TYPES)); + } else { + additionalProperties.put(CodegenConstants.NULLABLE_REFERENCE_TYPES, nullReferenceTypesFlag); + } + if (additionalProperties.containsKey(CodegenConstants.INTERFACE_PREFIX)) { String useInterfacePrefix = additionalProperties.get(CodegenConstants.INTERFACE_PREFIX).toString(); if ("false".equals(useInterfacePrefix.toLowerCase(Locale.ROOT))) { @@ -1112,6 +1119,13 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co return interfacePrefix; } + public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag){ + this.nullReferenceTypesFlag = nullReferenceTypesFlag; + if (nullReferenceTypesFlag == true){ + this.nullableType.add("string"); + } + } + public void setInterfacePrefix(final String interfacePrefix) { this.interfacePrefix = interfacePrefix; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java index ca50b0ff1f4..16957ff6b3b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java @@ -60,7 +60,6 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { public static final String USE_NEWTONSOFT = "useNewtonsoft"; public static final String USE_DEFAULT_ROUTING = "useDefaultRouting"; public static final String NEWTONSOFT_VERSION = "newtonsoftVersion"; - public static final String NULLABLE_REFERENCE_TYPES = "nullableReferenceTypes"; private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}"; private String userSecretsGuid = randomUUID().toString(); @@ -85,7 +84,6 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { private boolean useFrameworkReference = false; private boolean useNewtonsoft = true; private boolean useDefaultRouting = true; - private boolean nullableReferenceTypes = false; private String newtonsoftVersion = "3.0.0"; public AspNetCoreServerCodegen() { @@ -211,6 +209,10 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { cliOptions.add(swashbuckleVersion); // CLI Switches + addSwitch(CodegenConstants.NULLABLE_REFERENCE_TYPES, + CodegenConstants.NULLABLE_REFERENCE_TYPES_DESC, + this.nullReferenceTypesFlag); + addSwitch(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC, sortParamsByRequiredFlag); @@ -251,11 +253,6 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { "Use default routing for the ASP.NET Core version.", useDefaultRouting); - addSwitch(NULLABLE_REFERENCE_TYPES, - "Annotate Project with annotations and use ? annotation on all nullable attributes. " + - "Only supported on C# 8 / ASP.NET Core 3.0 or newer.", - nullableReferenceTypes); - addOption(CodegenConstants.ENUM_NAME_SUFFIX, CodegenConstants.ENUM_NAME_SUFFIX_DESC, enumNameSuffix); @@ -373,7 +370,6 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { setIsFramework(); setUseNewtonsoft(); setUseEndpointRouting(); - setNullableReferenceTypes(); supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh")); supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat")); @@ -528,7 +524,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { @Override public String getNullableType(Schema p, String type) { if (languageSpecificPrimitives.contains(type)) { - if (isSupportNullable() && ModelUtils.isNullable(p) && (nullableType.contains(type) || nullableReferenceTypes)) { + if (isSupportNullable() && ModelUtils.isNullable(p) && (nullableType.contains(type) || nullReferenceTypesFlag)) { return type + "?"; } else { return type; @@ -657,20 +653,6 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { } } - private void setNullableReferenceTypes() { - if (additionalProperties.containsKey(NULLABLE_REFERENCE_TYPES)) { - if (aspnetCoreVersion.getOptValue().startsWith("2.")) { - LOGGER.warn("Nullable annotation are not supported in ASP.NET core version 2. Setting {} to false", - NULLABLE_REFERENCE_TYPES); - additionalProperties.put(NULLABLE_REFERENCE_TYPES, false); - } else { - nullableReferenceTypes = convertPropertyToBooleanAndWriteBack(NULLABLE_REFERENCE_TYPES); - } - } else { - additionalProperties.put(NULLABLE_REFERENCE_TYPES, nullableReferenceTypes); - } - } - private void setOperationIsAsync() { if (isLibrary) { operationIsAsync = false; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java index 2b0a5fe068d..d0bb7c24b6e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java @@ -230,6 +230,10 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen { cliOptions.add(modelPropertyNaming.defaultValue("PascalCase")); // CLI Switches + addSwitch(CodegenConstants.NULLABLE_REFERENCE_TYPES, + CodegenConstants.NULLABLE_REFERENCE_TYPES_DESC, + this.nullReferenceTypesFlag); + addSwitch(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC, this.hideGenerationTimestamp); diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/netcore_project.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/netcore_project.mustache index 7d77d34b4d8..4f29dcf3d34 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/netcore_project.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/netcore_project.mustache @@ -19,6 +19,9 @@ git{{#releaseNote}} {{releaseNote}}{{/releaseNote}}{{#packageTags}} {{{packageTags}}}{{/packageTags}} + {{#nullableReferenceTypes}} + annotations + {{/nullableReferenceTypes}} diff --git a/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache b/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache index 2cb30d650a5..fd4168d0d4c 100644 --- a/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache @@ -15,6 +15,9 @@ true {{packageName}} {{packageVersion}} + {{#nullableReferenceTypes}} + annotations + {{/nullableReferenceTypes}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CSharpModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CSharpModelTest.java index 4a8f8f3e3bc..b387a0a2986 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CSharpModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/csharp/CSharpModelTest.java @@ -21,6 +21,7 @@ import com.google.common.collect.Sets; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.*; import io.swagger.v3.parser.util.SchemaTypeUtil; +import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.DefaultCodegen; @@ -335,7 +336,7 @@ public class CSharpModelTest { .addProperties("subObject", new Schema().addProperties("name", new StringSchema()).nullable(true)) .addRequiredItem("id"); final DefaultCodegen codegen = new AspNetCoreServerCodegen(); - codegen.additionalProperties().put(AspNetCoreServerCodegen.NULLABLE_REFERENCE_TYPES, true); + codegen.additionalProperties().put(CodegenConstants.NULLABLE_REFERENCE_TYPES, true); codegen.processOpts(); OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); codegen.setOpenAPI(openAPI);