Skip to content

Commit 11caad9

Browse files
authored
[CSharp] feat!: add useDateOnly flag (#17471)
* feat: DateOnly for client * fix: generated doc for csharp * doc: fix generators README for csharp * add DateOnly option for aspnet * refactor: update csharp docs * refactor: fix typo * finish * finish * finish * regenerate * rebuild and regenerate * fix build error * finish * revert path * regenerate * remove exceed samples * regenerate * add DateOnly to nullable * remove obsolete file from list * update test files list * remove obsolete sample build * revert manual tests * revert samples to master and regenerate * remove new samples * remove eol * remove eol * add new specs * try fix test * update tests * update files * remove eol * revert * remove eol * regenerate * add net6+ support to aspnetcore * add jobs
1 parent 9b62b9b commit 11caad9

File tree

189 files changed

+8858
-277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+8858
-277
lines changed

.github/workflows/samples-dotnet.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
paths:
66
- 'samples/client/petstore/csharp/**net6.0**/'
7+
- 'samples/client/petstore/csharp/**net7.0**/'
78
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-netcore**/'
89
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/**'
910
- 'samples/server/petstore/aspnetcore-6.0/**'
@@ -12,6 +13,7 @@ on:
1213
pull_request:
1314
paths:
1415
- 'samples/client/petstore/csharp/**net6.0**/'
16+
- 'samples/client/petstore/csharp/**net7.0**/'
1517
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-netcore**/'
1618
- 'samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/**'
1719
- 'samples/server/petstore/aspnetcore-6.0/**'
@@ -35,6 +37,8 @@ jobs:
3537
- samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf
3638
- samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf
3739
- samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests
40+
- samples/client/petstore/csharp/OpenAPIClient-generichost-net7.0-useDateTimeForDate
41+
- samples/client/petstore/csharp/OpenAPIClient-net7.0-useDateTimeForDate
3842
- samples/server/petstore/aspnetcore-6.0
3943
- samples/server/petstore/aspnetcore-6.0-pocoModels
4044
- samples/server/petstore/aspnetcore-6.0-project4Models
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# for csharp generichost
2+
generatorName: csharp
3+
outputDir: samples/client/petstore/csharp/OpenAPIClient-generichost-net7.0-useDateTimeForDate
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/csharp/dates-api.yaml
5+
library: generichost
6+
templateDir: modules/openapi-generator/src/main/resources/csharp
7+
additionalProperties:
8+
packageGuid: '{2E60EF87-DB0B-4D01-A36E-F5E90F7EC757}'
9+
useDateTimeForDate: true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# for .net standard
2+
generatorName: csharp
3+
outputDir: samples/client/petstore/csharp/OpenAPIClient-net7.0-useDateTimeForDate
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/csharp/dates-api.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/csharp
6+
additionalProperties:
7+
packageGuid: '{D0A67E81-4061-48EB-B4B8-C73BDF8B2D95}'
8+
targetFramework: net7.0
9+
useDateTimeForDate: true

docs/generators/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The following generators are available:
1212
* [cpp-restsdk](cpp-restsdk.md)
1313
* [cpp-tizen](cpp-tizen.md)
1414
* [csharp](csharp.md)
15-
* [csharp-netcore](csharp-netcore.md)
15+
* [csharp-functions](csharp-functions.md)
1616
* [dart](dart.md)
1717
* [dart-dio](dart-dio.md)
1818
* [eiffel](eiffel.md)

docs/generators/aspnetcore.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4747
|sourceFolder|source folder for generated code| |src|
4848
|swashbuckleVersion|Swashbuckle version: 3.0.0 (deprecated), 4.0.0 (deprecated), 5.0.0 (deprecated), 6.4.0|<dl><dt>**3.0.0**</dt><dd>Swashbuckle 3.0.0</dd><dt>**4.0.0**</dt><dd>Swashbuckle 4.0.0</dd><dt>**5.0.0**</dt><dd>Swashbuckle 5.0.0</dd><dt>**6.4.0**</dt><dd>Swashbuckle 6.4.0</dd></dl>|3.0.0|
4949
|useCollection|Deserialize array types to Collection&lt;T&gt; instead of List&lt;T&gt;.| |false|
50+
|useDateTimeForDate|Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)| |false|
5051
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
5152
|useDefaultRouting|Use default routing for the ASP.NET Core version.| |true|
5253
|useFrameworkReference|Use frameworkReference for ASP.NET Core 3.0+ and PackageReference ASP.NET Core 2.2 or earlier.| |false|
@@ -74,6 +75,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
7475
<ul class="column-ul">
7576
<li>Boolean</li>
7677
<li>Collection</li>
78+
<li>DateOnly</li>
79+
<li>DateOnly?</li>
7780
<li>DateTime</li>
7881
<li>DateTime?</li>
7982
<li>DateTimeOffset</li>

