From 16d5d119cfce585ad76749fdf34d8c6dbdeae15d Mon Sep 17 00:00:00 2001 From: Bjarne Callewaert Date: Wed, 13 Oct 2021 16:17:52 +0200 Subject: [PATCH] Add logic for date and datetimes from the body to the request url #1613 --- .../Models/TypeScriptParameterModel.cs | 51 +++++++++++++++++++ .../Templates/Client.RequestUrl.liquid | 6 +-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptParameterModel.cs b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptParameterModel.cs index fdf8bee2db..4df9edd217 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptParameterModel.cs +++ b/src/NSwag.CodeGeneration.TypeScript/Models/TypeScriptParameterModel.cs @@ -6,8 +6,11 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using System; using System.Collections.Generic; +using NJsonSchema; using NJsonSchema.CodeGeneration; +using NJsonSchema.CodeGeneration.TypeScript; using NSwag.CodeGeneration.Models; namespace NSwag.CodeGeneration.TypeScript.Models @@ -16,6 +19,7 @@ namespace NSwag.CodeGeneration.TypeScript.Models public class TypeScriptParameterModel : ParameterModelBase { private readonly TypeScriptClientGeneratorSettings _settings; + private readonly JsonSchema _schema; /// Initializes a new instance of the class. /// Name of the parameter. @@ -38,6 +42,7 @@ public TypeScriptParameterModel( : base(parameterName, variableName, typeName, parameter, allParameters, settings.TypeScriptGeneratorSettings, generator, typeResolver) { _settings = settings; + _schema = parameter.Schema; } /// Gets the type postfix (e.g. ' | null | undefined') @@ -55,5 +60,51 @@ public string TypePostfix } } } + + /// + /// Format the datetime to a string based on the chosen datetime type setting + /// + public string GetDateTimeToString + { + get + { + switch (_settings.TypeScriptGeneratorSettings.DateTimeType) + { + case TypeScriptDateTimeType.Date: + return "toISOString()"; + + case TypeScriptDateTimeType.MomentJS: + case TypeScriptDateTimeType.OffsetMomentJS: + if (_schema.Format == JsonFormatStrings.TimeSpan) + { + return "format('d.hh:mm:ss.SS', { trim: false })"; + } + + if (_settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS) + { + return "toISOString(true)"; + } + return "toISOString()"; + + case TypeScriptDateTimeType.String: + return ""; + + case TypeScriptDateTimeType.Luxon: + return "toString()"; + + case TypeScriptDateTimeType.DayJS: + if (_schema.Format == JsonFormatStrings.TimeSpan) + { + return "format('d.hh:mm:ss.SSS')"; + } + + return "toISOString()"; + + default: + throw new ArgumentOutOfRangeException(); + } + } + } + } } diff --git a/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestUrl.liquid b/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestUrl.liquid index 984c2f52ca..7fbe7135b8 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestUrl.liquid +++ b/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestUrl.liquid @@ -7,9 +7,9 @@ if ({{ parameter.VariableName }} === undefined || {{ parameter.VariableName }} = if ({{ parameter.VariableName }} !== null && {{ parameter.VariableName }} !== undefined) {% endif -%} {% if parameter.IsDateOrDateTimeArray -%} -url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }}.map(s_ => s_ ? s_.toJSON() : "null").join())); +url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }}.map(s_ => s_ ? s_.{{ parameter.GetDateTimeToString }} : "null").join())); {% elseif parameter.IsDateOrDateTime -%} -url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.toJSON() : "null")); +url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.{{ parameter.GetDateTimeToString }} : "null")); {% elseif parameter.IsArray -%} url_ = url_.replace("{{ "{" }}{{ parameter.Name }}}", encodeURIComponent({{ parameter.VariableName }}.join())); {% else -%} @@ -50,7 +50,7 @@ else if ({{ parameter.VariableName }} !== undefined) } }); {% elseif parameter.IsDateOrDateTime -%} - url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.toJSON() : "{{ QueryNullValue }}") + "&"; + url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.{{ parameter.GetDateTimeToString }} : "{{ QueryNullValue }}") + "&"; {% elseif parameter.IsArray -%} {{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + "&"; }); {% else -%}