Skip to content

[BUG] ApiClient using incorrect method signature for RestSharp 106.5.4 for csharp framework 4.5 #6532

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

Closed
5 of 6 tasks
jburns24 opened this issue Jun 3, 2020 · 5 comments
Closed
5 of 6 tasks

Comments

@jburns24
Copy link

jburns24 commented Jun 3, 2020

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Generated client does not compile with RestSharp verions 106.5.4.

In the generated readme I saw

I had another library that was dependent on RestSharp 106.5.4 so tried to use that version.

Trying to compile with that version of RestSharp failed with the following error

1>C:\git\sniper-regression\EbayApiRest.Order\Generated\Order\EbayApiRest.Order\Client\ApiClient.cs(137,51,137,69): error CS1503: Argument 2: cannot convert from 'System.Action<System.IO.Stream>' to 'byte[]'

The generated method that fails to compile

private RestRequest PrepareRequest(
    String path, RestSharp.Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
    Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
    Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
    String contentType)
{
    var request = new RestRequest(path, method);

    // add path parameter, if any
    foreach(var param in pathParams)
        request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment);

    // add header parameter, if any
    foreach(var param in headerParams)
        request.AddHeader(param.Key, param.Value);

    // add query parameter, if any
    foreach(var param in queryParams)
        request.AddQueryParameter(param.Key, param.Value);

    // add form parameter, if any
    foreach(var param in formParams)
        request.AddParameter(param.Key, param.Value);

    // add file parameter, if any
    foreach(var param in fileParams)
    {
        request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
    }

    if (postBody != null) // http body (model or byte[]) parameter
    {
        request.AddParameter(contentType, postBody, ParameterType.RequestBody);
    }

    return request;
}

The line request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType); fails to compile because in RestSharp (106.5.4) the method signature for this change.

The method signature from RestSharp that matches the closest is public IRestRequest AddFile(string name, Action<Stream> writer, string fileName, long contentLength, string contentType = null);. The generated method is just missing the contentLength paramater.

openapi-generator version

4.3.1

OpenAPI declaration file content or url

Contract I am generating from
https://developer.ebay.com/api-docs/master/buy/order/openapi/3/buy_order_v1_beta_oas3.json

This contract does not pass validation on the command line as it appears the contract is using a reserved word (basePath) in the servers array. Though this contract does pass validation on https://apitools.dev/swagger-parser/online/#

I have an open ticket with ebay to fix their contract as I was able to edit it locally and change the variable in the servers array to foo and it passed validation and generated. Though it also appears that if you skip validation (--skip-validate-spec) the generated code is still correct.

(for JSON code), so it becomes more readable. If it is longer than about ten lines,
please create a Gist (https://gist.github.com) or upload it somewhere else and
link it here.
-->

Command line used for generation

open-api-codegen-cli.jar

java -DapiTests=false -DmodelTests=false -jar .\open-api-codegen-cli.jar generate -i "https://developer.ebay.com/api-docs/master/buy/order/openapi/3/buy_order_v1_beta_oas3.json" -c ".\.swagger-config.json" -g csharp -o "Generated/Orders" --package-name="EbayApiRest.Order" --ignore-file-override=".\.swagger-codegen-ignore" --skip-validation-spec

swagger-config.json

{
  "sourceFolder": "",
  "targetFramework": "v4.5",
  "modelPropertyNaming": "PascalCase",
  "hideGenerationTimestamp": false,
  "optionalMethodArgument": false,
  "optionalAssemblyInfo": false,
  "optionalProjectFile": false,
  "nonPublicApi": false,
  "validatable": false
}

.swagger-codegen-ignore

**/.gitignore
**/.travis.yml
**/build.*
**/*.sh
Steps to reproduce

Generate Code
Add to Visual Studio .NET Framework 4.5 project
Add packagest Newtonsoft.json (7.0.0), JsonSubTypes (1.2.0), and RestSharp (106.5.4)
Build in visual studio

@auto-labeler
Copy link

auto-labeler bot commented Jun 3, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@jburns24
Copy link
Author

jburns24 commented Jun 3, 2020

So I am just seeing that in the generated README.md you call out an issue with versions of RestSharp greater than 105.1.0 and point to this issue restsharp/RestSharp#742. That issue is now closed and that must be the new method signature that I am seeing in the later versions of RestSharp.

@erezgeva
Copy link

erezgeva commented Jan 7, 2021

Hi,
After looking in https://restsharp.dev/api/RestSharp.html#interface-irestrequest

Seems that the line should be:
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentLength, param.Value.ContentType);

As param.Value already have the value for ContentLength. Seems clear path to fix this one.

I use RestSharp 106.11.4

@dlumpp
Copy link

dlumpp commented Jan 22, 2021

RestSharp dropped net45 support in version 106 so it wouldn't make sense to update the dependency in the v4.5 generator. This changed signature has already been fixed in #5870.

If you generate with targetFramework v4.5.2 you'll get a dependency on RestSharp 106 and the correct AddFile call.

@wing328 wing328 closed this as completed Mar 10, 2021
@wing328
Copy link
Member

wing328 commented Mar 10, 2021

csharp-netcore supports framework 4.7 as well so please give it a try with the latest master assuming you can upgrade framework to 4.7 from 4.5 or older versions in your environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants