swagger-mongoose icon indicating copy to clipboard operation
swagger-mongoose copied to clipboard

Top level objects must not be type 'array'

Open kellertk opened this issue 9 years ago • 2 comments

I've given the swagger definitions in YAML, but I'm converting it to JSON before compilation.

definitions:
  Servers:
    type: array
    items:
      $ref: "#/definitions/Server"
  Server:
    type: object
    properties:
      uid:
        type: string
        description: Unique server ID
      name:
        type: string
        description: FQDN

This returns:

Error: Unrecognized schema type: undefined
    at propertyMap (node_modules\swagger-mongoose\lib\index.js:48:13)
    at getSchemaProperty (node_modules\swagger-mongoose\lib\index.js:240:16)
    at node_modules\swagger-mongoose\lib\index.js:217:26
    at node_modules\swagger-mongoose\node_modules\lodash\lodash.js:4944:15
    at baseForOwn (node_modules\swagger-mongoose\node_modules\lodash\lodash.js:3001:24)
    at node_modules\swagger-mongoose\node_modules\lodash\lodash.js:4913:18
    at Function.forEach (node_modules\swagger-mongoose\node_modules\lodash\lodash.js:9359:14)
    at getSchema (node_modules\swagger-mongoose\lib\index.js:216:5)
    at node_modules\swagger-mongoose\lib\index.js:319:14
    at node_modules\swagger-mongoose\node_modules\lodash\lodash.js:4944:15
    at baseForOwn (node_modules\swagger-mongoose\node_modules\lodash\lodash.js:3001:24)
    at node_modules\swagger-mongoose\node_modules\lodash\lodash.js:4913:18
    at Function.forEach (node_modules\swagger-mongoose\node_modules\lodash\lodash.js:9359:14)
    at Object.module.exports.compile (node_modules\swagger-mongoose\lib\index.js:307:5)
    at Object.<anonymous> (api\models\db.js:11:31)
    at Module._compile (module.js:570:32)

However, encapsulating the array in an object works properly:

definitions:
  Servers:
    description: Many Servers
    type: object
    properties:
      servers:
        type: array 
        items:
          $ref: "#/definitions/Server"
  Server:
    type: object
    properties:
      uid:
        type: string
        description: Unique server ID
      name:
        type: string
        description: FQDN

kellertk avatar Mar 14 '17 03:03 kellertk

What is the desired functionality for a top level array? What would you expect it to do on the mongo side as the object is the essence of the database collection that gets created? If you do:

definitions:
  Servers:
    x-swagger-mongoose:
      exclude-schema: true
    type: array
    items:
      $ref: "#/definitions/Server"
  Server:
    type: object
    properties:
      uid:
        type: string
        description: Unique server ID
      name:
        type: string
        description: FQDN

Does it still fail?

briananderson1222 avatar Mar 22 '17 12:03 briananderson1222

Just had a similar problem. Yes, it fails, probably because getSchema is called before checking for exclude-schema.

Upd: this is resloved in the repository but not in the files pulled from npm registry (even though the version matches). Could be something with my npm cache but it's the first version I installed.

Anyway, doing something with arrays like that is probably a good idea. Even as simple as ignoring them and mentioning it in the docs.

tnphis avatar Apr 12 '17 06:04 tnphis