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 support for x-www-form-urlencoded (CSharp) #1739

Merged
merged 5 commits into from
Nov 15, 2018
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
45 changes: 45 additions & 0 deletions src/NSwag.CodeGeneration.CSharp.Tests/FormParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,51 @@ public async Task When_action_has_file_parameter_then_Stream_is_generated_in_CSh
Assert.Contains("content_.Add(new System.Net.Http.StreamContent(file.Data), \"file\"", code);
}

[Fact]
public void When_form_parameters_are_defined_then_FormUrlEncodedContent_is_generated()
{
//// Arrange
var document = new SwaggerDocument();
document.Paths["foo/bar"] = new SwaggerPathItem
{
{
SwaggerOperationMethod.Post,
new SwaggerOperation
{
Consumes = new System.Collections.Generic.List<string> { "application/x-www-form-urlencoded" },
Parameters =
{
new SwaggerParameter
{
Name = "foo",
IsRequired = false,
IsNullableRaw = true,
Kind = SwaggerParameterKind.FormData,
Type = JsonObjectType.String
},
new SwaggerParameter
{
Name = "bar",
IsRequired = true,
IsNullableRaw = false,
Kind = SwaggerParameterKind.FormData,
Type = JsonObjectType.String
}
}
}
}
};

//// Act
var generator = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings());
var code = generator.GenerateFile();

//// Assert
Assert.Contains("new System.Net.Http.FormUrlEncodedContent", code);
Assert.Contains("if (foo != null)", code);
Assert.Contains("throw new System.ArgumentNullException(\"bar\");", code);
}

// TODO: Implement for JQuery, AngularJS and Angular 2

//[Fact]
Expand Down
37 changes: 26 additions & 11 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,44 @@
request_.Content = content_;
{% else -%}
{% if operation.HasFormParameters -%}
{% if operation.ConsumesFormUrlEncoded -%}
var keyValues_ = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>>();
{% for parameter in operation.FormParameters -%}
{% if parameter.IsNullable -%}
if ({{ parameter.VariableName }} != null)
{% else -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
else
{% endif -%}
keyValues_.Add(new System.Collections.Generic.KeyValuePair<string, string>("{{ parameter.Name }}", ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture)));
{% endfor -%}
request_.Content = new System.Net.Http.FormUrlEncodedContent(keyValues_);
{% else -%}
var boundary_ = System.Guid.NewGuid().ToString();
var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);
content_.Headers.Remove("Content-Type");
content_.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary_);
{% for parameter in operation.FormParameters -%}
{% if parameter.IsNullable -%}
{% for parameter in operation.FormParameters -%}
{% if parameter.IsNullable -%}
if ({{ parameter.VariableName }} != null)
{% else -%}
{% else -%}
if ({{ parameter.VariableName }} == null)
throw new System.ArgumentNullException("{{ parameter.VariableName }}");
else
{% endif -%}
{% if parameter.IsFile -%}
{% if parameter.IsArray -%}
{% endif -%}
{% if parameter.IsFile -%}
{% if parameter.IsArray -%}
foreach (var item_ in {{ parameter.VariableName }}) { content_.Add(new System.Net.Http.StreamContent(item_.Data), "{{ parameter.Name }}", item_.FileName ?? "{{ parameter.Name }}"); }
{% else -%}
{% else -%}
content_.Add(new System.Net.Http.StreamContent({{ parameter.VariableName }}.Data), "{{ parameter.Name }}", {{ parameter.VariableName }}.FileName ?? "{{ parameter.Name }}");
{% endif -%}
{% else -%}
{% endif -%}
{% else -%}
content_.Add(new System.Net.Http.StringContent(ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture)), "{{ parameter.Name }}");
{% endif -%}
{% endfor -%}
{% endif -%}
{% endfor -%}
request_.Content = content_;
{% endif -%}
{% elseif operation.IsGetOrDeleteOrHead == false -%}
request_.Content = new System.Net.Http.StringContent(string.Empty, System.Text.Encoding.UTF8, "{{ operation.Produces }}");
{% endif -%}
Expand Down
3 changes: 3 additions & 0 deletions src/NSwag.CodeGeneration/Models/OperationModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ public TParameterModel ContentParameter
/// <summary>Gets a value indicating whether the operation has form parameters.</summary>
public bool HasFormParameters => _operation.ActualParameters.Any(p => p.Kind == SwaggerParameterKind.FormData);

/// <summary>Gets a value indicating whether the operation consumes 'application/x-www-form-urlencoded'.</summary>
public bool ConsumesFormUrlEncoded => _operation.ActualConsumes?.Any(c => c == "application/x-www-form-urlencoded") == true;

/// <summary>Gets the form parameters.</summary>
public IEnumerable<TParameterModel> FormParameters => Parameters.Where(p => p.Kind == SwaggerParameterKind.FormData);

Expand Down