有没有一种方法可以在lb4中组合模型的属性?

时间:2020-07-23 04:17:56

标签: typescript loopback4

我在lb4中有两个模型,希望通过requestBody在端点上接收。 目前,我正在使用两个模型作为类型对象的属性来创建一个新模型。 这导致生成的openapi文件仅请求两个常规对象,而不是组合的属性。

这是我所拥有的例子

@model()
export class Model1 extends Entity {
  @property({
    type: 'string',
  })
    prop1: string;
    
    @property({
    type: 'string',
  })
  prop2: string;

  constructor(data?: Partial<Model1>) {
    super(data);
  }
}

@model()
export class Model2 extends Entity {
  @property({
    type: 'string',
  })
    prop3: string;

  constructor(data?: Partial<Model2>) {
    super(data);
  }
}

// Combine the models to request them over a single requestBody
@model()
export class ModelRequest extends Entity {
    @property({
        type: 'object',
    })
    m1: Model1;

    @property({
        type: 'object',
    })
    m2: Model2;

    constructor(data?: Partial<ModelRequest>) {
        super(data);
    }
}

// Controller uses ModelRequest so API can receive combined properties
export class Controller1 {
  @post('/')
  async save(
    @requestBody({
      required: true
    }) request: ModelRequest
  ) {
        // process request here
  }
}

// This controller generates this JSON for the openapi specs
{
    "m1": "<object>",
    "m2": "<object>"
}

我想拥有一些生成像这样的openapi JSON

{
    "prop1": "<string>",
    "prop2": "<string>",
    "prop3": "<string>"
}

这可以通过手动在请求中编写模型的属性来实现,但是我想在更新内容时只修改m1或m2,而不是查找使用其属性的所有位置。

0 个答案:

没有答案