Skip to content

[ADD] http_api_dispatcher: generic dispatcher for JSON APIs - #689

Merged
OCA-git-bot merged 1 commit into
OCA:18.0from
camptocamp:18-add-http_api_dispatcher
Nov 23, 2026
Merged

OCA-git-bot merged 1 commit into
OCA:18.0from
camptocamp:18-add-http_api_dispatcher

Conversation

@simahawk

@simahawk simahawk commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

base_rest ties every API to its components and to the restapi route type, so an API can't leave base_rest (shopfloor included) without a replacement. This module provides the common plumbing of an API endpoint as a route type of its own, http_json, independent of components. APIs can then move to it one endpoint at a time. The error format and the params stay compatible with base_rest .

Key features

  • http_json route type: params parsed once from the query string and the body (JSON or forms)
  • language set from the Accept-Language header
  • input and output validated/converted by pluggable params declared on the route (see http_json_cerberus)
  • results serialized as JSON (dates as ISO strings, decimals as floats)
  • exceptions turned into JSON error responses w/ the proper status codes
  • hooks on each step, extensible via standard Odoo inheritance

A bit more of context in comparison to base_rest/fastapi (from module's readme)

base_rest (OCA/rest-framework) has been the foundation of many APIs,
including the Shopfloor apps. Over time it showed some limits:

  • it is a monolith: dispatcher, service layer, validation,
    documentation and routing are bound together
    and you cannot use one piece without the others
  • services must be components, a second registry
    with its own inheritance and boot lifecycle,
    which is often used only to look up a class by name
  • controllers are generated at boot from the services,
    making routing hard to follow and to customize

The fastapi stack is a way to go for new standalone APIs,
but moving existing APIs to it means rewriting every service and schema and endpoints are not extendable models.
Plus, it pulls in more external dependencies that for many cases are not needed.

This module is the base of a lighter stack, made of small modules
that can be used separately and extended via standard Odoo inheritance:

  • http_api_dispatcher: the http_api route type (parsing, validation hooks, JSON errors)
    usable by plain Odoo controllers
  • endpoint_spec: declare endpoints on services, for routes generators
  • http_api_cerberus: Cerberus validation
  • openapi_doc_base: API docs from the actual routes
  • endpoint_route_handler generators: routes computed from the code
    without storing a row per route

Goals:

  • simplify the base_rest stack without moving to fastapi
  • keep modules small and reusable
  • drop the dependency on component, staying highly customizable
    (routing keys per route, ir.http hooks per database)
  • stay compatible with base_rest clients (error format, validation behavior)
    to allow a gradual migration

@OCA-git-bot OCA-git-bot added series:18.0 mod:http_api_dispatcher Module http_api_dispatcher labels Nov 13, 2026
@simahawk

Copy link
Copy Markdown
Contributor Author

@lmignon following your advice I'm moving OCA/web-api#179 here 😉
I'd love to have your review finalized as I addressed the last comments there 🙏

simahawk added a commit to camptocamp/rest-framework that referenced this pull request Nov 13, 2026
Pending OCA#689 (http_api_dispatcher).
To be removed once merged.
Comment thread http_api_dispatcher/errors.py Outdated
for k, v in httprequest.headers.items()
if k not in BLACKLISTED_LOG_HEADERS
}
params = dict(getattr(request, "params", None) or {})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@simahawk IMO, here you're at risk. You provide a way to a malware to consume physical resources by providing unrelated large payload...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added shortening of the strings before logging

@lmignon lmignon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@simahawk A little comment otherwise everything looks good to me...

Route type `http_api`: generic dispatcher for HTTP routes speaking JSON.

* parse query string and body (JSON, forms) once
* set lang from `Accept-Language`
* validate/convert input and output via pluggable params
  declared on the route
* serialize results as JSON
* map exceptions to JSON error responses (format compatible w/ base_rest)
* `ir.http` hooks to extend each step of the pipeline (`_http_api_*`)

Lazy params: `ApiCall` exposes `query`, `body` and `params` as cached
properties, parsed on first access. `params` merges them: query, then
body, then URL args.

CSRF: the token is read from the `X-CSRF-Token` header first; the query
and the body are parsed only when the header is missing. The `csrf_token`
key is transport data: it is never part of the params of the endpoint.

Parsers: routes override the body parsers via `body_parsers` (mimetype
map, formerly `parsers`) and the query parser via `query_parser`.

Params: `ApiParam.from_params(call)` receives the `ApiCall` and reads
`call.params`, `call.query` or `call.body` as it needs. A param reading
only the query never parses the body.

Errors: `dev_mode` is configured in the `[http_api]` section.
@simahawk
simahawk force-pushed the 18-add-http_api_dispatcher branch from e870ffa to 3120851 Compare November 16, 2026 10:45
@simahawk

Copy link
Copy Markdown
Contributor Author

/ocabot merge nobump

@OCA-git-bot

Copy link
Copy Markdown
Contributor

What a great day to merge this nice PR. Let's do it!
Prepared branch 18.0-ocabot-merge-pr-689-by-simahawk-bump-nobump, awaiting test results.

@OCA-git-bot

Copy link
Copy Markdown
Contributor

@simahawk The merge process could not be finalized, because command twine upload --disable-progress-bar --non-interactive --repository-url https://upload.pypi.org/legacy/ -u __token__ odoo_addon_http_api_dispatcher-18.0.1.0.0.2-py3-none-any.whl failed with output:

Uploading distributions to https://upload.pypi.org/legacy/
Uploading odoo_addon_http_api_dispatcher-18.0.1.0.0.2-py3-none-any.whl
�[33mWARNING �[0m Error during upload. Retry with the --verbose option for more details. 
�[31mERROR   �[0m HTTPError: 429 Too Many Requests from https://upload.pypi.org/legacy/  
         Too Many Requests                                                      

@simahawk

Copy link
Copy Markdown
Contributor Author

/ocabot merge nobump

@OCA-git-bot

Copy link
Copy Markdown
Contributor

This PR looks fantastic, let's merge it!
Prepared branch 18.0-ocabot-merge-pr-689-by-simahawk-bump-nobump, awaiting test results.

@OCA-git-bot

Copy link
Copy Markdown
Contributor

@simahawk The merge process could not be finalized, because command twine upload --disable-progress-bar --non-interactive --repository-url https://upload.pypi.org/legacy/ -u __token__ odoo_addon_http_api_dispatcher-18.0.1.0.0.2-py3-none-any.whl failed with output:

Uploading distributions to https://upload.pypi.org/legacy/
Uploading odoo_addon_http_api_dispatcher-18.0.1.0.0.2-py3-none-any.whl
�[33mWARNING �[0m Error during upload. Retry with the --verbose option for more details. 
�[31mERROR   �[0m HTTPError: 429 Too Many Requests from https://upload.pypi.org/legacy/  
         Too Many Requests                                                      

bt-admin pushed a commit to brain-tec/rest-framework that referenced this pull request Nov 20, 2026
Signed-off-by simahawk
@simahawk

Copy link
Copy Markdown
Contributor Author

/ocabot merge nobump

@OCA-git-bot

Copy link
Copy Markdown
Contributor

On my way to merge this fine PR!
Prepared branch 18.0-ocabot-merge-pr-689-by-simahawk-bump-nobump, awaiting test results.

@OCA-git-bot
OCA-git-bot merged commit c1efa30 into OCA:18.0 Nov 23, 2026
8 checks passed
@OCA-git-bot

Copy link
Copy Markdown
Contributor

Congratulations, your PR was merged at 8b0eb10. Thanks a lot for contributing to OCA. ❤️

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.

3 participants