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

Plaintext parameter/content with uri format causes errors in client code #4903

Open
fortender opened this issue May 27, 2024 · 0 comments
Open

Comments

@fortender
Copy link

fortender commented 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 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: string
              format: uri

and there's also an endpoint that defines parameters like this:

      parameters:
        - name: url
          in: query
          schema:
            type: string
            format: uri

The C# code generator generates the following client code:

public virtual async System.Threading.Tasks.Task RegisterReceiveCallbackAsync(MessageType? messageType, System.Uri body, System.Threading.CancellationToken cancellationToken)
{
    var client_ = _httpClient;
    var disposeClient_ = false;
    try
    {
        using (var request_ = new System.Net.Http.HttpRequestMessage())
        {
            var content_ = new System.Net.Http.StringContent(body); // Error here
            // ...
        }
        // ...
    }
    // ...
}

This fails of course, as body is neither a string, nor is there a constructor of StringContent that takes an Uri.

The actual problem is caused by the Client.Class.liquid template:

{% elsif operation.HasPlainTextBodyParameter -%}
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

@fortender fortender changed the title Plaintext parameter/content with uri format Plaintext parameter/content with uri format causes errors in client code May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant