You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TL;DR
I think i found a bug in the c# client code generator that occurs when a string is used with format: uri and causes to use the Uri type but uses new StringContent(string) on request creation.
I was handed an OpenAPI specification that defines endpoints that take plaintext inputs of type string with format uri.
As request body:
requestBody:
content:
text/plain:
schema:
type: stringformat: uri
and there's also an endpoint that defines parameters like this:
parameters:
- name: urlin: queryschema:
type: stringformat: uri
The C# code generator generates the following client code:
var content_ = new System.Net.Http.StringContent({{ operation.ContentParameter.VariableName }});
operation.HasPlainTextBodyParameter is true in this case, as the content type is text/plain. But even if you'd set the content type to something that fits to Uri, such as text/uri-list, the generator will serialize it to json, which is not correct anyway.
If we remove format: uri the code generator will use a string rather than an Uri, so we don't run into the problem. But that would also require me to alter the specification file everytime there's an update.
This seems like a bug to me anyway
The text was updated successfully, but these errors were encountered:
fortender
changed the title
Plaintext parameter/content with uri format
Plaintext parameter/content with uri format causes errors in client code
May 27, 2024
TL;DR
I think i found a bug in the c# client code generator that occurs when a string is used with
format: uri
and causes to use theUri
type but usesnew StringContent(string)
on request creation.I was handed an OpenAPI specification that defines endpoints that take plaintext inputs of type
string
with formaturi
.As request body:
and there's also an endpoint that defines parameters like this:
The C# code generator generates the following client code:
This fails of course, as
body
is neither a string, nor is there a constructor ofStringContent
that takes anUri
.The actual problem is caused by the
Client.Class.liquid
template:NSwag/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Lines 215 to 216 in b3615b4
operation.HasPlainTextBodyParameter
istrue
in this case, as the content type istext/plain
. But even if you'd set the content type to something that fits toUri
, such astext/uri-list
, the generator will serialize it to json, which is not correct anyway.If we remove
format: uri
the code generator will use astring
rather than anUri
, so we don't run into the problem. But that would also require me to alter the specification file everytime there's an update.This seems like a bug to me anyway
The text was updated successfully, but these errors were encountered: