Skip to content
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

Closed
atomgomba opened this issue Oct 16, 2014 · 21 comments
Closed

Parameter of type "file" causing exception #662

atomgomba opened this issue Oct 16, 2014 · 21 comments

Comments

@atomgomba
Copy link

Parameters of type file are causing an exception in the current swagger-ui (6b448c1) at line 537 in swagger-client.js. If parameter type is file then getType() function returns undefined and innerType.toString() will throw an exception. I had to add to the if statement in getType() this code else 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).

@webron
Copy link
Contributor

webron commented Oct 16, 2014

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"
                                }
                        }
                }

        }

}

@atomgomba
Copy link
Author

Np, this is not confidental at all. ;)

@fehguy
Copy link
Contributor

fehguy commented Oct 16, 2014

Honestly file upload in swagger UI for the 2.0 spec is not quite done. It's next on the list.

@atomgomba
Copy link
Author

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.

@fehguy
Copy link
Contributor

fehguy commented Oct 16, 2014

got it. watch for a fix here

@avishaan
Copy link

avishaan commented Dec 6, 2014

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.

@fehguy fehguy added this to the v2.1.0-alpha.8 milestone Dec 29, 2014
@fehguy
Copy link
Contributor

fehguy commented Dec 29, 2014

Should be addressed in the next couple days.

fehguy added a commit that referenced this issue Dec 31, 2014
@fehguy
Copy link
Contributor

fehguy commented Dec 31, 2014

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!

@micha-heigl
Copy link

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?

@fehguy
Copy link
Contributor

fehguy commented Jan 6, 2015

Hi, I was missing a commit, my apologies. I've pushed the file change here:

a5c1b63

Just pull the source and run from the dist folder, no need to rebuild.

Can you please retry? It's successfully submitting for me.

@micha-heigl
Copy link

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
The upload data is messed up and the file is just one blob in the $_POST-variable in php.

@webron
Copy link
Contributor

webron commented Jan 6, 2015

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.

@fehguy
Copy link
Contributor

fehguy commented Jan 6, 2015

using the spec from @atomgomba I get both the content-type and boundary correctly set:

image

@micha-heigl
Copy link

@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:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryK7wXVH1G13deSTiQ

When I upload a file to my API-endpoint using a REST-client, everything works fine and the parameter is set correctly.

@micha-heigl
Copy link

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.

@fehguy
Copy link
Contributor

fehguy commented Jan 6, 2015

Thanks. I'll look at this in a few

fehguy added a commit that referenced this issue Jan 7, 2015
@fehguy
Copy link
Contributor

fehguy commented Jan 7, 2015

ok tested and pushed. Please reopen if you see any other issues and thanks for the patience on this.

@fehguy fehguy closed this as completed Jan 7, 2015
@micha-heigl
Copy link

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).

@webron
Copy link
Contributor

webron commented Jan 7, 2015

Confirmed.

@webron webron reopened this Jan 7, 2015
@webron
Copy link
Contributor

webron commented Jan 7, 2015

To be exact, confirmed for 2.0 specs. 1.2 works fine.

@fehguy
Copy link
Contributor

fehguy commented Jan 31, 2015

this has been fixed in the develop_2.0 branch

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

5 participants