docs/generators/csharp-functions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4444
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4545
|sourceFolder|source folder for generated code| |src|
4646
|useCollection|Deserialize array types to Collection&lt;T&gt; instead of List&lt;T&gt;.| |false|
47+
|useDateTimeForDate|Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)| |false|
4748
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
4849
|useNewtonsoft|Uses the Newtonsoft JSON library.| |true|
4950

@@ -67,6 +68,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6768
<ul class="column-ul">
6869
<li>Boolean</li>
6970
<li>Collection</li>
71+
<li>DateOnly</li>
72+
<li>DateOnly?</li>
7073
<li>DateTime</li>
7174
<li>DateTime?</li>
7275
<li>DateTimeOffset</li>

docs/generators/csharp.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4747
|sourceFolder|source folder for generated code| |src|
4848
|targetFramework|The target .NET framework version. To target multiple frameworks, use `;` as the separator, e.g. `netstandard2.1;netcoreapp3.1`|<dl><dt>**netstandard1.3**</dt><dd>.NET Standard 1.3</dd><dt>**netstandard1.4**</dt><dd>.NET Standard 1.4</dd><dt>**netstandard1.5**</dt><dd>.NET Standard 1.5</dd><dt>**netstandard1.6**</dt><dd>.NET Standard 1.6</dd><dt>**netstandard2.0**</dt><dd>.NET Standard 2.0</dd><dt>**netstandard2.1**</dt><dd>.NET Standard 2.1</dd><dt>**net47**</dt><dd>.NET Framework 4.7</dd><dt>**net48**</dt><dd>.NET Framework 4.8</dd><dt>**net6.0**</dt><dd>.NET 6.0 (End of Support 12 November 2024)</dd><dt>**net7.0**</dt><dd>.NET 7.0</dd><dt>**net8.0**</dt><dd>.NET 8.0</dd></dl>|net8.0|
4949
|useCollection|Deserialize array types to Collection&lt;T&gt; instead of List&lt;T&gt;.| |false|
50+
|useDateTimeForDate|Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)| |false|
5051
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
5152
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
5253
|useSourceGeneration|Use source generation where available (only `generichost` library supports this option).| |false|
@@ -73,6 +74,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
7374
<ul class="column-ul">
7475
<li>Boolean</li>
7576
<li>Collection</li>
77+
<li>DateOnly</li>
78+
<li>DateOnly?</li>
7679
<li>DateTime</li>
7780
<li>DateTime?</li>
7881
<li>DateTimeOffset</li>

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public class CodegenConstants {
156156
public static final String USE_DATETIME_OFFSET = "useDateTimeOffset";
157157
public static final String USE_DATETIME_OFFSET_DESC = "Use DateTimeOffset to model date-time properties";
158158

159+
public static final String USE_DATETIME_FOR_DATE = "useDateTimeForDate";
160+
public static final String USE_DATETIME_FOR_DATE_DESC = "Use DateTime to model date properties even if DateOnly supported. (.net 6.0+ only)";
161+
159162
public static final String ENSURE_UNIQUE_PARAMS = "ensureUniqueParams";
160163
public static final String ENSURE_UNIQUE_PARAMS_DESC = "Whether to ensure parameter names are unique in an operation (rename parameters that are not).";
161164

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
5555
protected boolean optionalProjectFileFlag = true;
5656
protected boolean optionalMethodArgumentFlag = true;
5757
protected boolean useDateTimeOffsetFlag = false;
58+
protected boolean useDateTimeForDateFlag = false;
5859
protected boolean useCollection = false;
5960
protected boolean returnICollection = false;
6061
protected boolean netCoreProjectFileFlag = false;
@@ -186,6 +187,8 @@ public AbstractCSharpCodegen() {
186187
"DateTime",
187188
"DateTimeOffset?",
188189
"DateTimeOffset",
190+
"DateOnly?",
191+
"DateOnly",
189192
"Boolean",
190193
"Double",
191194
"Decimal",
@@ -234,6 +237,11 @@ public void useDateTimeOffset(boolean flag) {
234237
this.setTypeMapping();
235238
}
236239

240+
public void useDateTimeForDate(boolean flag) {
241+
this.useDateTimeForDateFlag = flag;
242+
this.setTypeMapping();
243+
}
244+
237245
@Override
238246
protected void addParentFromContainer(CodegenModel model, Schema schema) {
239247
// we do not want to inherit simply because additionalProperties is true
@@ -354,6 +362,13 @@ public void processOpts() {
354362
additionalProperties.put(CodegenConstants.USE_DATETIME_OFFSET, useDateTimeOffsetFlag);
355363
}
356364

365+
// {{useDateTimeForDate}}
366+
if (additionalProperties.containsKey(CodegenConstants.USE_DATETIME_FOR_DATE)) {
367+
useDateTimeForDate(convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_DATETIME_FOR_DATE));
368+
} else {
369+
additionalProperties.put(CodegenConstants.USE_DATETIME_FOR_DATE, useDateTimeForDateFlag);
370+
}
371+
357372
if (additionalProperties.containsKey(CodegenConstants.USE_COLLECTION)) {
358373
setUseCollection(convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_COLLECTION));
359374
} else {
@@ -1756,6 +1771,12 @@ protected boolean isValueType(CodegenProperty var) {
17561771
return (this.getValueTypes().contains(var.dataType) || var.isEnum);
17571772
}
17581773

1774+
protected boolean useNet60OrLater() { return false; }
1775+
1776+
protected boolean useDateOnly() {
1777+
return useNet60OrLater() && !useDateTimeForDateFlag;
1778+
}
1779+
17591780
@Override
17601781
public void setParameterExampleValue(CodegenParameter p) {
17611782
String example;
@@ -1820,16 +1841,18 @@ public void setParameterExampleValue(CodegenParameter p) {
18201841
}
18211842
example = "System.Text.Encoding.ASCII.GetBytes(\"" + escapeText(example) + "\")";
18221843
} else if (p.isDate) {
1844+
String dateType = typeMapping.get("date");
18231845
if (example == null) {
1824-
example = "DateTime.Parse(\"2013-10-20\")";
1846+
example = dateType + ".Parse(\"2013-10-20\")";
18251847
} else {
1826-
example = "DateTime.Parse(\"" + example + "\")";
1848+
example = dateType + ".Parse(\"" + example + "\")";
18271849
}
18281850
} else if (p.isDateTime) {
1851+
String dateType = typeMapping.get("DateTime");
18291852
if (example == null) {
1830-
example = "DateTime.Parse(\"2013-10-20T19:20:30+01:00\")";
1853+
example = dateType + ".Parse(\"2013-10-20T19:20:30+01:00\")";
18311854
} else {
1832-
example = "DateTime.Parse(\"" + example + "\")";
1855+
example = dateType + ".Parse(\"" + example + "\")";
18331856
}
18341857
} else if (p.isDecimal) {
18351858
if (example == null) {
@@ -1993,7 +2016,7 @@ protected Set<String> getNullableTypes() {
19932016
}
19942017

19952018
protected Set<String> getValueTypes() {
1996-
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double", "DateTime", "DateTimeOffset", "Guid"));
2019+
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double", "DateTime", "DateOnly", "DateTimeOffset", "Guid"));
19972020
}
19982021

19992022
protected void setTypeMapping() {
@@ -2012,7 +2035,7 @@ protected void setTypeMapping() {
20122035
typeMapping.put("decimal", "decimal");
20132036
typeMapping.put("BigDecimal", "decimal");
20142037
typeMapping.put("DateTime", this.useDateTimeOffsetFlag ? "DateTimeOffset" : "DateTime");
2015-
typeMapping.put("date", "DateTime");
2038+
typeMapping.put("date", this.useDateOnly() ? "DateOnly" : "DateTime");
20162039
typeMapping.put("file", "System.IO.Stream");
20172040
typeMapping.put("array", "List");
20182041
typeMapping.put("list", "List");

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetServerCodegen.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class AspNetServerCodegen extends AbstractCSharpCodegen {
5252
public static final String BUILD_TARGET = "buildTarget";
5353
public static final String MODEL_CLASS_MODIFIER = "modelClassModifier";
5454
public static final String TARGET_FRAMEWORK = "targetFramework";
55+
public static final String NET_60_OR_LATER = "net60OrLater";
5556

5657
public static final String PROJECT_SDK = "projectSdk";
5758
public static final String SDK_WEB = "Microsoft.NET.Sdk.Web";
@@ -209,6 +210,10 @@ public AspNetServerCodegen() {
209210
CodegenConstants.USE_DATETIME_OFFSET_DESC,
210211
useDateTimeOffsetFlag);
211212

213+
addSwitch(CodegenConstants.USE_DATETIME_FOR_DATE,
214+
CodegenConstants.USE_DATETIME_FOR_DATE_DESC,
215+
useDateTimeForDateFlag);
216+
212217
addSwitch(CodegenConstants.USE_COLLECTION,
213218
CodegenConstants.USE_COLLECTION_DESC,
214219
useCollection);
@@ -299,7 +304,7 @@ public AspNetServerCodegen() {
299304
@Override
300305
protected Set<String> getNullableTypes() {
301306
return new HashSet<>(Arrays.asList("decimal", "bool", "int", "uint", "long", "ulong", "float", "double",
302-
"DateTime", "DateTimeOffset", "Guid"));
307+
"DateTime", "DateOnly", "DateTimeOffset", "Guid"));
303308
}
304309

305310
@Override
@@ -462,6 +467,11 @@ public void processOpts() {
462467
this.setTypeMapping();
463468
}
464469

470+
@Override
471+
protected boolean useNet60OrLater() {
472+
return additionalProperties.containsKey(NET_60_OR_LATER);
473+
}
474+
465475
public void setPackageGuid(String packageGuid) {
466476
this.packageGuid = packageGuid;
467477
}
@@ -775,6 +785,20 @@ private void setIsFramework() {
775785
useFrameworkReference = true;
776786
additionalProperties.put(USE_FRAMEWORK_REFERENCE, useFrameworkReference);
777787
additionalProperties.put(TARGET_FRAMEWORK, "net6.0");
788+
} else if (aspnetCoreVersion.getOptValue().startsWith("7.")) {
789+
LOGGER.warn(
790+
"ASP.NET core version is {} so changing to use frameworkReference instead of packageReference ",
791+
aspnetCoreVersion.getOptValue());
792+
useFrameworkReference = true;
793+
additionalProperties.put(USE_FRAMEWORK_REFERENCE, useFrameworkReference);
794+
additionalProperties.put(TARGET_FRAMEWORK, "net7.0");
795+
} else if (aspnetCoreVersion.getOptValue().startsWith("8.")) {
796+
LOGGER.warn(
797+
"ASP.NET core version is {} so changing to use frameworkReference instead of packageReference ",
798+
aspnetCoreVersion.getOptValue());
799+
useFrameworkReference = true;
800+
additionalProperties.put(USE_FRAMEWORK_REFERENCE, useFrameworkReference);
801+
additionalProperties.put(TARGET_FRAMEWORK, "net8.0");
778802
} else {
779803
if (additionalProperties.containsKey(USE_FRAMEWORK_REFERENCE)) {
780804
useFrameworkReference = convertPropertyToBooleanAndWriteBack(USE_FRAMEWORK_REFERENCE);
@@ -783,6 +807,17 @@ private void setIsFramework() {
783807
}
784808
additionalProperties.put(TARGET_FRAMEWORK, "netcoreapp" + aspnetCoreVersion);
785809
}
810+
811+
setAddititonalPropertyForFramework();
812+
}
813+
814+
private void setAddititonalPropertyForFramework() {
815+
String targetFramework = ((String)additionalProperties.get(TARGET_FRAMEWORK));
816+
if (targetFramework.startsWith("net6.0") ||
817+
targetFramework.startsWith("net7.0") ||
818+
targetFramework.startsWith("net8.0")) {
819+
additionalProperties.put(NET_60_OR_LATER, true);
820+
}
786821
}
787822

788823
private void setUseNewtonsoft() {

0 commit comments

Comments
 (0)