Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for date and datetimes from the body to the request url #1613 #3660

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using NJsonSchema;
using NJsonSchema.CodeGeneration;
using NJsonSchema.CodeGeneration.TypeScript;
using NSwag.CodeGeneration.Models;

namespace NSwag.CodeGeneration.TypeScript.Models
Expand All @@ -16,6 +19,7 @@ namespace NSwag.CodeGeneration.TypeScript.Models
public class TypeScriptParameterModel : ParameterModelBase
{
private readonly TypeScriptClientGeneratorSettings _settings;
private readonly JsonSchema _schema;

/// <summary>Initializes a new instance of the <see cref="TypeScriptParameterModel" /> class.</summary>
/// <param name="parameterName">Name of the parameter.</param>
Expand All @@ -38,6 +42,7 @@ public TypeScriptParameterModel(
: base(parameterName, variableName, typeName, parameter, allParameters, settings.TypeScriptGeneratorSettings, generator, typeResolver)
{
_settings = settings;
_schema = parameter.Schema;
}

/// <summary>Gets the type postfix (e.g. ' | null | undefined')</summary>
Expand All @@ -55,5 +60,51 @@ public string TypePostfix
}
}
}

/// <summary>
/// Format the datetime to a string based on the chosen datetime type setting
/// </summary>
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();
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down Expand Up @@ -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 -%}
Expand Down