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

PostGameで以前のAPIと互換性を持たせる #989

Merged
merged 7 commits into from
Sep 21, 2024

Conversation

ikura-hamu
Copy link
Member

@ikura-hamu ikura-hamu commented Sep 21, 2024

fix: #906

新しく作ったAPIでは、ゲームの作成・編集においてvisibilityがrequiredになっており、以前のAPIとの互換性が無くなってしまって、mainに入れられなかった。
visibilityをoptionalにすることで、以前のAPIからも問題なくリクエストを送ることができる。visibilityが指定されなかった場合、privateとして扱う

Copy link

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Key issues to review

デフォルト値の設定
visibilityがnilの場合にデフォルトでprivateに設定されるロジックが追加されましたが、この変更がドキュメントにも反映されているか確認が必要です。特にOpenAPIのドキュメント更新が適切に行われているかを見直すべきです。

テストケースの追加
visibilityがnilの場合にprivateとして扱われることを確認するテストケースが追加されています。これにより、新しいデフォルト動作が正しく機能しているかを保証することができます。

OpenAPI生成コードの更新
OpenAPIの仕様に基づいて自動生成されたコードにvisibilityのデフォルト値がprivateであることを示すコメントが追加されています。この変更はAPIの使用者にとって重要な情報であり、適切にドキュメント化されていることを確認してください。

Copy link

github-actions bot commented Sep 21, 2024

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Best practice
omitempty オプションを使用して、不要なフィールドをJSON出力から省略します。

GameVisibility のポインタ型を使用している場合、JSONタグに omitempty
を追加することで、フィールドがゼロ値(nil)の場合にJSON出力から省略されるようにします。これにより、APIのレスポンスがよりクリーンになり、必要な情報のみを提供できるようになります。

src/handler/v2/openapi/openapi.gen.go [526]

-Visibility *GameVisibility `json:"visibility"`
+Visibility *GameVisibility `json:"visibility,omitempty"`
 
Suggestion importance[1-10]: 10

Why: The suggestion correctly identifies the use of omitempty to clean up JSON output, aligning with best practices for API responses.

10
Enhancement
switch 文に default ケースを追加して、想定外の値のハンドリングを行う。

switch 文内で reqVisibility の値に基づいて visibility を設定していますが、default
ケースを追加して、想定外の値が入った場合のハンドリングを行うことを検討してください。これにより、ロバストなコードになります。

src/handler/v2/game.go [196-199]

 switch reqVisibility {
   case openapi.Public:
     visibility = values.GameVisibilityTypePublic
   case openapi.Limited:
     visibility = values.GameVisibilityTypeLimited
+  default:
+    // ログを出力またはエラーを返すなどの処理
+    log.Errorf("unexpected visibility value: %v", reqVisibility)
+    return errors.New("unexpected visibility value")
 
Suggestion importance[1-10]: 9

Why: Adding a default case to handle unexpected values in a switch statement increases code robustness and helps prevent potential runtime errors.

9
不正な可視性値に対するエラーメッセージを検証するテストアサーションを追加する。

invalidVisibility
がテストケースで使用されていますが、この値が意図した通りのエラーを引き起こすかどうかを確認するために、エラーメッセージを検証するテストアサーションを追加することをお勧めします。

src/handler/v2/game_test.go [1219]

 Visibility:  &invalidVisibility,
+// アサーションを追加
+assert.Contains(t, err.Error(), "invalid visibility value")
 
Suggestion importance[1-10]: 8

Why: Adding assertions to verify error messages for invalid visibility values improves test coverage and ensures that the system behaves as expected when encountering errors.

8
GameVisibility のデフォルト値を設定して、安全性を向上させます。

GameVisibility のデフォルト値を private
に設定することで、明示的に指定されていない場合のデフォルト動作をコードで保証します。これにより、APIの使用者が visibility
パラメータを省略した場合の挙動が明確になり、安全性が向上します。

src/handler/v2/openapi/openapi.gen.go [461]

 type GameVisibility string
+const DefaultGameVisibility = private
 
Suggestion importance[1-10]: 8

Why: Setting a default value for GameVisibility enhances safety by ensuring consistent behavior when the visibility parameter is omitted, which is a significant improvement.

8
各テストケースで可視性を明示的に設定して、テストの独立性を向上させる。

テストケースで visibilityPublic
を複数のテストで使用していますが、各テストケースで可視性を明示的に設定することで、テストの独立性を高めることができます。これにより、テスト間での副作用を避け、より信頼性の高いテストを実現できます。

src/handler/v2/game_test.go [601]

-Visibility:  &visibilityPublic,
+Visibility:  &openapi.GameVisibility(openapi.Public),  # 直接値を設定
 
Suggestion importance[1-10]: 6

Why: Explicitly setting visibility in each test case enhances test independence and reliability, which is a good practice for maintaining robust tests.

6
Possible issue
デフォルトの可視性の値を見直し、必要に応じて変更する。

req.Visibility のポインタが nil の場合、デフォルトの可視性を openapi.Private
に設定していますが、これは意図した挙動でしょうか?もし他のデフォルト値が適切であれば、ここでの割り当てを変更することを検討してください。

src/handler/v2/game.go [189-192]

 if req.Visibility == nil {
-  reqVisibility = openapi.Private
+  reqVisibility = openapi.Public  # 例としてPublicをデフォルトに設定
 } else {
   reqVisibility = *req.Visibility
 }
 
Suggestion importance[1-10]: 7

Why: The suggestion to reconsider the default visibility value is valid as it prompts a review of whether 'openapi.Private' is the intended default. This could be crucial for ensuring the correct behavior of the application.

7

Copy link

codecov bot commented Sep 21, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 3 lines in your changes missing coverage. Please review.

Project coverage is 50.67%. Comparing base (beafc97) to head (3fe6170).
Report is 197 commits behind head on develop.

Files with missing lines Patch % Lines
src/handler/v2/game.go 83.33% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #989      +/-   ##
===========================================
+ Coverage    50.49%   50.67%   +0.17%     
===========================================
  Files          120      120              
  Lines         8607     8654      +47     
===========================================
+ Hits          4346     4385      +39     
- Misses        3947     3951       +4     
- Partials       314      318       +4     

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

@ikura-hamu ikura-hamu merged commit 89cd10c into develop Sep 21, 2024
10 checks passed
@ikura-hamu ikura-hamu deleted the fix/post_game_backward_compatibility branch September 21, 2024 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant