Skip to content

fix: audit_create uses @api.model breaking model_create_multi overrides#138

Merged
gonzalesedwin1123 merged 4 commits into19.0from
fix/audit-create-model-create-multi
Mar 30, 2026
Merged

fix: audit_create uses @api.model breaking model_create_multi overrides#138
gonzalesedwin1123 merged 4 commits into19.0from
fix/audit-create-model-create-multi

Conversation

@haklyray
Copy link
Copy Markdown
Contributor

@haklyray haklyray commented Mar 25, 2026

Problem

audit_create in spp_audit/tools/decorator.py is decorated with @api.model (single-dict signature). When it calls audit_create.origin(self, vals), it passes a single dict to the next function in the origin chain. If that function is decorated with @api.model_create_multi (the standard Odoo 19 pattern for create), the wrapper is bypassed because origin calls the raw function directly.

This causes any module that overrides create with @api.model_create_multi on an audited model to have its create logic silently skipped.

Impact

  • Auto-generation of IDs in create overrides doesn't run
  • Data normalization (e.g., uppercasing) in create doesn't run
  • Default value assignment in create doesn't run
  • Any module extending create on res.partner (or other audited models) is affected

Root cause

The monkey-patch in _register_hook does:

original_method = getattr(type(RecordModel), method)
decorated_method = audit_decorator(method)
decorated_method.origin = original_method
setattr(type(RecordModel), method, decorated_method)

Because audit_create was @api.model, it received a single dict and passed it through origin() as a single dict — bypassing the @api.model_create_multi wrapper on downstream overrides.

Fix

  1. audit_create: Changed from @api.model to @api.model_create_multi so vals_list flows correctly through the origin chain. Removed dead long = int alias and isinstance(result, int | long) branch.

  2. Markup sanitization in audit_write and audit_unlink: Fixed the same class of bug — the Markup-to-string conversion only processed the first record ([0]), skipping all others in multi-record operations. Now iterates all records, matching the pattern already fixed in audit_create.

Tests added (11 new tests)

  • TestAuditCreateMulti (3 tests): Multi-create returns all records, each gets an audit log, single-dict create still works
  • TestAuditWrite (4 tests): Write produces audit log, captures old/new values, batch write logs each record, audit_in_progress context prevents duplicate logs
  • TestAuditUnlink (4 tests): Delete produces audit log, logs old values, batch delete logs each record, record is actually deleted

All 30 tests pass. Pre-commit clean.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical issue where the audit_create decorator was incorrectly configured with @api.model, causing it to bypass custom create method overrides in Odoo 19. This oversight led to silent failures in modules extending create on audited models, impacting essential functionalities like ID generation, data normalization, and default value assignment. The fix involves updating the decorator to @api.model_create_multi and adapting the method's internal logic to correctly process lists of values and records, thereby ensuring that all create overrides are properly executed and audited.

Highlights

  • API Decorator Update: Updated the audit_create method's decorator from @api.model to @api.model_create_multi to align with the standard Odoo 19 create method signature, which expects a list of value dictionaries.
  • Multi-Record Handling: Modified the audit_create method to correctly process a list of value dictionaries (vals_list) and return a list of records, ensuring compatibility with multi-record creation operations.
  • Data Type Conversion Logic: Adjusted the internal logic for converting markupsafe.Markup objects to strings to iterate over all new values in a list, rather than just the first element, to correctly handle multi-record creation scenarios.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the audit_create method in spp_audit/tools/decorator.py to handle multiple record creations by switching to @api.model_create_multi and adjusting the processing of new_values accordingly. A review comment suggests improving the robustness and efficiency of the markupsafe.Markup conversion loop by iterating through record items directly and using isinstance for type checking.

Comment thread spp_audit/tools/decorator.py Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.17%. Comparing base (b99d82c) to head (46a7365).
⚠️ Report is 9 commits behind head on 19.0.

