Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering cables by terminations in the API returns wrong results #10615

Closed
kkthxbye-code opened this issue Oct 11, 2022 · 1 comment · Fixed by #12182
Closed

Filtering cables by terminations in the API returns wrong results #10615

kkthxbye-code opened this issue Oct 11, 2022 · 1 comment · Fixed by #12182
Assignees
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@kkthxbye-code
Copy link
Contributor

kkthxbye-code commented Oct 11, 2022

NetBox version

v3.3.5

Python version

3.10

Steps to Reproduce

We discussed this on slack. Reproduction steps will be a little lackluster, but if needed I'll try to improve them.

  1. Create a device testdevice
  2. Create an interface inf1
  3. Create a rearport rp1 and a frontport fp1
  4. Remember the id of inf1 and fp1 - for the sake of this example, let's say inf1 has id 123 and fp1 has id 999.
  5. Create a cable between the interface and the frontport
  6. Try to filter for any cable with a termination with the type dcim.interface and the id 999.

https://netboxurl/api/dcim/cables/?termination_b_type=dcim.interface&termination_b_id=999

Expected Behavior

The API request should match any cables where one of the terminations has an id of 999 and the type dcim.interface.

Observed Behavior

A cable where one of the terminations is of type dcim.frontport and the id is 999.

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 140,
            "url": "https://demo.netbox.dev/api/dcim/cables/140/",
            "display": "#140",
            "type": "",
            "a_terminations": [
                {
                    "object_type": "dcim.frontport",
                    "object_id": 999,
                    "object": {
                        "id": 999,
                        "url": "https://demo.netbox.dev/api/dcim/front-ports/999/",
                        "display": "fp1",
                        "device": {
                            "id": 119,
                            "url": "https://demo.netbox.dev/api/dcim/devices/119/",
                            "display": "testdevice123",
                            "name": "testdevice123"
                        },
                        "name": "fp1",
                        "cable": 140,
                        "_occupied": true
                    }
                }
            ],
            "b_terminations": [
                {
                    "object_type": "dcim.interface",
                    "object_id": 1832,
                    "object": {
                        "id": 1832,
                        "url": "https://demo.netbox.dev/api/dcim/interfaces/1832/",
                        "display": "interface2",
                        "device": {
                            "id": 120,
                            "url": "https://demo.netbox.dev/api/dcim/devices/120/",
                            "display": "testdevice321",
                            "name": "testdevice321"
                        },
                        "name": "interface2",
                        "cable": 140,
                        "_occupied": true
                    }
                }
            ],
         ...
        }
    ]
}

The generated SQL looks like this:

SELECT DISTINCT "dcim_cable"."id",
       "dcim_cable"."created",
       "dcim_cable"."last_updated",
       "dcim_cable"."custom_field_data",
       "dcim_cable"."type",
       "dcim_cable"."status",
       "dcim_cable"."tenant_id",
       "dcim_cable"."label",
       "dcim_cable"."color",
       "dcim_cable"."length",
       "dcim_cable"."length_unit",
       "dcim_cable"."_abs_length"
  FROM "dcim_cable"
 INNER JOIN "dcim_cabletermination"
    ON ("dcim_cable"."id" = "dcim_cabletermination"."cable_id")
 INNER JOIN "django_content_type"
    ON ("dcim_cabletermination"."termination_type_id" = "django_content_type"."id")
 INNER JOIN "dcim_cabletermination" T4
    ON ("dcim_cable"."id" = T4."cable_id")
 WHERE ("django_content_type"."app_label" = 'dcim' AND "django_content_type"."model" = 'interface' AND T4."termination_id" = 999)
 ORDER BY "dcim_cable"."id" ASC
 LIMIT 50

The issue is that id and content type is matches independently, so if just one of the terminations is of the type dcim.interface it matches, regardless if that was the termination with the correct ID.

termination_b_type/termination_b_id and termination_a_type/termination_a_id also matches the same thing, not sure if this is intended and the a/b end are just for ease of visualization.

@kkthxbye-code kkthxbye-code added the type: bug A confirmed report of unexpected behavior in the application label Oct 11, 2022
@arthanson arthanson assigned arthanson and unassigned arthanson Nov 4, 2022
@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions github-actions bot added the pending closure Requires immediate attention to avoid being closed for inactivity label Jan 31, 2023
@kkthxbye-code kkthxbye-code removed the pending closure Requires immediate attention to avoid being closed for inactivity label Feb 8, 2023
@arthanson arthanson self-assigned this Apr 4, 2023
@jeremystretch jeremystretch added the status: accepted This issue has been accepted for implementation label Apr 6, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants