Loopback-next: Can't use an array of entities as request body

Created on 29 May 2020  路  3Comments  路  Source: strongloop/loopback-next

I am trying to use an array of entity as a request object but it does not work

Here's my post function

@operation('post', '/customers')
  async addCustomers(@requestBody() _requestBody: Customer[]): Promise<any> {
    throw new Error('Not implemented');
 }

when I use the entity without it being inside an array it works
Customer instead of Customer[]

here's the OpenApi error

Semantic error at paths./customers.post.requestBody.content.application/json.schema
Schemas with 'type: array', require a sibling 'items: ' field

that comes from this

requestBody:
        content:
          application/json:
            schema:
              type: array
      operationId: CustomerController.addCustomers

I tried to do the same thing in the todo example and the same problem occured

Steps to reproduce

Create any model
Create any controller
Use an array of model as request object

Current Behavior

image

Should be an array of the model in the example value

Additional information

node -v v12.13.1

linux x64 12.13.1

+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- [email protected]
Docs bug

Most helpful comment

@achrinza Thank you, It's working, here's my updated code

@operation('post', '/customers')
  async addCustomers(@requestBody.array(getModelSchemaRef(Customer, {title: 'NewCustomer', exclude: ['id']}))
    _requestBody: Customer[]): Promise<any> {
    throw new Error('Not implemented');
  }

and as you said I searched first in the docs for a solution but it seems that
@requestBody.array()
doesn't seems to be documented,

I think it'll be better to add it for anyone in the future looking for this kind of functionality

All 3 comments

Hey, @Tahakun if you want to send many entities than you can send it in the multiple objects into an array then wrap it under the object.

{
 [
   { },
   { }
 ]
}

I think It will work and then after the req parameter you have to divide it.

For arrays, you should use @requestBody.array():

@post('/customers') {
  async addCustomers(@requestBody.array() _requestBody: Customer[]): Promise<any> {
    throw new Error('Not implemented');
 }
}

Could you try if it works?


To use @requestBody() with arrays:

const schemaWithArrayOfMyModel = {
  type: 'array',
  items: {
    'x-ts-type': Customer,
  },
};

export class Customers {
  @post('/customers')
  addCustomers(
    @requestBody({
      content: {'application/json': {schema: schemaWithArrayOfMyModel}},
    })
    _requestBody: MyModel[],
  ): string {
    throw new Error('Not implemented');
  }
}

Though note that this is what @requestBody.array() already does for you. So it's better to use the first example.

See: https://loopback.io/doc/en/lb4/Decorators_openapi.html#x-ts-type-extension


This doesn't seem to be documented, though. So if the first example works, I think we should modify scope of this issue to improve the docs.

@achrinza Thank you, It's working, here's my updated code

@operation('post', '/customers')
  async addCustomers(@requestBody.array(getModelSchemaRef(Customer, {title: 'NewCustomer', exclude: ['id']}))
    _requestBody: Customer[]): Promise<any> {
    throw new Error('Not implemented');
  }

and as you said I searched first in the docs for a solution but it seems that
@requestBody.array()
doesn't seems to be documented,

I think it'll be better to add it for anyone in the future looking for this kind of functionality

Was this page helpful?
0 / 5 - 0 ratings

Related issues

milindsingh picture milindsingh  路  3Comments

shadyanwar picture shadyanwar  路  3Comments

mightytyphoon picture mightytyphoon  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments

teambitcodeGIT picture teambitcodeGIT  路  3Comments