diff --git a/changelog/+escape-hfid.fixed.md b/changelog/+escape-hfid.fixed.md new file mode 100644 index 00000000..b7621ef5 --- /dev/null +++ b/changelog/+escape-hfid.fixed.md @@ -0,0 +1 @@ +- Fixed issue with improperly escaped special characters in `hfid` fields and other string values in GraphQL mutations by implementing proper JSON-style string escaping \ No newline at end of file diff --git a/infrahub_sdk/graphql.py b/infrahub_sdk/graphql.py index cf48ad83..2610e8d1 100644 --- a/infrahub_sdk/graphql.py +++ b/infrahub_sdk/graphql.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json from enum import Enum from typing import Any @@ -18,7 +19,9 @@ def convert_to_graphql_as_string(value: Any, convert_enum: bool = False) -> str: return convert_to_graphql_as_string(value=value.value, convert_enum=True) return value.name if isinstance(value, str): - return f'"{value}"' + # Use json.dumps() to properly escape the string according to JSON rules, + # which are compatible with GraphQL string escaping + return json.dumps(value) if isinstance(value, bool): return repr(value).lower() if isinstance(value, list):