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

Fix for #3414 - C# generated client using FormUrlEncodedContent instead of StringContent #5029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
{% elsif operation.HasXmlBodyParameter -%}
var content_ = new System.Net.Http.StringContent({{ operation.ContentParameter.VariableName }});
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("{{ operation.Consumes }}");
{% elsif operation.ConsumesFormUrlEncoded -%}
{% elsif operation.ConsumesOnlyFormUrlEncoded -%}
var json_ = {% if UseSystemTextJson %}System.Text.Json.JsonSerializer.SerializeToUtf8Bytes{% else %}Newtonsoft.Json.JsonConvert.SerializeObject{% endif %}({{ operation.ContentParameter.VariableName }}, JsonSerializerSettings);
var dictionary_ = {% if UseSystemTextJson %}System.Text.Json.JsonSerializer.Deserialize{% else %}Newtonsoft.Json.JsonConvert.DeserializeObject{% endif %}<System.Collections.Generic.Dictionary<string, string>>(json_, JsonSerializerSettings);
var content_ = new System.Net.Http.FormUrlEncodedContent(dictionary_);
Expand All @@ -228,7 +228,7 @@
request_.Content = content_;
{% else -%}
{% if operation.HasFormParameters -%}
{% if operation.ConsumesFormUrlEncoded -%}
{% if operation.ConsumesOnlyFormUrlEncoded -%}
var keyValues_ = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>>();
{% for parameter in operation.FormParameters -%}
{% if parameter.IsNullable -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
{%- for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{%- endfor -%}
{%- if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
{%- if operation.HasContent or operation.ConsumesOnlyFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{%- endif -%}
{%- if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{%- for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{% endfor -%}
{% if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
{% if operation.HasContent or operation.ConsumesOnlyFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{% endif -%}
{% if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{% for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{% endfor -%}
{% if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
{% if operation.HasContent or operation.ConsumesOnlyFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{% endif -%}
{% if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{%- if operation.HasFormParameters -%}
{%- if operation.ConsumesFormUrlEncoded -%}
{%- if operation.ConsumesOnlyFormUrlEncoded -%}
let content_ = "";
{%- for parameter in operation.FormParameters -%}
{%- if parameter.IsRequired -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{%- for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{%- endfor -%}
{%- if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
{%- if operation.HasContent or operation.ConsumesOnlyFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{%- endif -%}
{%- if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{% for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{% endfor -%}
{% if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
{% if operation.HasContent or operation.ConsumesOnlyFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{% endif -%}
{% if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
{%- for parameter in operation.HeaderParameters -%}
"{{ parameter.Name }}": {{ parameter.VariableName }} !== undefined && {{ parameter.VariableName }} !== null ? "" + {{ parameter.VariableName }} : "",
{% endfor -%}
{%- if operation.HasContent or operation.ConsumesFormUrlEncoded -%}
{%- if operation.HasContent or operation.ConsumesOnlyFormUrlEncoded -%}
"Content-Type": "{{ operation.Consumes }}",
{%- endif -%}
{%- if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
Expand Down
13 changes: 11 additions & 2 deletions src/NSwag.CodeGeneration/Models/OperationModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,19 @@ public TParameterModel ContentParameter
/// <summary>Gets a value indicating whether the operation has form parameters.</summary>
public bool HasFormParameters => Parameters.Any(p => p.Kind == OpenApiParameterKind.FormData);

/// <summary>Gets a value indicating whether the operation consumes 'application/x-www-form-urlencoded'.</summary>
public bool ConsumesOnlyFormUrlEncoded =>
ConsumesFormUrlEncoded && !ConsumesJson;

/// <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 ||
_operation.ActualRequestBody?.Content.Any(mt => mt.Key == "application/x-www-form-urlencoded") == true;
(_operation.ActualConsumes?.Any(c => c == "application/x-www-form-urlencoded") == true ||
_operation.ActualRequestBody?.Content.Any(mt => mt.Key == "application/x-www-form-urlencoded") == true);

/// <summary>Gets a value indicating whether the operation consumes 'application/json'.</summary>
public bool ConsumesJson =>
(_operation.ActualConsumes?.Any(c => c == "application/json") == true ||
_operation.ActualRequestBody?.Content.Any(mt => mt.Key == "application/json") == true);

/// <summary>Gets the form parameters.</summary>
public IEnumerable<TParameterModel> FormParameters => Parameters.Where(p => p.Kind == OpenApiParameterKind.FormData);
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.ConsoleCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"NSwag.ConsoleCore": {
"commandName": "Project",
"commandLineArgs": "run \"C:\\Data\\Projects\\Picturepark.SDK.TypeScript\\src\\picturepark-sdk-v1-angular\\projects\\picturepark-sdk-v1-angular\\nswag.json\""
"commandLineArgs": "run \"D:\\pfsense\\pfsense.nswag\""
}
}
}
4 changes: 2 additions & 2 deletions src/NSwag.Npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading