-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Parameter of type "file" causing exception #662
Comments
I hope you don't mind, but I'll just copy the sample here: {
"swagger": 2,
"info": {
"title": "This is an API",
"version": "0.1.1",
"license": {
"name": "WTFPL 2.0",
"url": "http://www.wtfpl.net/txt/copying/"
}
},
"host": "example.com",
"basePath": "/api",
"schemes": ["http"],
"produces": ["application/json"],
"paths": {
"/masterdata": {
"get": {
"tags": ["masterdata"],
"operationId": "getMasterdata",
"summary": "Növénykultúrák és városok listájának lekérdezése",
"description": "A három lista alfabetikusan van rendezve.",
"responses": {
"200": {
"description": "Siker",
"schema": {
"$ref": "#/definitions/Masterdata"
}
}
}
}
},
"/contents": {
"get": {
"tags": ["content"],
"operationId": "getContents",
"summary": "Tartalom lekérdezése megye és növénykultúra alapján",
"parameters": [
{
"in": "query",
"name": "county_id",
"description": "A megye azonosítója",
"type": "integer",
"format": "int64"
},
{
"in": "query",
"name": "crop_id",
"description": "A növénykultúra azonosítója",
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "Siker",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Contents"
}
}
},
"404": {
"description": "Nincs ilyen megye vagy növénykultúra"
},
"422": {
"description": "Hiányzó paraméter"
}
}
}
},
"/users/sign_up": {
"post": {
"tags": ["user"],
"operationId": "signUp",
"summary": "Felhasználó regisztrációja",
"parameters": [
{
"in": "formData",
"name": "username",
"type": "string",
"required": true
},
{
"in": "formData",
"name": "password",
"type": "string",
"required": true
}
],
"responses": {
"200": {
"description": "Siker",
"schema": {
"$ref": "#/definitions/Preferences"
}
},
"302": {
"description": "Ez a felhasználó már létezik"
},
"400": {
"description": "A request nem POST vagy üres"
},
"422": {
"description": "A jelszavak nem egyeznek"
}
}
}
},
"/users/sign_in": {
"post": {
"tags": ["user"],
"operationId": "signIn",
"summary": "Felhasználó beléptetése",
"description": "A jelszót plaintextben várja. Siker esetén visszaadja a felhasználó weben kiválasztott megyéjét és növénykultúráját.",
"consumes": ["application/x-www-form-urlencoded"],
"parameters": [
{
"in": "formData",
"name": "username",
"type": "string",
"required": true
},
{
"in": "formData",
"name": "password",
"type": "string",
"required": true
}
],
"responses": {
"200": {
"description": "Siker",
"schema": {
"$ref": "#/definitions/Preferences"
}
},
"400": {
"description": "A request nem POST vagy üres"
},
"401": {
"description": "Hibás felhasználónév vagy jelszó"
}
}
}
},
"/users/preferences": {
"get": {
"tags": ["user"],
"operationId": "getPreferences",
"summary": "Felhasználó megye- és növénypreferenciáinak lekérdezése",
"responses": {
"200": {
"description": "Siker",
"schema": {
"$ref": "#/definitions/Preferences"
}
},
"401": {
"description": "Felhasználó nincs bejelentkezve"
}
}
},
"post": {
"tags": ["user"],
"operationId": "postPreferences",
"summary": "Felhasználó megye- és növénypreferenciáinak mentése",
"produces": ["application/json"],
"parameters": [
{
"in": "formData",
"name": "crop_id",
"type": "integer",
"format": "int64",
"required": true
},
{
"in": "formData",
"name": "county_id",
"type": "integer",
"format": "int64",
"required": true
}
],
"responses": {
"200": {
"description": "Siker"
},
"401": {
"description": "Felhasználó nincs bejelentkezve"
},
"500": {
"description": "Valami szerverhiba"
}
}
}
},
"/users/report": {
"post": {
"tags": ["user"],
"operationId": "postReport",
"summary": "Bejelentés küldése",
"consumes": ["multipart/form-data"],
"parameters": [
{
"in": "formData",
"name": "crop_id",
"type": "integer",
"format": "int64",
"required": true
},
{
"in": "formData",
"name": "county_id",
"type": "integer",
"format": "int64",
"required": true
},
{
"in": "formData",
"name": "image_file",
"description": "Egy képfájl",
"type": "file",
"required": true
}
],
"responses": {
"200": {
"description": "Siker"
},
"401": {
"description": "Felhasználó nincs bejelentkezve"
},
"500": {
"description": "Valami szerverhiba"
}
}
}
}
},
"definitions": {
"Masterdata": {
"properties": {
"crops": {
"type": "array",
"items": {
"$ref": "ListItem"
}
},
"cities": {
"type": "array",
"items": {
"$ref": "ListItem"
}
},
"dangers": {
"type": "array",
"items": {
"$ref": "Danger"
}
}
}
},
"ListItem": {
"properties": {
"id": {
"type": "integer"
},
"value": {
"type": "string"
}
}
},
"Danger": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"type": {
"type": "string",
"description": "Lehetséges értékei: pathogen (kórokozó), pest (kártevő)"
}
}
},
"Contents": {
"properties": {
"forecasts": {
"type": "array",
"items": {
"$ref": "Forecast"
}
},
"specialists": {
"type": "array",
"items": {
"$ref": "Specialist"
}
}
}
},
"Forecast": {
"properties": {
"title": {
"type": "string"
},
"fenology": {
"type": "string"
},
"region": {
"type": "string"
},
"text": {
"type": "string"
},
"infectionLevel": {
"type": "integer",
"description": "Lehetséges értékei: 5 (mentes), 4 (gyenge), 3 (közepes), 2 (erős), 1 (nagyon erős)"
}
}
},
"Specialist": {
"properties": {
"fullName": {
"type": "string"
},
"region": {
"type": "string"
},
"address": {
"type": "string"
},
"email": {
"type": "string"
}
}
},
"Preferences": {
"properties": {
"county_id": {
"type": "integer"
},
"crop_id": {
"type": "integer"
}
}
}
}
} |
Np, this is not confidental at all. ;) |
Honestly file upload in swagger UI for the 2.0 spec is not quite done. It's next on the list. |
I see, but in this particular case I wasn't about to test the API, but to display the documentation. The exception was blocking the rendering process thus stuck at the loading message forever. |
got it. watch for a fix here |
Any workaround for this for now? I am getting ready to deploy and the only reason I used this was so the front end developers had their documentations without needed someone to write it all. |
Should be addressed in the next couple days. |
Hi, can you please pull from the develop branch and try it out? Made some updates in the JS library to address this issue. Thanks! |
Hi, I've tried your fix from the develop branch as I had the same issue. Now swagger-client.js stops in line 753 as this.resource somehow gets undefined: http://prntscr.com/5ow5j2 I tried @atomgomba 's sample json, the same error shows up. Am I doing something wrong? Does the sample work for you? |
Hi, I was missing a commit, my apologies. I've pushed the file change here: Just pull the source and run from the Can you please retry? It's successfully submitting for me. |
Now the submit passes, but in the request header the Content-Type is set to application/json (shouldn't this change to multipart/form-data automatically?) If I set the consumes parameter to ['multipart/form-data'] manually, the Content-Type is correct but the boundary is missing, which results in an invalid POST: http://prntscr.com/5p629g |
With regards to the content-type, you have to set it to 'multipart/form-data' when using "file" type. The spec is explicit about it. If the boundary is missing, that's indeed a bug. |
using the spec from @atomgomba I get both the content-type and boundary correctly set: |
@webron you're right, but I thought if (type && type.toLowerCase() === 'file') {
if (!contentTypeModel.consumes) {
contentTypeModel.consumes = 'multipart/form-data';
}
} would do that for me. But reading it a second time I realized it would only work if no "consumes" is set at all. @fehguy regarding to http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 the Content-Type should contain the boundary parameter:
When I upload a file to my API-endpoint using a REST-client, everything works fine and the parameter is set correctly. |
I think the problem is, as soon as you set the Content-Type header manually, the boundary is missing. According to http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax/5976031#5976031 the contentType option must be set to false to force jQuery to build the Content-Type header on it's own, including the correct boundary. |
Thanks. I'll look at this in a few |
ok tested and pushed. Please reopen if you see any other issues and thanks for the patience on this. |
Thanks for the fix. File upload works fine, but now normal POST with application/x-www-form-urlencoded is broken (e.g. users/sign_up/ doesn't pass any POST-parameters). |
Confirmed. |
To be exact, confirmed for 2.0 specs. 1.2 works fine. |
this has been fixed in the develop_2.0 branch |
Parameters of type
file
are causing an exception in the current swagger-ui (6b448c1) at line 537 in swagger-client.js. If parameter type isfile
thengetType()
function returnsundefined
andinnerType.toString()
will throw an exception. I had to add to theif
statement ingetType()
this codeelse if (type == "file") { return type; }
in order to get my API doc displayed.Here is the API definition JSON:
http://pastebin.com/AC8cB8C1
Please note the
swagger
field is an integer because of an other issue with swagger-js (swagger-api/swagger-js#142).The text was updated successfully, but these errors were encountered: