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

WIP: Array parameters in URL (#3529) #3980

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 @@ -40,21 +40,51 @@ if ({{ parameter.VariableName }} === null)
else if ({{ parameter.VariableName }} !== undefined)
{% endif -%}
{% endif -%}
{% if parameter.IsDateOrDateTimeArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach(item_ => { url_ += "{{ parameter.Name }}=" + encodeURIComponent(item_ ? "" + item_.{{ parameter.GetDateTimeToString }} : "null") + "&"; });
{% elsif parameter.IsObjectArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach((item, index) => {
for (let attr in item)
if (item.hasOwnProperty(attr)) {
url_ += "{{ parameter.Name }}[" + index + "]." + attr + "=" + encodeURIComponent("" + (item as any)[attr]) + "&";
}
});
{% elsif parameter.IsDateOrDateTime -%}
url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.{{ parameter.GetDateTimeToString }} : "{{ QueryNullValue }}") + "&";
{% elsif parameter.IsArray -%}
{{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + "&"; });
{% if parameter.IsArray -%}
{% if parameter.IsForm or parameter.IsSimple -%}
if ({{ parameter.VariableName }}.length)
{
url_ += "{{ parameter.Name }}=";
{{ parameter.VariableName }}.forEach(item => { url_ += encodeURIComponent("" + item) + ","; });
url_ = url_.replace(/,$/, "");
}
{% elsif parameter.IsLabel -%}
if ({{ parameter.VariableName }}.length)
{
url_ += "{{ parameter.Name }}=";
{{ parameter.VariableName }}.forEach(item => { url_ += "." encodeURIComponent("" + item); });
}
{% elsif parameter.IsMatrix -%}
{
{{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + ";"; });
url_ = url_.replace(/;$/, "");
}
{% elsif parameter.IsSpaceDelimeted -%}
{
{{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + " "; });
url_ = url_.replace(/ $/, "");
}
{% elsif parameter.IsPipeDelimited -%}
{
{{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + "|"; });
url_ = url_.replace(/|$/, "");
}
{% elsif parameter.IsDeepObject -%}
{{ parameter.VariableName }}.forEach((item, index) => {
for (let attr in item)
if (item.hasOwnProperty(attr)) {
url_ += "{{ parameter.Name }}[" + index + "]." + attr + "=" + encodeURIComponent("" + (item as any)[attr]) + "&";
}
});
{% else -%}
{{ parameter.VariableName }}.forEach(item => { url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + item) + "&"; });
{% endif -%}
{% else -%}
{% if parameter.IsDateOrDateTime -%}
url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }} ? "" + {{ parameter.VariableName }}.{{ parameter.GetDateTimeToString }} : "{{ QueryNullValue }}") + "&";
{% else -%}
url_ += "{{ parameter.Name }}=" + encodeURIComponent("" + {{ parameter.VariableName }}) + "&";
{% endif -%}
{% endif -%}
{% endfor -%}
url_ = url_.replace(/[?&]$/, "");
url_ = url_.replace(/[?&]$/, "");
17 changes: 16 additions & 1 deletion src/NSwag.CodeGeneration/Models/ParameterModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,24 @@ public string Default
/// <summary>Gets a value indicating whether the parameter is a deep object (OpenAPI 3).</summary>
public bool IsDeepObject => _parameter.Style == OpenApiParameterStyle.DeepObject;

/// <summary>Gets a value indicating whether the parameter has form style.</summary>
/// <summary>Gets a value indicating whether the parameter has 'form' style.</summary>
public bool IsForm => _parameter.Style == OpenApiParameterStyle.Form;

/// <summary>Gets a value indicating whether the parameter has 'simple' style.</summary>
public bool IsSimple => _parameter.Style == OpenApiParameterStyle.Simple;

/// <summary>Gets a value indicating whether the parameter has 'label' style.</summary>
public bool IsLabel => _parameter.Style == OpenApiParameterStyle.Label;

/// <summary>Gets a value indicating whether the parameter has 'matrix' style.</summary>
public bool IsMatrix => _parameter.Style == OpenApiParameterStyle.Matrix;

/// <summary>Gets a value indicating whether the parameter has 'spaceDelimited' style.</summary>
public bool IsSpaceDelimited => _parameter.Style == OpenApiParameterStyle.SpaceDelimeted;

/// <summary>Gets a value indicating whether the parameter has 'pipeDelimited' style.</summary>
public bool IsPipeDelimited => _parameter.Style == OpenApiParameterStyle.PipeDelimited;

/// <summary>Gets the contained value property names (OpenAPI 3).</summary>
public IEnumerable<PropertyModel> PropertyNames
{
Expand Down