Skip to content

CSharp code generator - body parameter not passed on to invokeBinaryAPI #734

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
boazsapir opened this issue May 7, 2015 · 8 comments
Closed

Comments

@boazsapir
Copy link

I defined a binary parameter in the 'body' part of a POST request whose response is also binary

  • @swg\Parameter(name="BinaryData", in="body", required=true, @swg\Schema(type="string", format="byte")),
  • @swg\Response(response=200, description="Encrypted data", @swg\Schema(type="string", format="byte"))

In the generated code, the parameter is:

  1. defined as a string
  2. not passed on to invokeBinaryAPI
    (see also CSharp code generator - generated function's response type is string instead of byte[] #733 for a related issue).

The generated code (note that the 5th parameter to invokeBinaryAPI () is null):

public string  encrypt (string ItemName, string Token, string BinaryData) {
  // create path and map variables
  var path = "/keys/{itemName}?op=encrypt".Replace("{format}","json").Replace("{" + "itemName" + "}", apiInvoker.escapeString(ItemName.ToString()))

  // query params
  var queryParams = new Dictionary<String, String>();
  var headerParams = new Dictionary<String, String>();
  var formParams = new Dictionary<String, object>();


  headerParams.Add("Token", Token);


  try {
    if (typeof(string) == typeof(byte[])) {

      var response = apiInvoker.invokeBinaryAPI(basePath, path, "GET", queryParams, null, headerParams, formParams);
      return ((object)response) as string;


    } else {

      var response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, BinaryData, headerParams, formParams);
      if (response != null){
         return (string) ApiInvoker.deserialize(response, typeof(string));
      }
      else {
        return null;
      }


    }
  } catch (ApiException ex) {
    if(ex.ErrorCode == 404) {
      return null;
    }
    else {
      throw ex;
    }
  }
}
@wing328
Copy link
Contributor

wing328 commented May 8, 2015

For BinaryData, do you expect its type to be byte array (byte[]) instead of string ?

invokeBinaryAPI will be removed after switching to RestSharp #700

@webron
Copy link
Contributor

webron commented May 9, 2015

@boazsapir - you've opened a few issues so far, and thank you for that. It definitely helps us improve the project. However, you tend to share your annotations from what I suspect is PHP (specifically swagger-PHP). That ends up being a bit less useful because it assumes that the output of the library is correct, but what's really being parsed is the Swagger definition you get from it. In the future, please make sure to include those so we can eliminate the option that the definition is not being produced properly rather than the codegen not processing it properly.

@boazsapir
Copy link
Author

@webron no problem, here is the Swagger definition:

        "post": {
            "operationId": "encrypt",
            "consumes": [
                "application/octet-stream"
            ],
            "produces": [
                "application/octet-stream"
            ],
            "parameters": [
                {
                    "name": "BinaryData",
                    "in": "body",
                    "required": true,
                    "schema": {
                        "type": "string",
                        "format": "byte"
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "Encrypted data",
                    "schema": {
                        "type": "string",
                        "format": "byte"
                    }
                }
            }
        }

@boazsapir
Copy link
Author

@wing328 yes, I expect the type of BinaryData to be byte array

@wing328
Copy link
Contributor

wing328 commented May 10, 2015

@boazsapir Currently, csharp-restsharp only supports model (JSON) as body. As you've confirmed, the master branch does not work with binary body either.

I'll see what I can do to address the issue in the csharp_restsharp branch.

@webron
Copy link
Contributor

webron commented May 10, 2015

@boazsapir - for what it's worth, file uploads using application/octet-stream is not quite well defined in Swagger (unfortunately). The byte format is also not defined well enough and the original intention was a Base64 encoding of binary data.

See OAI/OpenAPI-Specification#326.

@boazsapir
Copy link
Author

Thank you for the clarification. I will open an issue in swagger-spec with my proposal how the definition for byte format should be

@wing328
Copy link
Contributor

wing328 commented Jan 23, 2016

@boazsapir C# API client now supports binary format (mapped to byte[] in C#) via #1864

@wing328 wing328 closed this as completed Jan 23, 2016
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

3 participants