Skip to content

Commit 80088d4

Browse files
walidcaveliusclaude
andcommitted
fix(formatter): properly escape values in untyped additionalProperties
When formatting values for untyped additionalProperties (e.g. additionalProperties: {}), infer the schema from the Python value type so that string escaping, boolean and number formatting are handled correctly. Previously, the fallback `f'"{v}"'` would embed raw newlines in Go string literals, causing compile errors for values like multi-line strings. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1f89937 commit 80088d4

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

.generator/src/generator/formatter.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,31 @@ def format_data_with_schema_dict(
677677
**kwargs,
678678
)
679679
else:
680-
value = f'"{v}"'
680+
# Infer schema from the Python value type so primitives are formatted
681+
# correctly (e.g. strings with newlines use backtick literals, booleans
682+
# emit "true"/"false"). bool must come before int because bool is a
683+
# subclass of int in Python — format_interface would otherwise return
684+
# "True"/"False" instead of valid Go literals.
685+
# For complex types (dict, list) inferred stays {}: format_data_with_schema
686+
# short-circuits on an empty schema and returns "", so the fallback
687+
# f'"{v}"' emits a Python repr string. Not ideal but these values were
688+
# previously silently dropped, so this is strictly an improvement.
689+
if isinstance(v, bool):
690+
inferred = {"type": "boolean"}
691+
elif isinstance(v, (int, float)):
692+
inferred = {"type": "number"}
693+
elif isinstance(v, str):
694+
inferred = {"type": "string"}
695+
else:
696+
inferred = {}
697+
value = format_data_with_schema(
698+
v,
699+
inferred,
700+
name_prefix=name_prefix,
701+
replace_values=replace_values,
702+
required=True,
703+
**kwargs,
704+
) or f'"{v}"'
681705
parameters += f'"{k}": {value},\n'
682706

683707
# IMPROVE: find a better way to get nested schema name

0 commit comments

Comments
 (0)