Skip to content

💫 Toolkit to help you get started with SDD or any other process!

fix(agents): coerce a non-string description in TOML command rendering (#3799)

Ali jawwad2ef96532d22d
diff --git a/src/specify_cli/agents.py b/src/specify_cli/agents.py
index a7f40a7ef..b2861d0ad 100644
--- a/src/specify_cli/agents.py
+++ b/src/specify_cli/agents.py
@@ -302,8 +302,20 @@ class CommandRegistrar:
toml_lines = []
if "description" in frontmatter:
+ # Frontmatter comes from ``yaml.safe_load``, so ``description`` can
+ # be any YAML type: ``description:`` with no value yields None,
+ # ``description: 2`` an int, an unquoted ``true`` a bool.
+ # ``_render_basic_toml_string`` iterates the value and calls ord()
+ # on each character, so a non-string raises a raw TypeError -- and a
+ # list of single-character items is silently concatenated into a
+ # wrong value (``["a", "b"]`` -> ``"ab"``). Coerce first, matching
+ # ``render_yaml_command`` below and ``TomlIntegration
+ # ._extract_description``, which both normalise it already.
+ description = frontmatter["description"]
+ if not isinstance(description, str):
+ description = str(description) if description is not None else ""
toml_lines.append(
- f"description = {self._render_basic_toml_string(frontmatter['description'])}"
+ f"description = {self._render_basic_toml_string(description)}"
)
toml_lines.append("")
diff --git a/tests/test_extensions.py b/tests/test_extensions.py
index ad0ad9b31..4f33a9406 100644
--- a/tests/test_extensions.py
+++ b/tests/test_extensions.py
@@ -2720,6 +2720,36 @@ Real body starts here.
assert parsed["description"] == "first line\nsecond line\n"
+ @pytest.mark.parametrize(
+ ("description", "expected"),
+ [
+ (None, ""), # "description:" with no value
+ (42, "42"), # unquoted number
+ (True, "True"), # unquoted boolean
+ (["a", "b"], "['a', 'b']"), # was silently concatenated to "ab"
+ ],
+ )
+ def test_render_toml_command_coerces_non_string_description(
+ self, description, expected
+ ):
+ """Frontmatter comes from yaml.safe_load, so description can be any type.
+
+ _render_basic_toml_string iterates the value and calls ord() per
+ character, so a non-string raised a raw TypeError and a list of
+ single-character items was silently concatenated into a wrong value.
+ render_yaml_command (same class) already coerces; this brings the TOML
+ branch to parity.
+ """
+ from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar
+
+ registrar = AgentCommandRegistrar()
+ output = registrar.render_toml_command(
+ {"description": description}, "body", "extension:test-ext"
+ )
+
+ parsed = tomllib.loads(output)
+ assert parsed["description"] == expected
+
def test_render_toml_command_escapes_control_characters(self):
"""Control characters and a lone CR must be escaped so the TOML parses.