From a085f109f90759db174d86eef9884d79238c4eec Mon Sep 17 00:00:00 2001 From: Michael Baldwin Date: Fri, 2 Aug 2024 12:32:04 -0700 Subject: [PATCH 1/3] Support generating required fields as not optional in TS --- src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs | 2 +- .../JavaScriptGeneratorTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs b/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs index b87a317..ca64b75 100644 --- a/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs +++ b/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs @@ -930,7 +930,7 @@ private static void WriteDto(CodeWriter code, ServiceDtoInfo dtoInfo, ServiceInf { code.WriteLineSkipOnce(); WriteJsDoc(code, fieldInfo); - code.WriteLine($"{fieldInfo.Name}?: {RenderFieldType(service.GetFieldType(fieldInfo)!)};"); + code.WriteLine($"{fieldInfo.Name}{(fieldInfo.IsRequired ? "" : "?")}: {RenderFieldType(service.GetFieldType(fieldInfo)!)};"); } } } diff --git a/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs b/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs index 279203e..c68c861 100644 --- a/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs +++ b/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs @@ -344,8 +344,8 @@ public void GenerateExampleApiTypeScript_DataPropertiesRequired() var typesFile = result.Files.Single(f => f.Name == "testApiTypes.ts"); Assert.That(typesFile.Text, Does.Contain("export interface IWidget {")); - Assert.That(typesFile.Text, Does.Contain("id?: string;")); - Assert.That(typesFile.Text, Does.Contain("name?: string;")); + Assert.That(typesFile.Text, Does.Contain("id: string;")); + Assert.That(typesFile.Text, Does.Contain("name: string;")); Assert.That(typesFile.Text, Does.Contain("price?: number;")); Assert.That(typesFile.Text, Does.Contain("}")); } From ff4f6ea2a8c5a4afa1b7119c92ab29bc7e4cd898 Mon Sep 17 00:00:00 2001 From: Michael Baldwin Date: Fri, 2 Aug 2024 12:53:44 -0700 Subject: [PATCH 2/3] Run codegen since required now is not optional --- conformance/src/conformanceApiTypes.ts | 12 ++++++------ conformance/src/fastify/conformanceApiPlugin.ts | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/conformance/src/conformanceApiTypes.ts b/conformance/src/conformanceApiTypes.ts index c321db2..9fbcc8f 100644 --- a/conformance/src/conformanceApiTypes.ts +++ b/conformance/src/conformanceApiTypes.ts @@ -88,7 +88,7 @@ export interface ICreateWidgetResponse { /** Request for GetWidget. */ export interface IGetWidgetRequest { /** The widget ID. */ - id?: number; + id: number; /** Don't get the widget if it has this ETag. */ ifNotETag?: string; @@ -127,7 +127,7 @@ export interface IDeleteWidgetResponse { /** Request for GetWidgetBatch. */ export interface IGetWidgetBatchRequest { /** The IDs of the widgets to return. */ - ids?: number[]; + ids: number[]; } /** Response for GetWidgetBatch. */ @@ -258,9 +258,9 @@ export interface IMixedResponse { /** Request for Required. */ export interface IRequiredRequest { - query?: string; + query: string; - normal?: string; + normal: string; widget?: IWidget; @@ -281,7 +281,7 @@ export interface IRequiredRequest { /** Response for Required. */ export interface IRequiredResponse { - normal?: string; + normal: string; } /** Request for MirrorBytes. */ @@ -328,7 +328,7 @@ export interface IWidget { id?: number; /** The name of the widget. */ - name?: string; + name: string; } export interface IAny { diff --git a/conformance/src/fastify/conformanceApiPlugin.ts b/conformance/src/fastify/conformanceApiPlugin.ts index 21144f0..79d2ee9 100644 --- a/conformance/src/fastify/conformanceApiPlugin.ts +++ b/conformance/src/fastify/conformanceApiPlugin.ts @@ -905,7 +905,7 @@ export interface ICreateWidgetResponse { /** Request for GetWidget. */ export interface IGetWidgetRequest { /** The widget ID. */ - id?: number; + id: number; /** Don't get the widget if it has this ETag. */ ifNotETag?: string; @@ -944,7 +944,7 @@ export interface IDeleteWidgetResponse { /** Request for GetWidgetBatch. */ export interface IGetWidgetBatchRequest { /** The IDs of the widgets to return. */ - ids?: number[]; + ids: number[]; } /** Response for GetWidgetBatch. */ @@ -1075,9 +1075,9 @@ export interface IMixedResponse { /** Request for Required. */ export interface IRequiredRequest { - query?: string; + query: string; - normal?: string; + normal: string; widget?: IWidget; @@ -1098,7 +1098,7 @@ export interface IRequiredRequest { /** Response for Required. */ export interface IRequiredResponse { - normal?: string; + normal: string; } /** Request for MirrorBytes. */ @@ -1145,7 +1145,7 @@ export interface IWidget { id?: number; /** The name of the widget. */ - name?: string; + name: string; } export interface IAny { From 499aa473725866cd707cf2e1de6336578f898371 Mon Sep 17 00:00:00 2001 From: Michael Baldwin Date: Mon, 5 Aug 2024 07:39:34 -0700 Subject: [PATCH 3/3] Update version number and release notes per contributing docs since generating required fields as not optional is a breaking change --- Directory.Build.props | 2 +- ReleaseNotes.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 248d438..0faaf43 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 2.11.1 + 3.0.0 2.8.0 12.0 enable diff --git a/ReleaseNotes.md b/ReleaseNotes.md index c60f428..75597e3 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -2,6 +2,10 @@ These are the NuGet package releases. See also [npm Release Notes](ReleaseNotesNpm.md). +## 3.0.0 + +* Support required fields as not optional in TypeScript. + ## 2.11.1 * Ignore events for now.