loopbackio / loopbackio/loopback-next

Overriding title in `getModelSchemaRef` causes duplication in OpenAPI schema

オープン
#5,645 コメント 8 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug help wanted
主要言語
TypeScript
スター
5.1k
フォーク
1.1k
平均マージ
2日 21時間
マージ済み PR(30日)
27

説明

## Steps to reproduce

Define a controller with `getModelSchemaRef(SomeModel, {title: 'TitleOverride'})`.

Controller definition

```typescript
export class UserController {
@post('/users', {
security: OPERATION_SECURITY_SPEC,
responses: {
'200': {
description: 'User',
content: {
'application/json': {
schema: {
'x-ts-type': User,
},
},
},
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(NewUserRequest, {
title: 'NewUser',
}),
},
},
})
newUserRequest: NewUserRequest,
): Promise {
// implementation
}
}
```

## Current Behavior

The controller above emits schema definitions for both `NewUserRequest` and `NewUser`. While `NewUserRequest` is not referenced anywhere else in the schema.

## Expected Behavior

Only `NewUser` is present in OpenAPI schema.

## Additional information

`@loopback/repository-json-schema@2.4.2`
`@loopback/openapi-v3@3.4.1`

I presume `NewUserRequest` is generated from `getModelSchemaRef` as it should while `NewUser` comes from parsing `paramTypes` where it causes a cache miss due to overridden `title`.

A probable fix could exclude a parameter corresponding to `requestBody` from enumeration here

https://github.com/strongloop/loopback-next/blob/7f8d8356946dc236dd4daecbfae12e0a0662cf1c/packages/openapi-v3/src/controller-spec.ts#L329

## Acceptance Criteria

- [ ] Don't generate unused schema for parameter decorated with `@requestBody`

A solution I can think of is:
https://github.com/strongloop/loopback-next/blob/7f8d8356946dc236dd4daecbfae12e0a0662cf1c/packages/openapi-v3/src/controller-spec.ts#L329

should search through the content objects in the request body spec, if all contents' schemas exist in reference, then skip generating the one inferred from model ctor.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

packages/openapi-v3/src/controller-spec.ts の、参照されているパラメーター列挙の周辺から始め、requestBody スキーマと推論されたモデルコンストラクターが OpenAPI コンポーネントに入る経路を追ってください。title のオーバーライドケースを再現し、その後、スキーマがすでに参照されているリクエストボディが未使用の重複スキーマを生成しないことを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
openapi, typescript
領域
api, backend-api-design
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。