Skip to content

Chgen/view handle request context - #1321

Open
lemito wants to merge 12 commits into
userver-framework:developfrom
lemito:chgen/view-handle-request-context
Open

Chgen/view handle request context#1321
lemito wants to merge 12 commits into
userver-framework:developfrom
lemito:chgen/view-handle-request-context

Conversation

@lemito

@lemito lemito commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Add RequestContext for chaotic-openapi


Note: by creating a PR or an issue you automatically agree to the CLA. See CONTRIBUTING.md. Feel free to remove this note, the agreement holds.

lemito added 3 commits August 15, 2026 00:22
View::Handle now receives server::request::RequestContext as the third
parameter, giving handlers access to per-request data (e.g. user auth
info set by the auth middleware). The dispatcher falls back to the
legacy two-argument Handle via the ViewHasHandleWithContext concept, so
existing hand-written views keep working unchanged.
…e signature

Regenerate golden output with the three-argument Handle and migrate
hand-written views in integration tests and the chaotic_openapi_service
sample. The headersGet view demonstrates reading per-request context
data (x-user-id) set by an auth checker.
@lemito
lemito marked this pull request as draft August 14, 2026 21:58
Comment thread chaotic-openapi/AGENTS.md Outdated
`main.py`.


## View contract

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

это лишнее

Deps&& /*deps*/,
RequestContext& context
) {
const auto& auth_info = USERVER_NAMESPACE::server::auth::GetUserAuthInfo(context);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

можно прямо в chaotic_openapi_service записать в +1 ручку, отдельный сервис не требуется

**Step 3.** Implement the generated view stub. Chaotic writes a skeleton to
`src/handlers/NAME/OPERATION/view.cpp` on the first run:
`src/handlers/NAME/OPERATION/view.cpp` on the first run. `Handle` always receives the
per-request context as the third argument (an alias `RequestContext` is provided in `View`):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

лишнее

* **View stub** — a minimal `.hpp`/`.cpp` pair in `SRC_DIR` that you fill in with business logic.
Existing view stubs are **never overwritten** on subsequent runs.
`View::Handle` receives the parsed request, the dependencies (`Deps`), and the per-request
`RequestContext` (see @ref scripts/docs/en/userver/chaotic_handlers.md "Authentication and request context" below).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

лишнее для обзора

Comment thread .mapping.json Outdated
"samples/benchmark_service/static_config.yaml":"taxi/uservices/userver/samples/benchmark_service/static_config.yaml",
"samples/benchmark_service/testsuite/conftest.py":"taxi/uservices/userver/samples/benchmark_service/testsuite/conftest.py",
"samples/benchmark_service/testsuite/test_benchmark.py":"taxi/uservices/userver/samples/benchmark_service/testsuite/test_benchmark.py",
"samples/chaotic_openapi_auth_service/CMakeLists.txt":"taxi/uservices/userver/samples/chaotic_openapi_auth_service/CMakeLists.txt",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

не надо трогать этот файл)

assert set(by_path) == {'testmepost/view.hpp', 'testmepost/view.cpp'}


def test_view_hpp_contract():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

тесты на посимвольное сравнение не требуются

@lemito
lemito marked this pull request as ready for review August 17, 2026 08:16
@lemito
lemito requested a review from segoon August 17, 2026 09:46
const auto& auth_value = request.GetHeader(http::headers::kAuthorization);
constexpr std::string_view kBearerPrefix = "Bearer ";
if (auth_value.size() <= kBearerPrefix.size() ||
std::string_view{auth_value}.substr(0, kBearerPrefix.size()) != kBearerPrefix) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

StartsWith


std::uint64_t user_id = 0;
try {
user_id = std::stoull(std::string{auth_value.substr(kBearerPrefix.size())});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

FromNumber

SetUserAuthInfo(
request_context,
server::auth::UserAuthInfo{
server::auth::UserId{user_id},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

давай хотя бы из секдиста брать
или посмотри на другие примеры, откуда они токен/ключ берут

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.

2 participants