Repository navigation
feat: make error media type configurable - #317
Conversation
Add `exceptions.media_type` (default `application/problem+json`, so existing apps are unchanged) and send all error responses with it: errors raised by app code, request validation and security errors, and 404/405 for unknown routes, which all pass through FOCA's handler. Only JSON media types (`*/json`, `*/*+json`) without parameters are accepted, as the body is always JSON. Apps implementing specs that declare `application/json` for errors, such as many GA4GH APIs, can now match them. Closes #315 Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Declare the configured media type for the error responses of the default permissions API specification. - Send the media type exactly as configured (no charset for `text/*`). - Validate media types set after creation, too. - Simplify the media type pattern, mention the option in the 0.14 migration guide and shorten the template comment. - Test the media types explicitly, with assignment and `text/json`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Test end to end that errors of the permissions API have the configured media type, as declared in the served specification, and check the rewritten responses by status. Keep assignments unvalidated, as for all other config models: with `validate_assignment`, a failed assignment of `mapping` left the model invalid. Make the HTTP method constant private and explain when the specification is loaded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideIntroduces Sequence diagram for configurable error response media typessequenceDiagram
participant Request
participant Connexion
participant FOCA as FOCA error handler
participant App as Application
Request->>Connexion: Handle request
alt App or Connexion error
Connexion-->>FOCA: exception
FOCA->>FOCA: build_problem
FOCA-->>Request: Response(status, JSON body, conf.media_type)
else Unknown route or method
Connexion-->>FOCA: 404 or 405 error
FOCA->>FOCA: build_problem
FOCA-->>Request: Response(status, JSON body, conf.media_type)
end
Flow diagram for permissions API error media type configurationflowchart TD
Config["exceptions.media_type"] --> Register[register_permission_specs]
Register -->|default media type| DefaultSpec[Default permissions specification]
Register -->|non-default media type| Rewrite[_with_error_media_type]
Rewrite --> ServedSpec[Rewritten permissions specification]
DefaultSpec --> PermissionsAPI[Permissions API]
ServedSpec --> PermissionsAPI
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #317 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 1394 1423 +29
=========================================
+ Hits 1394 1423 +29
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/security/access_control/register_access_control.py" line_range="168" />
<code_context>
Returns:
Connexion app with updated permission specifications.
"""
+ media_type = get_app_config(app.app).exceptions.media_type
+ specification: Path | dict
if access_control_config.api_specs is None:
</code_context>
<issue_to_address>
**Permission API contract becomes stale**
When the default permissions API is registered and its media type is later changed, `register_permission_specs` rewrites the served specification once, while the exception handler reads the mutable config for each response. After the media type changes, errors use the new type but the specification still advertises the old one, so clients see a contract mismatch.
Keep the media type used by the served specification synchronized with the value the exception handler reads, or make both use the same immutable value.
Also at `foca/errors/exceptions.py:230`.
</issue_to_address>
### Comment 2
<location path="foca/security/access_control/register_access_control.py" line_range="168" />
<code_context>
Returns:
Connexion app with updated permission specifications.
"""
+ media_type = get_app_config(app.app).exceptions.media_type
+ specification: Path | dict
if access_control_config.api_specs is None:
</code_context>
<issue_to_address>
**Standalone spec registration now fails**
When a caller passes an unconfigured Connexion app, including when it supplies a custom `api_specs`, `get_app_config(app.app)` raises `RuntimeError` before `register_permission_specs` checks whether a custom specification was supplied, so callers without FOCA configuration cannot register that specification.
Only look up FOCA configuration when needed to rewrite the default specification, using the default media type if no FOCA configuration is attached.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: foca/security/access_control/register_access_control.py:168, foca/security/access_control/register_access_control.py:168
Read the error media type only for the default specification, and use the default media type for apps without FOCA configuration, so that `register_permission_specs()` works for them as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Error responses can have another media type than
application/problem+json(#315), e.g.,application/json, as declared by many GA4GH API specifications.exceptions.media_type, defaultapplication/problem+json, so existing apps are unchanged. Only JSON media types without parameters are accepted (type/jsonortype/subtype+json, lowercased), as the body is always JSON; other values fail config validation.400), security (401), unknown routes and methods (404,405), errors in request hooks. The header is set exactly as configured (no charset added fortext/*). The body is unchanged.api_specs) must declare it themselves.docs/configuration/exceptions.md, the template config, the README feature list, and a pointer in the 0.14 migration guide.Content negotiation via
Acceptwas rejected in the issue and is not implemented.Closes #315.
Verification
pytest(unit tests, MongoDB 7.0 on port 12345): 738 passed, 100% coverage. New tests: media types for app, mapped, unmapped, validation, security, 404 and 405 errors (default,application/json,text/json); valid and invalid config values; app created from a config file; permissions API errors and its served specification end to end; spec rewrite by status.ruff check,ruff format --check,mypy foca: clean.mkdocs build --strict: passes.gitleaks dir .: no leaks.tests/integration_tests.py, need the petstore example with Docker Compose); CI runs them.Content-Typefortext/*, simpler media type pattern, explicit test parameters, migration guide pointer, end-to-end test. Declined: validating assignments after creation (validate_assignment): no FOCA code assigns these fields, other config models don't validate assignments either, and with it a failedmappingassignment left the model unusable. Rewriting$refresponses: the default spec has none (documented in the docstring).Open questions
None.
🤖 Generated with Claude Code