Skip to content

Refactoring: OpenApiResponseValidator のキャッシュ最適化#36

Merged
wadakatu merged 2 commits intomainfrom
refactor/validator-cache-optimization
Mar 17, 2026
Merged

Refactoring: OpenApiResponseValidator のキャッシュ最適化#36
wadakatu merged 2 commits intomainfrom
refactor/validator-cache-optimization

Conversation

@wadakatu
Copy link
Collaborator

概要

OpenApiResponseValidator::validate() が呼ばれるたびに OpenApiPathMatcher・opis ValidatorErrorFormatter を毎回新規生成していた非効率を解消し、インスタンスレベル/静的キャッシュを導入。

変更内容

  • OpenApiResponseValidator: ValidatorErrorFormatter をコンストラクタで1回だけ生成するようにし、OpenApiPathMatcher を spec 名ごとにキャッシュする getPathMatcher() を追加
  • ValidatesOpenApiSchema trait: 静的キャッシュ ($cachedValidator) を導入し、maxErrors が変わらない限り同じバリデータインスタンスを再利用。テスト用に resetValidatorCache() を追加
  • テスト3ファイルの tearDown()self::resetValidatorCache() を追加してテスト間の分離を担保

関連情報

…nces

Instead of recreating PathMatcher, opis Validator, and ErrorFormatter on
every validate() call, cache them at the instance level in
OpenApiResponseValidator. Also add a static validator cache in the
ValidatesOpenApiSchema trait to avoid reconstructing the validator on
every assertion, with resetValidatorCache() for test isolation.
Use ?int with null instead of int with -1 to represent the
"no cached value" state, consistent with $cachedValidator.
@wadakatu wadakatu merged commit e0088f0 into main Mar 17, 2026
8 checks passed
@wadakatu wadakatu self-assigned this Mar 17, 2026
@wadakatu wadakatu deleted the refactor/validator-cache-optimization branch March 17, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant