Skip to content

refactor!: pass tasks to middlewares, not requests #315

Closed

Description

@uniqueg

Problem

Middlewares get the flask.Request of POST /tasks
(AbstractMiddleware.apply_middleware(request) -> request), and
TaskRuns reads flask.request directly (create_task(),
list_tasks()). FOCA 1.0 drops Flask (Connexion 3 AsyncApp), and the
maintainer's hard constraint is that nothing prepared now may block that
migration: task processing and plugin-facing code must get plain data,
with the Flask/Connexion glue confined to the controllers. It also makes
the adoption of FOCA's plugin engine (T1) larger than it needs to be,
because the middleware call is tangled with request handling.

Proposal

  1. Interface: AbstractMiddleware.apply_middleware(task: dict) -> dict.
    The handler gives each alternative its own deep copy and keeps it only
    on success (same semantics as FOCA's engine); alternatives that return
    no task (not a dict) fail, too. The built-in middlewares are ported.
  2. The controllers (server.CreateTask, server.ListTasks) are the
    only code that reads the request: they pass the task (Connexion's
    body) and the request path (collection) to TaskRuns, which has
    no Flask import and no longer changes the task of the request (it
    works on a copy).
  3. Seam for T1: TaskRuns._apply_middlewares(task) -> (task, tes_urls)
    wraps the handler and the tes_urls extraction (500 via
    MiddlewareException if middlewares select no list of URLs). T1
    replaces this method with host.start()/request()/route().
  4. ast-grep rule framework-free-tasks (with rule tests): no flask,
    werkzeug, connexion or starlette imports in
    pro_tes/ga4gh/tes/task_runs.py, pro_tes/middleware/,
    pro_tes/plugins/.
  5. Docs (docs/middlewares.md): the new interface and copy semantics.

Acceptance criteria

  • No Flask, Werkzeug or Connexion import in the task processing and
    the middlewares (ast-grep rule, checked to fire on the old code).
  • Order of the security checks in create_task() unchanged; the
    task of the request is not changed (test); failed alternatives
    leave no trace, also those returning no task (tests).
  • Middlewares that select no tes_urls list fail the request
    (500), and the task is not kept (tests).
  • ruff, mypy, ast-grep, semgrep, pytest (100% coverage),
    mkdocs build --strict pass locally.

Verification (local)

All checks pass on f4d434e (1042 passed, 80 skipped; 100% coverage).
Not run: smoke test (Docker Compose), MongoDB 7.0 matrix entry.

Release notes (for the proTES release)

Breaking for custom middlewares: implement
apply_middleware(task: dict) -> dict instead of taking and returning a
flask.Request. (T1 replaces middlewares by FOCA plugins in the same or
the next release; if both land in one release, mention only T1.)

Dependencies

Open questions

  • None.

Activity

  1. added
    priority: high
    status: in progress
    type flag: breaking change
    type: maintenance
    workload: days
    on Oct 24, 2026
  2. removed
    status: in progress
    on Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: hightype flag: breaking changetype: maintenanceworkload: days

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions