从 API 资源管理器中删除嵌套关系(招摇)

Remove nested relations from API explorer (swagger)

我有 Application 个具有以下关系的模型:

  @belongsTo(() => Ido)
  idoId: string;


export interface ApplicationRelations {
  ido?: IdoWithRelations;
}

export type ApplicationWithRelations = Application & ApplicationRelations;

Application 存储库如下所示:

  export class ApplicationRepository extends DefaultCrudRepository<
  Application,
  typeof Application.prototype.id,
  ApplicationRelations
> {
  public readonly user: BelongsToAccessor<
    User,
    typeof Application.prototype.id
  >;

  constructor(
    @inject('datasources.db') dataSource: DbDataSource,
    @repository.getter('UserRepository')
    protected userRepositoryGetter: Getter<UserRepository>,
  ) {
    super(Application, dataSource);
    this.user = this.createBelongsToAccessorFor('user', userRepositoryGetter);

    this.inclusionResolvers.delete('ido');
  }
}

以及 IDO 模型中的以下关系:

 @hasMany(() => Application)
  applications: Application[];

post /ido 中,我大摇大摆地得到了这个创建示例:

{
  "applications": [
    {
      "status": "string",
      "createdAt": 0,
      "idoId": "string",
      "userId": "string",
      "ido": {
        "applications": [
          {
            "status": "string",
            "createdAt": 0,
            "idoId": "string",
            "userId": "string",
            "ido": {
              "applications": [
                "string"
              ],
}

是否有任何方法可以从 swagger 中删除 applicationido 的重复和课程关系类型? 或者这并不重要我可以手动删除第一个应用程序上方的所有字段吗?

This answer was copied over from https://github.com/loopbackio/loopback-next/discussions/8536#discussioncomment-2655375.

OpenAPI/Swagger 位于 REST 层,这意味着您需要查看相应的 REST 控制器内部,其内容类似于:

@post('...')
function create(
  @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Application, {
            title: 'NewApplication',
            exclude: ['id'],
          }),
        },
      },
    })
)

您可以这样修改它以排除关系:

@post('...')
function create(
  @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Application, {
            title: 'NewApplication',
            exclude: ['id'],
+          includeRelations: false,
          }),
        },
      },
    })
)

另一种方法是使用 exclude

可以在 API 文档中找到更多信息:https://loopback.io/doc/en/lb4/apidocs.repository-json-schema.getjsonschemaref.html