Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions authentik/blueprints/tests/fixtures/tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ entries:
SEQ,
!Format ["prefixed-items-%%s-%%s", !Index 0, !Value 0]
]
enumerate_nested_sequence_to_flat_sequence: !Enumerate [
[!Context sequence, !Context sequence, scalar_value, [[deeply_nested]]],
FLAT_SEQ,
!Value 0
]
enumerate_sequence_to_uniq_sequence: !Enumerate [
["foo", "bar", "foo"],
UNIQ_SEQ,
!Value 0
]
enumerate_sequence_to_mapping: !Enumerate [
!Context sequence,
MAP,
Expand Down
12 changes: 12 additions & 0 deletions authentik/blueprints/tests/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ def test_import_yaml_tags(self):
"prefixed-items-0-foo",
"prefixed-items-1-bar",
],
"enumerate_nested_sequence_to_flat_sequence": [
"foo",
"bar",
"foo",
"bar",
"scalar_value",
"deeply_nested",
],
"enumerate_sequence_to_uniq_sequence": [
"foo",
"bar",
],
"enumerate_sequence_to_mapping": {"index: 0": "foo", "index: 1": "bar"},
"nested_complex_enumeration": {
"0": {
Expand Down
12 changes: 8 additions & 4 deletions authentik/blueprints/v1/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from deepmerge import always_merger
from django.apps import apps
from django.db.models import Model, Q
from more_itertools import collapse, unique_everseen
from rest_framework.exceptions import ValidationError
from rest_framework.fields import Field
from rest_framework.serializers import Serializer
Expand Down Expand Up @@ -504,13 +505,16 @@ class Enumerate(YAMLTag, YAMLTagContext):

iterable: YAMLTag | Iterable
item_body: Any
output_body: Literal["SEQ", "MAP"]
output_body: Literal["SEQ", "FLAT_SEQ", "UNIQ_SEQ", "MAP"]

_OUTPUT_BODIES = {
"SEQ": (list, lambda a, b: [*a, b]),
"SEQ": (list, lambda a, b: [*a, b], None),
"FLAT_SEQ": (list, lambda a, b: [*a, b], lambda x: list(collapse(x))),
"UNIQ_SEQ": (list, lambda a, b: [*a, b], lambda x: list(unique_everseen(x, key=tuple))),
"MAP": (
dict,
lambda a, b: always_merger.merge(a, {b[0]: b[1]} if isinstance(b, tuple | list) else b),
None,
),
}

Expand Down Expand Up @@ -550,7 +554,7 @@ def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
iterable = tuple(enumerate(iterable))

try:
output_class, add_fn = self._OUTPUT_BODIES[self.output_body.upper()]
output_class, add_fn, post_process_fn = self._OUTPUT_BODIES[self.output_body.upper()]
except KeyError as exc:
raise EntryInvalidError.from_entry(exc, entry) from exc

Expand All @@ -570,7 +574,7 @@ def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
finally:
self.__current_context = tuple()

return result
return post_process_fn(result) if post_process_fn else result


class EnumeratedItem(YAMLTag):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"kubernetes==35.0.0",
"ldap3==2.9.1",
"lxml==6.0.2",
"more-itertools>=10.8.0",
"msgraph-sdk==1.55.0",
"opencontainers==0.0.15",
"packaging==26.0",
Expand Down
11 changes: 11 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion website/docs/customize/blueprints/v1/tags.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ This tag takes 3 arguments:
```

- **iterable**: Any Python iterable or custom tag that resolves to such iterable
- **output_object_type**: `SEQ` or `MAP`. Controls whether the returned YAML will be a mapping or a sequence.
- **output_object_type**: `SEQ`, `FLAT_SEQ`, `UNIQ_SEQ` or `MAP`. Controls whether the returned YAML will be a mapping or a sequence. With `FLAT_SEQ` the resulting list will also be flattened, while with `UNIQ_SEQ` duplicated entries will be removed
- **single_item_yaml**: The YAML to use to create a single entry in the output object

:::info
`FLAT_SEQ` and `UNIQ_SEQ` require authentik 2026.05+
:::

2. `!Index` tag:

:::info
Expand Down
Loading