Files with missing lines Patch % Lines
spp_audit/tools/decorator.py 81.81% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             19.0     #138      +/-   ##
==========================================
- Coverage   72.25%   72.17%   -0.08%     
==========================================
  Files         898      947      +49     
  Lines       52637    56253    +3616     
==========================================
+ Hits        38032    40602    +2570     
- Misses      14605    15651    +1046     
Flag Coverage Δ
spp_audit 64.44% <81.81%> (+0.25%) ⬆️
spp_base_common 90.26% <ø> (ø)
spp_cel_event 85.11% <ø> (ø)
spp_claim_169 58.11% <ø> (ø)
spp_dci_demo 69.23% <ø> (ø)
spp_drims 79.55% <ø> (ø)
spp_drims_sl_demo 68.48% <ø> (-0.43%) ⬇️
spp_farmer_registry 88.39% <ø> (+0.09%) ⬆️
spp_farmer_registry_cr 61.15% <ø> (+0.05%) ⬆️
spp_farmer_registry_demo 54.01% <ø> (+0.62%) ⬆️
spp_hxl 98.34% <ø> (?)
spp_hxl_area 63.74% <ø> (ø)
spp_mis_demo_v2 69.73% <ø> (-0.09%) ⬇️
spp_programs 62.23% <ø> (ø)
spp_security 66.66% <ø> (ø)
spp_starter_social_registry 0.00% <ø> (ø)
spp_starter_sp_mis 81.25% <ø> (ø)
spp_studio 62.94% <ø> (?)
spp_studio_api_v2 95.32% <ø> (?)
spp_studio_change_requests 84.67% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
spp_audit/__manifest__.py 0.00% <ø> (ø)
spp_audit/tools/decorator.py 92.30% <81.81%> (+2.51%) ⬆️

... and 57 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

emjay0921

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@emjay0921 emjay0921 left a comment

Choose a reason for hiding this comment

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

Approving — the @api.model@api.model_create_multi fix is correct and needed for Odoo 19. The Markup loop fix for batch records is also good.

One suggestion (non-blocking): consider adding a test that verifies a @api.model_create_multi override on an audited model actually runs its logic. This would prevent regressions if the audit decorator is changed again in the future.

haklyray and others added 2 commits March 26, 2026 16:09
audit_create was decorated with @api.model (single-dict signature) and
called origin(self, vals) with a single dict. This bypassed the
@api.model_create_multi wrapper on downstream create overrides, causing
their logic to be silently skipped when audit rules are active.

Changed audit_create to use @api.model_create_multi so it passes
vals_list through the origin chain correctly. Also updated the Markup
sanitization loop to handle multiple records.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@emjay0921 emjay0921 force-pushed the fix/audit-create-model-create-multi branch from 29a965b to 0530092 Compare March 26, 2026 08:15
…e/unlink

Add 11 tests covering all three audit decorator methods (create, write,
unlink) including multi-record operations, audit log verification, and
recursive audit prevention.

Fix Markup sanitization in audit_write and audit_unlink to iterate all
records instead of only the first — the same bug pattern fixed in
audit_create by the previous commit.
gonzalesedwin1123 added a commit that referenced this pull request Mar 30, 2026
Replace fragile str(type(...)) comparisons with isinstance(value, Markup)
and restructure sanitization loops to iterate each record's items directly
instead of extracting keys from the first record.

Addresses gemini-code-assist review feedback on PR #138.
Replace fragile str(type(...)) comparisons with isinstance(value, Markup)
and restructure sanitization loops to iterate each record's items directly
instead of extracting keys from the first record.

Addresses gemini-code-assist review feedback on PR #138.
@gonzalesedwin1123 gonzalesedwin1123 force-pushed the fix/audit-create-model-create-multi branch from e3ed195 to 46a7365 Compare March 30, 2026 05:50
@gonzalesedwin1123 gonzalesedwin1123 merged commit 696029f into 19.0 Mar 30, 2026
35 checks passed
@gonzalesedwin1123 gonzalesedwin1123 deleted the fix/audit-create-model-create-multi branch March 30, 2026 06:13
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.

3 participants