Skip to content

Commit

Permalink
fix(printer): Do not print UNSET inputs as "" in directive arguments
Browse files Browse the repository at this point in the history
Fix #3761
  • Loading branch information
bellini666 committed Feb 9, 2025
1 parent 2d4234a commit 45720b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion strawberry/printer/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 36 additions & 0 deletions tests/test_printer/test_schema_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 45720b6

Please sign in to comment.