Add methods for set cache_age in openapi schema#6
Conversation
|
bugbot run |
…i' into feature/add_cache-age-for-openapi
| except KeyError: | ||
| continue | ||
|
|
||
| app.openapi_schema = openapi_schema |
There was a problem hiding this comment.
а как она обычно подставляется? мы не ломаем какие то едж кейсы про генерацию схемы?
There was a problem hiding this comment.
Скорее всего ещё надо в начале прокинуть проверку схему. Доке ФастАПИ есть пример кастомной схемы - https://fastapi.tiangolo.com/how-to/extending-openapi/#normal-fastapi
Там прям так и идёт:
app.openapi_schema = openapi_schema
return app.openapi_schema
Единственное я ничего не возвращаю.
There was a problem hiding this comment.
а в app.openapi_schema когда метод выполняется ничего нет? может схема уже готова
There was a problem hiding this comment.
бездушная машина тоже говорит ... by always calling get_openapi(), which bypasses any custom OpenAPI schema generation logic
There was a problem hiding this comment.
Там логика такая: мы запрашиваем уже готовую схему от app, её редактируем, добавляем новые поля в ручки и передаём обновлённую схему. В прошлом варианте мы получали эту схему и целиком перезаписывали её, тем самым все пользовательские схемы перетирали.
|
bugbot run |
There was a problem hiding this comment.
Bug: Lifespan Event Missing Type Check
The on_lifespan method calls set_cache_age_in_openapi_schema with scope["app"] without a FastAPI type check, unlike the on_http method, which can lead to runtime errors if scope["app"] is not a FastAPI instance. Furthermore, the _openapi_initialized flag is not set to True in on_lifespan after this call, causing duplicate OpenAPI schema processing on the first HTTP request.
fast_cache_middleware/middleware.py#L231-L245
FastCacheMiddleware/fast_cache_middleware/middleware.py
Lines 231 to 245 in 5ebd29f
Bug: Cache Age Setting and Schema Overwrite Issues
The set_cache_age_in_openapi_schema function has two issues: it incorrectly accesses route dependencies via route.dependant.dependencies instead of route.dependencies, preventing x-cache-age from being set for CacheConfig dependencies in the OpenAPI schema. Additionally, it unconditionally overwrites app.openapi_schema by always calling get_openapi(), which bypasses any custom OpenAPI schema generation logic.
fast_cache_middleware/_helpers.py#L6-L33
FastCacheMiddleware/fast_cache_middleware/_helpers.py
Lines 6 to 33 in 5ebd29f
Was this report helpful? Give feedback by reacting with 👍 or 👎
|
bugbot run |
| @@ -0,0 +1,141 @@ | |||
| import time | |||
There was a problem hiding this comment.
Создал conftest, надеюсь не против. А то фикстуры разрастаются, становится тяжко. И свои тесты по схеме запихнул в отдельный модуль.
| app.openapi_schema = openapi_schema | ||
| return app.openapi_schema | ||
|
|
||
| app.openapi = custom_openapi # type: ignore |
There was a problem hiding this comment.
Мне никак не победить mypy с его ошибкой Mypy: Cannot assign to a method [method-assign].
|
|
||
|
|
||
| def set_cache_age_in_openapi_schema(app: FastAPI) -> None: | ||
| openapi_schema = app.openapi() |
There was a problem hiding this comment.
Добавил тест, который проверяет схему на "добавленные пользователем поля", работаю с функцией из utils, всё перезаписывалось. Переделал что б мы работали чисто со схемой приложения и всё нормально, перезаписей нету.
| except KeyError: | ||
| continue | ||
|
|
||
| app.openapi_schema = openapi_schema |
There was a problem hiding this comment.
а в app.openapi_schema когда метод выполняется ничего нет? может схема уже готова
| except KeyError: | ||
| continue | ||
|
|
||
| app.openapi_schema = openapi_schema |
There was a problem hiding this comment.
бездушная машина тоже говорит ... by always calling get_openapi(), which bypasses any custom OpenAPI schema generation logic
Автоматическое проставления cache-age в OpenAPI схему.