Skip to content

Conversation

@jseifeddine
Copy link
Contributor

@jseifeddine jseifeddine commented Jul 28, 2025

Fixes: #19917

Fix MAC address pagination by ensuring deterministic ordering
Add pk to MACAddress model ordering to ensure deterministic results
when multiple MAC addresses have the same value. This prevents the same
MAC address from appearing on multiple pages during pagination.

The issue occurred because Django's default ordering by 'mac_address'
alone is non-deterministic when multiple records share the same MAC
address value, causing inconsistent pagination results.

@jeremystretch jeremystretch requested review from a team and jeremystretch and removed request for a team July 28, 2025 13:47
@jseifeddine jseifeddine force-pushed the 19917-fix-mac-address-pagination-duplicates branch 2 times, most recently from 357a7b9 to 29b0285 Compare July 28, 2025 14:03
@jseifeddine jseifeddine changed the title 19917 fix mac address pagination duplicates Closes: #19917 - Fix MAC address pagination duplicates by adding 'id' to model ordering Jul 28, 2025
@jseifeddine jseifeddine changed the title Closes: #19917 - Fix MAC address pagination duplicates by adding 'id' to model ordering Fixes: #19917 - Fix MAC address pagination duplicates by adding 'id' to model ordering Jul 28, 2025
@jseifeddine jseifeddine force-pushed the 19917-fix-mac-address-pagination-duplicates branch 2 times, most recently from 3ad83da to 83b312a Compare July 28, 2025 14:48
@jseifeddine
Copy link
Contributor Author

Thanks @DanSheps

Done jseifeddine@83b312a

@jseifeddine jseifeddine changed the title Fixes: #19917 - Fix MAC address pagination duplicates by adding 'id' to model ordering Fixes: #19917 - Fix MAC address pagination duplicates by adding 'pk' to model ordering Jul 28, 2025
@jseifeddine jseifeddine requested a review from DanSheps July 28, 2025 15:34
@jseifeddine
Copy link
Contributor Author

Thanks @DanSheps

Done jseifeddine@83b312a

sorry, just saw the failed builds,
Will address this

@jseifeddine jseifeddine force-pushed the 19917-fix-mac-address-pagination-duplicates branch from 83b312a to d0331ee Compare July 28, 2025 18:58
@jseifeddine
Copy link
Contributor Author

should be good to go... thanks

@jseifeddine jseifeddine force-pushed the 19917-fix-mac-address-pagination-duplicates branch from d0331ee to 14e2028 Compare July 28, 2025 19:07
@jseifeddine
Copy link
Contributor Author

Cleaned up the tests and able to reproduce in both dcim.tests.test_filtersets and dcim.tests.test_api

System check identified no issues (0 silenced).
test_mac_address_pagination_duplicates (dcim.tests.test_filtersets.MACAddressTestCase.test_mac_address_pagination_duplicates) ... FAIL
test_mac_address_pagination_duplicates (dcim.tests.test_api.MACAddressTest.test_mac_address_pagination_duplicates) ... FAIL

======================================================================
FAIL: test_mac_address_pagination_duplicates (dcim.tests.test_filtersets.MACAddressTestCase.test_mac_address_pagination_duplicates)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/netbox/netbox/dcim/tests/test_filtersets.py", line 6959, in test_mac_address_pagination_duplicates
    self.assertEqual(len(all_ids & page_ids), 0)
AssertionError: 1 != 0

======================================================================
FAIL: test_mac_address_pagination_duplicates (dcim.tests.test_api.MACAddressTest.test_mac_address_pagination_duplicates)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/netbox/netbox/dcim/tests/test_api.py", line 2696, in test_mac_address_pagination_duplicates
    self.assertEqual(len(all_mac_ids & page_ids), 0)
AssertionError: 1 != 0

----------------------------------------------------------------------
Ran 2 tests in 0.271s

FAILED (failures=2)

