krakenjs / generator-swaggerize

Yeoman generator for design-driven apis with swagger 2.0 and krakenjs/swaggerize tools.
Other
70 stars 34 forks source link

Possible issue with multipart/form-data #9

Closed MrWako closed 9 years ago

MrWako commented 9 years ago

I think this is how you should include multipart/form-data post operations in swagger. To reproduce take https://github.com/swagger-api/swagger-spec/blob/master/examples/v2.0/json/petstore-simple.json and save to file. Replace the definition for petInput with the following

"petInput": {
  "required": [
    "id",
    "name"
  ],
  "properties": {
    "id": {
      "type": "integer",
      "format": "int64"
    },
    "name": {
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  }
},

this should give a spec file with which the generator works. Now replace the /pets post consumes and parameters specifications with:

    "consumes": [ "multipart/form-data" ],
    "parameters" : [
        {
           "name": "File",
           "in" : "formData",
           "type": "file",
           "required": true
        }
    ],

which I understand to be correct for post'ing multpart/form-data. If I run the generator I then get:

Swaggerize Generator ? What would you like to call this project: gg ? Your name: ? Your github user name: ? Your email: ? Path (or URL) to swagger document: ./schemas/issue/petstore-simple-fail.json ? Express or Hapi: express create .jshintrc create .gitignore create .npmignore create index.js create package.json create README.md create config/petstore-simple-fail.json create handlers/pets.js create handlers/pets/{id}.js create models/pet.js create models/petinput.js create models/errormodel.js

undefined:53 _.forEach(Object.keys(body).filter(function (k) { return !!body[k]; }), functi ^ TypeError: Object.keys called on non-object at Function.keys (native) at eval (eval at template (/node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/node_modules/yeoman-generator/node_modules/lodash/dist/lodash.js:6305:22), :53:18) at Function.forEach (/node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/node_modules/yeoman-generator/node_modules/lodash/dist/lodash.js:3297:15) at eval (eval at template (/node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/node_modules/yeoman-generator/node_modules/lodash/dist/lodash.js:6305:22), :16:3) at underscore as _engine at engine (/node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/node_modules/yeoman-generator/lib/actions/actions.js:303:10) at template (/node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/node_modules/yeoman-generator/lib/actions/actions.js:281:15) at /node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/app/index.js:345:18 at Array.forEach (native) at yeoman.generators.Base.extend.tests (/node-v0.10.33-linux-x64/lib/node_modules/generator-swaggerize/app/index.js:321:32)

node version v0.10.33, yeoman version 1.3.3. Latest version of generator-swaggerize.

Thanks.

tlivings commented 9 years ago

petInput is missing the type attribute (which should be object).

tlivings commented 9 years ago

I'll also add that the generated acceptance tests do not include the body parser for multipart, nor do they post as multipart. This would be a manual step if you'd like to use multipart.

MrWako commented 9 years ago

Thanks. I'm making good progress with the generator and its proving very useful. I'm quite happy with having to manually add the additional additional express code to handle multipart,

tlivings commented 9 years ago

Closing this issue.

teohhanhui commented 9 years ago

There's an (incorrect) assumption in the code (e.g. app/templates/_test_hapi.js)

var body;

...

if (param.in === 'body') {
    body = models[param.schema.$ref.slice(param.schema.$ref.lastIndexOf('/') + 1)];
}

...

if (operation.method.toLowerCase() === 'post' || operation.method.toLowerCase() === 'put') {%>
var body = {<%_.forEach(Object.keys(body).filter(function (k) { return !!body[k]; }), function (k, i) {%>

that the body key must be present when the type of operation is post or put

Even if you don't support code generation for this kind of use cases, the generator should not just crash...

hunnymunch commented 8 years ago

Was there any resolution to this issue? i'm getting the same error when trying to create the test express templates using the example swagger document.

https://github.com/BigstickCarpet/swagger-express-middleware/blob/master/samples/PetStore.yaml

undefined:54 _.forEach(Object.keys(body).filter(function (k) { return !!body[k]; }), functi ^ TypeError: Object.keys called on non-object at Function.keys (native) at eval (eval at template (swaggertest/node_modules/yeoman-generator/node_modules/lodash/dist/lodash.js:6306:22), :54:18) at Function.forEach (swaggertest/node_modules/yeoman-generator/node_modules/lodash/dist/lodash.js:3298:15) at eval (eval at template (swaggertest/node_modules/yeoman-generator/node_modules/lodash/dist/lodash.js:6306:22), :17:3) at underscore as _engine at engine (swaggertest/node_modules/yeoman-generator/lib/actions/actions.js:305:10) at template (swaggertest/node_modules/yeoman-generator/lib/actions/actions.js:283:15) at swaggertest/node_modules/generator-swaggerize/app/index.js:356:18 at Array.forEach (native) at yeoman.generators.Base.extend.tests (swaggertest/node_modules/generator-swaggerize/app/index.js:332:32)

tlivings commented 8 years ago

Should be resolved in latest. Sorry for the delay.