Gi60s / openapi-enforcer

Apache License 2.0
94 stars 22 forks source link

Schema.prototype.populate not working with type array #104

Closed dr-aiuta closed 3 years ago

dr-aiuta commented 3 years ago

Hi,

I saw this project just a couple days ago but didn't find anything about passing arrays as params on populate method, it only accepts plain objects.

properties = { 
  type: 'array',
  items:{
    $ref:'#/components/schemas/Property'
  }
};

UnhandledPromiseRejectionWarning: Error: Invalid params specified. Must be a plain object

The problem is that my schema is an array of objects. How do I deal with it?

obs.: derefence is ok.

Gi60s commented 3 years ago

Hello, @dr-aiuta.

That is correct that there is nothing about using populate with arrays. I couldn't think of a way to make data map to arrays, so what I recommend for using populate with arrays is to create your own loop that generates the array by calling the populate repeatedly.

Here is a pseudo-code example that may be similar to what you're trying to accomplish:

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string'
    },
    birthdate: {
      type: 'string',
      format: 'date-time'
    },
    children: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: {
            type: 'string'
          },
          birthdate: {
            type: 'string',
            format: 'date-time'
          }
        }
      }
    }
  }
}

const rows = getChildrenOfPersonId(123)
const children = rows.map(row => {
  return schema.children.populate(row)
})
const result = {
  name: 'Bob',
  birthdate: new Date('1990-01-01'),
  children: children
}

Let me know if you have any other questions. Thank you.