Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swagger-cli validateがvalidとなるapi.jsonを作れるようにする #12403

Merged
merged 11 commits into from
Nov 22, 2023

Conversation

samunohito
Copy link
Member

What

#12402 の対応です。
この対応により、swagger-cli validateがvalidとなるapi.jsonを作れるようになります。

◎認証が必要なエンドポイントはbodyのなかにiを埋め込む必要があることが明示される
必須パラメータとして、各エンドポイントのパラメータ一覧に登場します。

 
◎バックエンドを起動せずともapi.jsonが作れるようになる(ただしビルドは必要)
バックエンドのパッケージまで移動し、pnpm generate-api-jsonでbuilt配下に作成されます。

Why

#12402 に記載のとおりです

Additional info (optional)

◎validになることを確認しました。

openapi-typescriptというパッケージを使用し、developとこのブランチでそれぞれ作成したapi.jsonからTypeScriptの型を生成
 →前述のi(と軽微な修正)以外の差分が無いことを確認しました。

実際に生成されたもの:api.json

Checklist

  • Read the contribution guide
  • Test working in a local environment ※実行中
  • (If needed) Add story of storybook
  • (If needed) Update CHANGELOG.md
  • (If possible) Add tests

@github-actions github-actions bot added the packages/backend Server side specific issue/PR label Nov 21, 2023
}],
type: 'object',
optional: false, nullable: true,
ref: 'FederationInstance',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type: 'null'は定義されない書式らしいです…
代わりに、res直下のoptionalまたはnullableをtrueにすると、レスポンスのHTTPステータス一覧に204が生えるようにしています。

Copy link

codecov bot commented Nov 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (a4f8863) 78.79% compared to head (f9bf848) 78.77%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #12403      +/-   ##
===========================================
- Coverage    78.79%   78.77%   -0.02%     
===========================================
  Files          948      947       -1     
  Lines       102865   102778      -87     
  Branches      8277     8284       +7     
===========================================
- Hits         81053    80966      -87     
  Misses       21812    21812              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

in: 'body',
name: 'i',
},
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inには"header"、"query"、"cookie"のみ設定できるとのことでした…
先人がどう解決したのか調べましたが、bodyにパラメータとして記述することを選んでいたので、それに倣いました

if (endpoint.meta.requireCredential) {
// https://swagger.io/docs/specification/authentication/api-keys/
// ↑曰く、「can be "header", "query" or "cookie"」とのこと。
// Misskeyはbodyに埋め込む形にしているので、各エンドポイントのパラメータに直接APIキー用のフィールドを追加する必要がある
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misskeyはbodyに埋め込む形にしているので

表向きはそうですが実際はヘッダーに取ってもよいので

"securitySchemas": {
  "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "Authorization" }
}

にしてしまった方が取り回しの点でも優れて良さそう

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bodyにiをつける形はやめ、securitySchemesを復活させました。
あと、ApiCallServiceの実装的に、typeをapiKeyとするよりベアラトークンとして記載したほうがよさそうに思えたので、そのようにしています。

https://swagger.io/docs/specification/authentication/api-keys/
https://swagger.io/docs/specification/authentication/bearer-authentication/

b043aaa

@samunohito
Copy link
Member Author

samunohito commented Nov 22, 2023

追加で以下の対応もしました。
蛇足でしたら戻しますので、その旨ご指摘願えればと思います…

  • パラメータのボディに空オブジェクトが設定されているエンドポイント(admin/metaなど)は、requestBodyを出力しないようにしました( 819f2a2 )。api.jsonからの型定義生成時、空オブジェクトを明示的に渡す必要があるような形になってしまうので…
  • allowGetがtrueなエンドポイントはgetリクエストでもいける旨を明示するようにしました( a9cb82e

@syuilo syuilo merged commit c284d41 into misskey-dev:develop Nov 22, 2023
17 of 18 checks passed
@syuilo
Copy link
Member

syuilo commented Nov 22, 2023

🙏🙏🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
packages/backend Server side specific issue/PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants