Comments (9)
@centur thanks for reporting this, can you please attach sample Swagger for the repro?
from autorest.
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.
@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.
@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.
Yep, that should work.
from autorest.
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.
Implemented in #186
from autorest.
Fixed in Release 0.10.0 - nuget, chocolatey, github
from autorest.
Great, thank you
from autorest.
Related Issues (20)
- [TSP] Tsp compilie issues from CosmosDB to DefenderEasm
- [TSP] Issue found in conveting Swagger to TSP for AVS
- For multiline documentation, use new /** */ syntax in tsp
- Body param name is not same with the original swagger HOT 2
- allOf generation problem HOT 1
- restore the resource name and operation group name from swagger instead of .net metadata HOT 1
- add default, maximun, minimun for tempalte parameter HOT 1
- [TSP] Issue found in conveting Swagger to TSP for BotService
- [TSP] Issue found in conveting Swagger to TSP for Batch
- [TSP] Issue found in conveting Swagger to TSP for ConnectedVMwarevSphere
- [TSP] Issue summary
- Duplicate object schemas with 'ArrayItemschema' name detected. HOT 4
- Error: Enum types of 'undefined' and format 'undefined' are not supported. Correct your input HOT 1
- should support: patch operation does not have body HOT 1
- Enhance key property search logic HOT 1
- support extra path param for resource operation HOT 1
- Add support for new ARM LRO header HOT 1
- Docs for body parameter is different from the original swagger HOT 1
- [TSP] Support flatten in TSP conversion tool HOT 4
- Duplicate object schemas with 'HostOptions' name detected HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autorest.