with the proposed pk order fix:

test_mac_address_pagination_duplicates (dcim.tests.test_filtersets.MACAddressTestCase.test_mac_address_pagination_duplicates) ... ok
test_mac_address_pagination_duplicates (dcim.tests.test_api.MACAddressTest.test_mac_address_pagination_duplicates) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.421s

OK

@jseifeddine jseifeddine force-pushed the 19917-fix-mac-address-pagination-duplicates branch 2 times, most recently from fb76b55 to d151401 Compare July 30, 2025 03:56
@jseifeddine
Copy link
Contributor Author

@jeremystretch

I've removed the tests completely just keeping the actual fix,

netbox (19917-fix-mac-address-pagination-duplicates) ✔ ruff check netbox/ 
All checks passed!

@jseifeddine jseifeddine force-pushed the 19917-fix-mac-address-pagination-duplicates branch from d151401 to 2043bdc Compare July 31, 2025 23:05
@jseifeddine
Copy link
Contributor Author

OK @jeremystretch

I added the appropriate test, with 10 identical mac address values;

It now fails without the pk ordering fix;

Test that MAC addresses with the same value are ordered by their primary key (pk). ... FAIL

======================================================================
FAIL: test_mac_address_ordering_with_duplicates (dcim.tests.test_models.MACAddressTestCase.test_mac_address_ordering_with_duplicates)
Test that MAC addresses with the same value are ordered by their primary key (pk).
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/netbox/netbox/dcim/tests/test_models.py", line 82, in test_mac_address_ordering_with_duplicates
    self.assertLess(
AssertionError: 12 not less than 1 : MAC addresses with value 12:34:56:78:90:AB are not ordered by primary key

----------------------------------------------------------------------
Ran 1 test in 0.027s

and passes with the pk ordering fix;

Test that MAC addresses with the same value are ordered by their primary key (pk). ... ok

----------------------------------------------------------------------
Ran 1 test in 0.028s

OK

@jeremystretch jeremystretch self-requested a review August 1, 2025 12:53
jeremystretch
jeremystretch previously approved these changes Aug 4, 2025
Copy link
Member

@jeremystretch jeremystretch left a comment

Choose a reason for hiding this comment

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

Thanks @jseifeddine. I've removed the test as I don't feel it's necessary; there are many other models with a similar pattern for which we don't include an explicit regression test for ordering. With pk added to the model's default ordering this shouldn't be an issue.

jseifeddine and others added 3 commits August 4, 2025 09:31
Add 'pk' to MACAddress model ordering to ensure deterministic results
when multiple MAC addresses have the same value. This prevents the same
MAC address from appearing on multiple pages during pagination.

The issue occurred because Django's default ordering by 'mac_address'
alone is non-deterministic when multiple records share the same MAC
address value, causing inconsistent pagination results when the same
MAC address is assigned to multiple interfaces on a device.

Added regression test that verifies MAC addresses with identical values
are properly ordered by their primary key, ensuring consistent pagination
behavior across the application.

Fixes netbox-community#19917
@jeremystretch jeremystretch force-pushed the 19917-fix-mac-address-pagination-duplicates branch from 66e2b3c to 3d2cf79 Compare August 4, 2025 13:32
@jeremystretch jeremystretch self-requested a review August 4, 2025 13:48
@jeremystretch jeremystretch removed the request for review from DanSheps August 4, 2025 13:49
@DanSheps DanSheps self-requested a review August 4, 2025 14:03
@jeremystretch jeremystretch merged commit d222913 into netbox-community:main Aug 4, 2025
7 checks passed
@jseifeddine jseifeddine deleted the 19917-fix-mac-address-pagination-duplicates branch August 4, 2025 14:36
@jseifeddine jseifeddine restored the 19917-fix-mac-address-pagination-duplicates branch August 9, 2025 02:00
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent data being returned from new dcim.mac_addresses endpoint

3 participants