krakenjs / swaggerize-routes

Swagger document driven route builder.
Other
58 stars 57 forks source link

$ref ignored in body parameter schemas #96

Open smil2k opened 5 years ago

smil2k commented 5 years ago

Example: ...

...
  put:
      parameters:
      - name: body
        required: true
        in: body
        schema:
          $ref: '#/definitions/employeeFull'

validator.js does not call $ref resolution before enjoi at line 97. Suggested fix:

  if ((parameter.in === 'body' || parameter.in === 'formData') && template.schema) {
               iif (template.schema.$ref) {
                    template.schema = refresolver(schemas, template.schema.$ref);
                } // resolve schema!
                schema = enjoi(template.schema, {
                    subSchemas: schemas,
                    types: types
                });
            }
smil2k commented 5 years ago

Maybe resolving all of the refs makes it easier. I do it externally currently, but I would have expected it to be done:

const http = require('http');
const express = require('express');
const swaggerize = require('swaggerize-express');
const refParser = require('json-schema-ref-parser');
const morgan = require('morgan');
app = express();

const server = http.createServer(app);

refParser.dereference('./api-docs/swagger.yaml')
  .then(function(schema) {
      app.use(morgan("dev"));
      app.use(swaggerize({
          api: schema,
          docspath: '/api-docs',
          handlers: './handlers'
      }));

      server.listen(10010, 'localhost', function () {
          app.swagger.api.host = server.address().address + ':' + server.address().port;
      });
  })
  .catch(function(err) {
    console.error(err);
  });
djMax commented 4 years ago

I think I'm running into exactly this on query parameters as well. It's partly because it's using it as a key in a dictionary:

            if (def.parameters) {
                def.parameters.forEach(function (parameter) {
                    validators[parameter.in + parameter.name] = parameter;
                });
            }

And with a ref, those things aren't there.