trailsjs / sails-swagger

Swagger integration for sails.js
107 stars 47 forks source link

Nested blueprint route returning an array of objects instead of one object when including an :id #38

Closed G2Jose closed 7 years ago

G2Jose commented 7 years ago

GET /model1/:id/model2/:id returns an array of objects (same response as /model1/:id/model2/ (Not expected). GET /model2/:id returns just one object as expected (Expected).

Model1 has a one to many relationship with Model2.

More specifically, I have 3 sails models with the following one to many relationships:

- User
  - Account
    - Transaction
    - Transaction...
  - Account ...

When I GET /users/:id/accounts/:id, I get an array like so

[
  {
    "user": "d7156354-6587-4614-b92b-7e8091e3311c",
    "id": "e7c41cc3-7b98-482e-8aa3-2cdd42eab3a1"
  }
]

I expected this to return a single Account object, and not an array containing just one Account object.

I get a single Account object as expected when I GET /accounts/:id I also get a single User object as expected when I GET /users/:id/,

My model files look like so

// User.js
module.exports = {
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',
      unique: true,
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
    },
    name: {
      type: 'string',
    },
    email: {
      type: 'string',
      unique: true,
    },
    accounts: {
      collection: 'account',
      via: 'user',
    }
  }
};
// Account.js
module.exports = {
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',
      unique: true,
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
    },
    date: {
      type: 'date',
      defaultsTo: () => new Date(),
    },
    amount: {
      type: 'float',
      defaultsTo: 0.0,
    },
    account: {
      model: 'account',
    },
    type: {
      type: 'string',
      enum: ['credit', 'debit'],
      defaultsTo: 'credit',
    },
    description: {
      type: 'string',
      defaultsTo: () => `Transaction at ${new Date()}`
    }
  }
};
// Transaction.js
module.exports = {
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',
      unique: true,
      primaryKey: true,
      defaultsTo: () => uuid.v4(),
    },
    date: {
      type: 'date',
      defaultsTo: () => new Date(),
    },
    amount: {
      type: 'float',
      defaultsTo: 0.0,
    },
    account: {
      model: 'account',
    },
    type: {
      type: 'string',
      enum: ['credit', 'debit'],
      defaultsTo: 'credit',
    },
    description: {
      type: 'string',
      defaultsTo: () => `Transaction at ${new Date()}`
    }
  }
};

Am I doing it wrong?