From 45720b6cd9c3032c96288f61ec48a9d72b7d84ea Mon Sep 17 00:00:00 2001 From: Thiago Bellini Ribeiro Date: Sun, 9 Feb 2025 12:29:46 +0100 Subject: [PATCH] fix(printer): Do not print UNSET inputs as "" in directive arguments Fix #3761 --- strawberry/printer/printer.py | 2 +- tests/test_printer/test_schema_directives.py | 36 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/strawberry/printer/printer.py b/strawberry/printer/printer.py index d51ae32a2c..2719c2ac25 100644 --- a/strawberry/printer/printer.py +++ b/strawberry/printer/printer.py @@ -83,7 +83,7 @@ def _serialize_dataclasses(value: object) -> object: ... def _serialize_dataclasses(value): if dataclasses.is_dataclass(value): - return dataclasses.asdict(value) # type: ignore + return {k: v for k, v in dataclasses.asdict(value).items() if v is not UNSET} # type: ignore if isinstance(value, (list, tuple)): return [_serialize_dataclasses(v) for v in value] if isinstance(value, dict): diff --git a/tests/test_printer/test_schema_directives.py b/tests/test_printer/test_schema_directives.py index e7f82fea09..70c9fe5d3c 100644 --- a/tests/test_printer/test_schema_directives.py +++ b/tests/test_printer/test_schema_directives.py @@ -712,3 +712,39 @@ def hello( schema = strawberry.Schema(query=Query) assert print_schema(schema) == textwrap.dedent(expected_output).strip() + + +def test_print_directive_with_unset_value(): + @strawberry.input + class FooInput: + a: Optional[str] = strawberry.UNSET + b: Optional[str] = strawberry.UNSET + + @strawberry.schema_directive(locations=[Location.FIELD_DEFINITION]) + class FooDirective: + input: FooInput + optional_input: Optional[FooInput] = strawberry.UNSET + + @strawberry.type + class Query: + @strawberry.field(directives=[FooDirective(input=FooInput(a="something"))]) + def foo(self, info) -> str: ... + + schema = strawberry.Schema(query=Query) + + expected_output = """ + directive @fooDirective(input: FooInput!, optionalInput: FooInput) on FIELD_DEFINITION + + type Query { + foo: String! @fooDirective(input: {a: "something"}) + } + + input FooInput { + a: String + b: String + } + """ + + schema = strawberry.Schema(query=Query) + + assert print_schema(schema) == textwrap.dedent(expected_output).strip()