openwisp / openwisp/openwisp-controller

[bug] Custom command expects string type

Open
#809 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
773
Forks
315
Avg merge
2d 16h
Merged PRs (30d)
14

Description

The controller expects strings, but if you pass it a list, we get a TypeError.
For instance, if you define a custom command that has checkboxes like:

(
        'lockbands',
        {
            'label': 'Lock to Specific Bands',
            'schema': {
                'title': 'Lock Mobile Bands',
                'type': 'object',
                'properties': {
                    'bands': {
                        'type': 'array',
                        'title': 'Warning: Check that the bands you are locking to are actually available first or your device might not reconnect - Select Bands to Lock',
                        'items': {
                            'type': 'string',
                            'enum': [
                                '4G Band 1',
                                '4G Band 3',
                                '4G Band 5',
                                '4G Band 7',
                                '4G Band 8',
                                '4G Band 28',
                                '4G Band 40',
                            ]
                        },
                        'uniqueItems': True,
                        'minItems': 1
                    },
                    'modem_index': {
                        'type': 'string',
                        'title': 'Modem ID (leave blank for default)',
                    },
                },
                'required': ['bands'],
                'additionalProperties': False,
            },
            'callable': lockbands_command_callable,
        }
    ),

TypeError: sequence item 0: expected str instance, list found

There are a ton of ways to fix this, and I'm not sure if there are other architecture-level decisions that would limit the choices, but what I did is cast the argument to a string
On line 606 in openwisp_controller/connection/base/models.py
replace
return ', '.join(self.arguments)
with
return ', '.join(str(arg) for arg in self.arguments)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at line 606 in openwisp_controller/connection/base/models.py and reproduce the custom command with an array-valued argument. Confirm that list arguments no longer raise a TypeError while existing string arguments retain their behavior; the issue does not mention a specific test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, networking
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.