Skip to content

is_expanded logic for nested fields is incorrect #48

Open
@mitchelljkotler

Description

@mitchelljkotler
  1. If I have nested fields such that a.b and b are both legal expands, is_epxanded will not distinguish between b at the top level or nested
  2. is_expanded will return true for all keys if ~all is in expanded, but ~all only expands top level expands

Proposed fix:

def is_expanded(expand, key):
    """Determine if the given key is expanded"""
    expand_fields = []

    # first split on commas to get each expand
    for full in expand.split(","):
        # than split on dots to get each component that is expanded
        parts = full.split(".")
        for i in range(len(parts)):
            # add each prefix, as each prefix is epxanded, ie
            # a.b.c will add a, a.b and a.b.c to the expand_fields list
            # we do this to differentiate a.b from b
            expand_fields.append(".".join(parts[: i + 1]))

    # ~all only expands top level fields
    if "." not in key and "~all" in expand_fields:
        return True

    return key in expand_fields

Test:

import pytest

from .utils import is_expanded

data = [
    ("a", "a", True),
    ("a", "b", False),
    ("a,b,c", "a", True),
    ("a,b,c", "b", True),
    ("a,b,c", "c", True),
    ("a,b,c", "d", False),
    ("a.b.c", "a", True),
    ("a.b.c", "a.b", True),
    ("a.b.c", "a.b.c", True),
    ("a.b.c", "b", False),
    ("a.b.c", "c", False),
    ("a.b.c", "d", False),
    ("a.b.c,d", "a", True),
    ("a.b.c,d", "d", True),
    ("~all", "a", True),
    ("~all", "a.b", False),
]


class TestIsExpanded:
    @pytest.mark.parametrize("expand,key,ret", data)
    def test_expanded(self, expand, key, ret):
        assert is_expanded(expand, key) is ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions