Coder Social home page Coder Social logo

Comments (9)

ogail avatar ogail commented on May 17, 2024

@centur thanks for reporting this, can you please attach sample Swagger for the repro?

from autorest.

centur avatar centur commented on May 17, 2024

check this gist https://gist.github.com/centur/953490b7c5b48c74fc99 - I've edited it a bit to remove unrelated bits.
The method in question is "/api/items/{id}" - for Get signature it always generates deserialization logic.

My current fix for this specific item is to edit proxy manually after each proxy generation and add condition to avoid de-serialization logic in case of application/octet-stream response type.

    // Deserialize Response
            if (statusCode == HttpStatusCode.OK)
            {
                if (Equals(httpResponse.Content.Headers.ContentType, new MediaTypeHeaderValue("application/octet-stream")))
                {
                    result.Body = responseContent;
                }
                else
                {
                    string resultModel = default(string);
                    JToken responseDoc = null;
                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        responseDoc = JToken.Parse(responseContent);
                    }
                    if (responseDoc != null)
                    {
                        resultModel = responseDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                    }
                    result.Body = resultModel;
                }
            }

from autorest.

stankovski avatar stankovski commented on May 17, 2024

@centur thank you for filing this. We were able to repro the issue. As far as the spec goes, however, you should specify the return type to be a file or to have a property of a file and use the appropriate MIME in produces. For example:

"produces": [
          "application/octet-stream"
        ],

and

  "definitions": {
    "Object": {
      "type": "file"
    },

from autorest.

centur avatar centur commented on May 17, 2024

@stankovski thanks, I've fixed produces section. Can you please elaborate a bit on definitions example?

If I understood this correctly - I can't just replace "type":"file" of Object definition (other methods will break as they are also using Object).

Can I just set that inside inline response (not sure yet how to do this with Swashbuckle :) but will find out) with "type":"file" like this:

 "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "file"
            }
          }
        },

Is this correct ?

from autorest.

stankovski avatar stankovski commented on May 17, 2024

Yep, that should work.

from autorest.

centur avatar centur commented on May 17, 2024

Sorry for the delay: I've got correct swagger definition for the file but it actually got worse :(
We have this definition

...
"/api/items/{id}": {
      "get": {
        "tags": [
          "Items"
        ],
        "summary": "Download a specific File by Id.",
        "operationId": "Items_DownloadFile",
        "consumes": [],
        "produces": [
          "application/octet-stream"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "String ID of the File",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "file"
            }
          }
        },
        "deprecated": false
      }
  }
...

With "application/octet-stream" in produces section, AutoRest doesn't generate proxy at all

PS D:\Projects\Tools> .\AutoRest.DocX.ps1
error: [FATAL] Error generating service model: The given key was not present in the dictionary.
Finished generating CSharp code for D:\Projects\Tools\Docx.swagger.json.

If I get rid of application/octet-stream and switch it back to

"produces": [
          "application/json",
          "text/json",
          "application/xml",
          "text/xml"
        ],

but keep

"responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "file"
            }
          }
        },

Code which is generated by AutoRest is broken (see inline comments for specific errrors):

public async Task<HttpOperationResponse<Stream>> DownloadFileWithOperationResponseAsync(string id, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
/*Removed unrelated parts*/
 // Deserialize Response
            if (statusCode == HttpStatusCode.OK)
            {
                Stream resultModel = default(System.IO.Stream);
                JToken responseDoc = null;
                if (string.IsNullOrEmpty(responseContent) == false)
                {
                    responseDoc = JToken.Parse(responseContent);
                }
                if (responseDoc != null)
                {
// Got Error: Cannot implicitly convert type 'string' to 'System.IO.Stream' here
                    resultModel = ((string)responseDoc);
                }
                result.Body = resultModel;
            }

So I'm currently switching back to the old fix we had. Hope this will be resolved soon.
AutoRest version is 0.9.7 as latest publicly released one.

from autorest.

stankovski avatar stankovski commented on May 17, 2024

Implemented in #186

from autorest.

hovsepm avatar hovsepm commented on May 17, 2024

Fixed in Release 0.10.0 - nuget, chocolatey, github

from autorest.

centur avatar centur commented on May 17, 2024

Great, thank you

from autorest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.