Skip to content

Commit d7634cf

Browse files
committed
released for NgDateOnlyFormControlEnabled
1 parent 2c9f10b commit d7634cf

12 files changed

+59
-25
lines changed

DemoCoreWeb/CodeGen.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
"ClientNamespaceSuffix": ".Client",
6969
"ContainerNameSuffix": "",
7070
"DataAnnotationsToComments": true,
71-
"HelpStrictMode": true
71+
"HelpStrictMode": true,
72+
"NgDateOnlyFormControlEnabled": true
7273
},
7374

7475
{

DemoTextJsonWeb/CodeGen.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"ClientNamespaceSuffix": ".Client",
6464
"ContainerNameSuffix": "",
6565
"DataAnnotationsToComments": true,
66-
"HelpStrictMode": true
66+
"HelpStrictMode": true,
67+
"NgDateOnlyFormControlEnabled": true
6768
},
6869

6970
{

Fonlow.Poco2TsCore/Poco2TsGen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void CreateMemberDocComment(MemberInfo memberInfo, Type memberType, string docFl
201201
}
202202
}
203203

204-
static void AddDocComments(CodeCommentStatementCollection comments, docMember dm, IList<string> extra)
204+
static void AddDocComments(CodeCommentStatementCollection comments, docMember dm, List<string> extra)
205205
{
206206
if (dm != null && dm.summary != null)
207207
{

HeroesDemo/src/clientapi/WebApiCoreNG2FormGroupClientAuto.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable, Inject } from '@angular/core';
22
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { FormControl, FormGroup, Validators } from '@angular/forms';
5+
56
function CreateDateOnlyFormControl(){
67
const fc = new FormControl<any | null | undefined>(undefined);
78
fc.valueChanges.subscribe(v=>{
@@ -98,8 +99,8 @@ export namespace DemoWebApi_Controllers_Client {
9899
}
99100
export function CreateHeroFormGroup() {
100101
return new FormGroup<HeroFormProperties>({
101-
death: new FormControl<Date | null | undefined>(undefined),
102-
dob: new FormControl<Date | null | undefined>(undefined),
102+
death: CreateDateOnlyFormControl(),
103+
dob: CreateDateOnlyFormControl(),
103104
emailAddress: new FormControl<string | null | undefined>(undefined, [Validators.email]),
104105
id: new FormControl<string | null | undefined>(undefined, [Validators.pattern('/^-?\d{0,19}$/')]),
105106
name: new FormControl<string | null | undefined>(undefined, [Validators.required, Validators.minLength(2), Validators.maxLength(120)]),
@@ -116,8 +117,8 @@ export namespace DemoWebApi_Controllers_Client {
116117
}
117118
export function CreateSuperHeroFormGroup() {
118119
return new FormGroup<SuperHeroFormProperties>({
119-
death: new FormControl<Date | null | undefined>(undefined),
120-
dob: new FormControl<Date | null | undefined>(undefined),
120+
death: CreateDateOnlyFormControl(),
121+
dob: CreateDateOnlyFormControl(),
121122
emailAddress: new FormControl<string | null | undefined>(undefined, [Validators.email]),
122123
id: new FormControl<string | null | undefined>(undefined, [Validators.pattern('/^-?\d{0,19}$/')]),
123124
name: new FormControl<string | null | undefined>(undefined, [Validators.required, Validators.minLength(2), Validators.maxLength(120)]),
@@ -1904,7 +1905,7 @@ export namespace DemoWebApi_DemoData_Client {
19041905
business_no: new FormControl<string | null | undefined>(undefined),
19051906
businessNumberType: new FormControl<string | null | undefined>(undefined),
19061907
foundDate: new FormControl<Date | null | undefined>(undefined),
1907-
registerDate: new FormControl<Date | null | undefined>(undefined),
1908+
registerDate: CreateDateOnlyFormControl(),
19081909
});
19091910

19101911
}
@@ -2159,7 +2160,7 @@ export namespace DemoWebApi_DemoData_Client {
21592160
name: new FormControl<string | null | undefined>(undefined, [Validators.required, Validators.minLength(2), Validators.maxLength(255)]),
21602161
web: new FormControl<string | null | undefined>(undefined, [Validators.pattern('https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)')]),
21612162
baptised: new FormControl<Date | null | undefined>(undefined),
2162-
dob: new FormControl<Date | null | undefined>(undefined),
2163+
dob: CreateDateOnlyFormControl(),
21632164
givenName: new FormControl<string | null | undefined>(undefined),
21642165
surname: new FormControl<string | null | undefined>(undefined),
21652166
});

WebApiClientGenCore.Abstract/ClientApiTsFunctionGenAbstract.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public CodeMemberMethod CreateApiFunction(WebApiDescription description, IPoco2C
101101
RenderImplementation();
102102
break;
103103
default:
104-
Console.Error.WriteLine("This HTTP method {0} is not yet supported", description.HttpMethod);
104+
Console.Error.WriteLine("This HTTP method {0} is not yet supported", description.HttpMethod);
105105
break;
106106
}
107107

@@ -286,7 +286,7 @@ void CreateParamDocComment(StringBuilder builder, ParameterDescription paramDesc
286286
/// </summary>
287287
/// <param name="lines"></param>
288288
/// <returns></returns>
289-
static string LinesToIndentedLines(IList<string> lines)
289+
static string LinesToIndentedLines(List<string> lines)
290290
{
291291
if (lines == null || lines.Count == 0)
292292
{

WebApiClientGenCore.Abstract/CodeGenParameters.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ public class JSPlugin
286286
/// The returned types and parameters may be null. And some primitive types in data model / interface may be null.
287287
/// </summary>
288288
public bool HelpStrictMode { get; set; }
289+
290+
/// <summary>
291+
/// For Angular reactive form control binding with DateOnly, requiring yyyy-MM-dd local date, while the strongly typed field is JavaScript Date.
292+
/// </summary>
293+
public bool NgDateOnlyFormControlEnabled { get; set; }
289294
}
290295

291296
/// <summary>
@@ -327,5 +332,8 @@ public class JSOutput : CodeGenSettingsBase
327332
public bool MaybeNullAttributeOnMethod { get; set; }
328333

329334
public bool MethodSuffixWithClrTypeName { get; set; }
335+
336+
public bool NgDateOnlyFormControlEnabled { get; set; }
337+
330338
}
331339
}

WebApiClientGenCore.Abstract/WebApiClientGenCore.Abstract.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<Copyright>Copyright © Zijian Huang 2015-$([System.DateTime]::Now.Year)</Copyright>
99
<Authors>Zijian Huang</Authors>
1010
<PackageProjectUrl>https://github.com/zijianhuang/webapiclientgen</PackageProjectUrl>
11-
<Version>3.5</Version>
11+
<Version>3.5.1</Version>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
13-
<PackageReleaseNotes>.NET 9</PackageReleaseNotes>
13+
<PackageReleaseNotes>NgDateOnlyFormControlEnabled</PackageReleaseNotes>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<NoWarn>1701;1702;IDE0057</NoWarn>
1616
<AnalysisLevel>latest-all</AnalysisLevel>

WebApiClientGenCore.NG2FormGroup/CodeObjectHelperForNg2FormGroup.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ namespace Fonlow.TypeScriptCodeDom
1515
public class CodeObjectHelperForNg2FormGroup : CodeObjectHelper
1616
{
1717
readonly CodeNamespaceCollection codeNamespaceCollection;
18+
readonly bool careForDateOnly;
1819

19-
public CodeObjectHelperForNg2FormGroup(CodeNamespaceCollection codeNamespaceCollection) : base(true)
20+
public CodeObjectHelperForNg2FormGroup(CodeNamespaceCollection codeNamespaceCollection, bool careForDateOnly=false) : base(true)
2021
{
2122
this.codeNamespaceCollection = codeNamespaceCollection;
23+
this.careForDateOnly = careForDateOnly;
2224
}
2325

2426
/// <summary>
@@ -228,6 +230,7 @@ string GetCodeMemberFieldTextForAngularFormGroup(CodeMemberField codeMemberField
228230
{
229231
Attribute[] customAttributes = codeMemberField.UserData[UserDataKeys.CustomAttributes] as Attribute[];
230232
string fieldName = codeMemberField.Name.EndsWith('?') ? codeMemberField.Name.Substring(0, codeMemberField.Name.Length - 1) : codeMemberField.Name;
233+
FieldTypeInfo fieldTypeInfo = codeMemberField.Type.UserData[UserDataKeys.FieldTypeInfo] as FieldTypeInfo;
231234

232235
if (customAttributes?.Length > 0)
233236
{
@@ -309,7 +312,8 @@ string GetCodeMemberFieldTextForAngularFormGroup(CodeMemberField codeMemberField
309312
}
310313
}
311314

312-
FieldTypeInfo fieldTypeInfo = codeMemberField.Type.UserData[UserDataKeys.FieldTypeInfo] as FieldTypeInfo;
315+
bool isFieldDateOnly = false;
316+
313317
if (fieldTypeInfo != null)
314318
{
315319
bool validatorsHasValidatorMinOrMax = validatorList.Exists(d => d.Contains("max(") || d.Contains("min"));
@@ -321,15 +325,29 @@ string GetCodeMemberFieldTextForAngularFormGroup(CodeMemberField codeMemberField
321325
}
322326
}
323327

324-
if (integralJsStringValidatorsDic.TryGetValue(fieldTypeInfo.ClrType.FullName, out string integralJsStringValidators)){
328+
if (integralJsStringValidatorsDic.TryGetValue(fieldTypeInfo.ClrType.FullName, out string integralJsStringValidators))
329+
{
325330
validatorList.Add(integralJsStringValidators);
326331
}
332+
333+
if (fieldTypeInfo.ClrType == typeof(DateOnly) || fieldTypeInfo.ClrType == typeof(DateOnly?))
334+
{
335+
isFieldDateOnly = true;
336+
}
327337
}
328338

329339
string text = String.Join(", ", validatorList);
330340
string tsTypeName = RefineAngularFormControlTypeName(codeMemberField);
331-
return string.IsNullOrEmpty(text) ? $"{fieldName}: new FormControl<{tsTypeName}>(undefined)" :
332-
$"{fieldName}: new FormControl<{tsTypeName}>(undefined, [{text}])";
341+
342+
if (isFieldDateOnly && careForDateOnly)
343+
{
344+
return $"{fieldName}: CreateDateOnlyFormControl()";
345+
}
346+
else
347+
{
348+
return string.IsNullOrEmpty(text) ? $"{fieldName}: new FormControl<{tsTypeName}>(undefined)" :
349+
$"{fieldName}: new FormControl<{tsTypeName}>(undefined, [{text}])";
350+
}
333351
}
334352
else
335353
{

WebApiClientGenCore.NG2FormGroup/ControllersTsNG2FormGroupClientApiGen.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ControllersTsNG2FormGroupClientApiGen : ControllersTsNG2ClientApiGe
1313
/// </summary>
1414
/// <param name="jsOutput"></param>
1515
/// <remarks>The client data types should better be generated through SvcUtil.exe with the DC option. The client namespace will then be the original namespace plus suffix ".client". </remarks>
16-
public ControllersTsNG2FormGroupClientApiGen(JSOutput jsOutput, bool handleHttpRequestHeaders, Fonlow.Poco2Client.IDocCommentTranslate docCommentTranslate)
16+
public ControllersTsNG2FormGroupClientApiGen(JSOutput jsOutput, bool handleHttpRequestHeaders, Fonlow.Poco2Client.IDocCommentTranslate docCommentTranslate)
1717
: base(jsOutput, handleHttpRequestHeaders, docCommentTranslate)
1818
{
1919
CreatePoco2TsGen(jsOutput.ClientNamespaceSuffix);
@@ -27,7 +27,7 @@ protected override void CreatePoco2TsGen(string clientNamespaceSuffix)
2727

2828
protected override CodeObjectHelper CreateCodeObjectHelper(bool asModule)
2929
{
30-
return new CodeObjectHelperForNg2FormGroup(TargetUnit.Namespaces);
30+
return new CodeObjectHelperForNg2FormGroup(TargetUnit.Namespaces, jsOutput.NgDateOnlyFormControlEnabled);
3131
}
3232

3333
protected override void AddBasicReferences()
@@ -37,7 +37,10 @@ protected override void AddBasicReferences()
3737
TargetUnit.ReferencedAssemblies.Add("import { Observable } from 'rxjs';");
3838
TargetUnit.ReferencedAssemblies.Add("import { FormControl, FormGroup, Validators } from '@angular/forms';");
3939

40-
TargetUnit.ReferencedAssemblies.Add(@"function CreateDateOnlyFormControl(){
40+
if (jsOutput.NgDateOnlyFormControlEnabled)
41+
{
42+
TargetUnit.ReferencedAssemblies.Add(@"
43+
function CreateDateOnlyFormControl(){
4144
const fc = new FormControl<any | null | undefined>(undefined);
4245
fc.valueChanges.subscribe(v=>{
4346
if (v){
@@ -48,6 +51,7 @@ protected override void AddBasicReferences()
4851
return fc;
4952
}
5053
");
54+
}
5155
}
5256
}
5357

WebApiClientGenCore.NG2FormGroup/WebApiClientGenCore.NG2FormGroup.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
<PackageId>Fonlow.WebApiClientGenCore.NG2FormGroup</PackageId>
66
<Authors>Zijian Huang</Authors>
77
<Description>Plugin for WebApiClientGen 3.0+ to generate client API codes with Angular 6+, plus helper interfaces and functions for creating FormGroup of Angular Reactive Forms.</Description>
8-
<PackageReleaseNotes>.NET 9</PackageReleaseNotes>
8+
<PackageReleaseNotes>NgDateOnlyFormControlEnabled</PackageReleaseNotes>
99
<NeutralLanguage>en</NeutralLanguage>
1010
<PackageProjectUrl>https://github.com/zijianhuang/webapiclientgen</PackageProjectUrl>
1111
<Copyright>Copyright © Zijian Huang 2015-$([System.DateTime]::Now.Year)</Copyright>
1212
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1414
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1515
<AssemblyName>Fonlow.WebApiClientGenCore.NG2FormGroup</AssemblyName>
16-
<Version>1.9</Version>
16+
<Version>1.9.1</Version>
1717
<PackageTags>Angular</PackageTags>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<RepositoryUrl>https://github.com/zijianhuang/webapiclientgen</RepositoryUrl>

WebApiClientGenCore/CodeGen.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static void GenerateClientAPIs(string webRootPath, CodeGenSettings settin
5858
ContainerNameSuffix = plugin.ContainerNameSuffix,
5959
DataAnnotationsToComments = plugin.DataAnnotationsToComments,
6060
HelpStrictMode = plugin.HelpStrictMode,
61+
NgDateOnlyFormControlEnabled = plugin.NgDateOnlyFormControlEnabled,
6162
MethodSuffixWithClrTypeName = settings.ClientApiOutputs.JsMethodSuffixWithClrTypeName,
6263
NotNullAttributeOnMethod = settings.ClientApiOutputs.NotNullAttributeOnMethod,
6364
MaybeNullAttributeOnMethod = settings.ClientApiOutputs.MaybeNullAttributeOnMethod,

WebApiClientGenCore/WebApiClientGenCore.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
<RootNamespace>Fonlow.CodeDom.Web</RootNamespace>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<PackageId>Fonlow.WebApiClientGenCore</PackageId>
8-
<Version>7.9.1</Version>
8+
<Version>7.9.2</Version>
99
<Description>Generate strongly typed client API in C# or TypeScript for .NET Core Web API. Strongly Typed Client API Generator generates C# and TypeScript codes of strongly typed client API similar to what generated by SvcUtil.exe of WCF, provided you have access to the ASP.NET Web API source codes or assemblies. Then you may provide or publish either the source codes in C# and TypeScript or the compiled client API library to other developers who are developing client programs of your Web API. The TypeScript outputs support jQuery, Angular 2+, AXIOS and Aurelia.</Description>
1010
<Copyright>Copyright © Zijian Huang 2015-$([System.DateTime]::Now.Year)</Copyright>
1111
<PackageLicenseUrl></PackageLicenseUrl>
1212
<PackageProjectUrl>https://github.com/zijianhuang/webapiclientgen</PackageProjectUrl>
1313
<PackageTags>ASP API Web Client webapi rest restful aspnet aspnetwebapi httpclient .net core aspnetcore jquery angular</PackageTags>
1414
<PackageReleaseNotes>
15-
Generated codes for data model assemblies not referenced in API prototypes.
15+
NgDateOnlyFormControlEnabled.
1616
</PackageReleaseNotes>
1717
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1818
<NeutralLanguage>en</NeutralLanguage>

0 commit comments

Comments
 (0)