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. 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 { 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("}")); }