netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.83k stars 2.54k forks source link

GraphQL CustomFieldChoiceSet erroring on extra_choices #17562

Open gellis713 opened 2 days ago

gellis713 commented 2 days ago

Deployment Type

Self-hosted

NetBox Version

v4.1.1

Python Version

3.12

Steps to Reproduce

1: Create a new custom field choice set called "MY_CHOICE_SET" under Customization -> Custom Field Choices. In the "Extra choices" field enter:

A:A
B:B
C:C

2: Try to execute following graphql query from the API or from the web interface. Replace MY_CHOICE_SET with

query {
  custom_field_choice_set_list {
    id
    extra_choices
  }
}

Expected Behavior

Returned JSON:

{
  "data": {
    "custom_field_choice_set_list": [
      {
        "id": "5",
        "extra_choices": [
          [
            "A",
            "A"
          ],
          [
            "B",
            "B"
          ],
          [
            "C",
            "C"
          ]
        ]
      }
    ]
  }
}

Observed Behavior

Returned JSON:

{
  "data": {
    "custom_field_choice_set_list": [
      {
        "id": "5",
        "extra_choices": null
      },
      {
        "id": "1",
        "extra_choices": null
      },
      {
        "id": "2",
        "extra_choices": null
      },
      {
        "id": "3",
        "extra_choices": null
      },
      {
        "id": "4",
        "extra_choices": null
      }
    ]
  },
  "errors": [
    {
      "message": "String cannot represent value: ['A', 'A']",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "custom_field_choice_set_list",
        0,
        "extra_choices",
        0
      ]
    }
  ]
}
gellis713 commented 2 days ago

I already have a fix for this issue which is to replace the following in netbox/extras/types.py (note extra_choices should be List[List[str]]

class CustomFieldChoiceSetType(ObjectType):

    choices_for: List[Annotated["CustomFieldType", strawberry.lazy('extras.graphql.types')]]
    extra_choices: List[str] | None

with

class CustomFieldChoiceSetType(ObjectType):

    choices_for: List[Annotated["CustomFieldType", strawberry.lazy('extras.graphql.types')]]
    extra_choices: List[List[str]] | None
arthanson commented 17 minutes ago

@gellis713 Assigning you to the issue as it looks like you have a PR. Note, I was actually getting an error on the GraphQL query:

{
  "data": {
    "custom_field_choice_set_list": [
      {
        "id": "1",
        "extra_choices": null
      }
    ]
  },
  "errors": [
    {
      "message": "String cannot represent value: ['A', 'A']",
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "custom_field_choice_set_list",
        0,
        "extra_choices",
        0
      ]
    }
  ]
}
gellis713 commented 11 minutes ago

@arthanson Yes it appears you have the same observed behavior that I posted in the issue report. Glad to see it was reproducible. Is there anything I can/should do while waiting for the PR to be reviewed? Thanks