Skip to content

Commit 4c40617

Browse files
authored
Fixed SerializeAs SerializationType Inference (#8871)
1 parent 60f2c1e commit 4c40617

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

src/HotChocolate/Core/src/Types/Types/Composite/Directives/SerializeAs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ protected override void OnConfigure(
7979
else if (typeArg is not null && listValue is null)
8080
{
8181
listValue = [(EnumValueNode)typeArg, map[types.Current]];
82+
typeArg = null;
8283
}
8384
else
8485
{

src/HotChocolate/Core/test/Types.Tests/Types/Composite/SerializeAsTests.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,36 @@ public static async Task SerializeAs_Is_Added()
3232

3333
public class Query
3434
{
35-
[GraphQLType<NonNullType<CustomString>>]
35+
[GraphQLType<NonNullType<CustomString1>>]
3636
public string GetFoo() => "foo";
37+
38+
[GraphQLType<NonNullType<CustomString2>>]
39+
public string GetBar() => "foo";
40+
41+
[GraphQLType<NonNullType<CustomString3>>]
42+
public string GetBaz() => "foo";
3743
}
3844

3945
[SerializeAs(ScalarSerializationType.String)]
40-
public class CustomString : StringType
46+
public class CustomString1 : StringType
47+
{
48+
public CustomString1() : base("Custom1")
49+
{
50+
}
51+
}
52+
53+
[SerializeAs(ScalarSerializationType.Any)]
54+
public class CustomString2 : StringType
55+
{
56+
public CustomString2() : base("Custom2")
57+
{
58+
}
59+
}
60+
61+
[SerializeAs(ScalarSerializationType.String, "\\b\\d{3}\\b")]
62+
public class CustomString3 : StringType
4163
{
42-
public CustomString() : base("Custom")
64+
public CustomString3() : base("Custom3")
4365
{
4466
}
4567
}

src/HotChocolate/Core/test/Types.Tests/Types/Composite/__snapshots__/SerializeAsTests.SerializeAs_Is_Added.graphql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ schema {
33
}
44

55
type Query {
6-
foo: Custom!
6+
foo: Custom1!
7+
bar: Custom2!
8+
baz: Custom3!
79
}
810

911
"Defines the possible serialization types for GraphQL scalar values."
@@ -24,4 +26,8 @@ enum ScalarSerializationType {
2426

2527
directive @serializeAs("The primitive type a scalar is serialized to." type: [ScalarSerializationType!]! "The ECMA-262 regex pattern that the serialized scalar value conforms to." pattern: String) on SCALAR
2628

27-
scalar Custom @serializeAs(type: STRING)
29+
scalar Custom1 @serializeAs(type: STRING)
30+
31+
scalar Custom2 @serializeAs(type: [ STRING, BOOLEAN, INT, FLOAT, OBJECT, LIST ])
32+
33+
scalar Custom3 @serializeAs(type: STRING, pattern: "\\b\\d{3}\\b")

src/HotChocolate/Core/test/Types.Tests/Types/Composite/__snapshots__/SerializeAsTests.SerializeAs_Is_Not_Added.graphql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ schema {
33
}
44

55
type Query {
6-
foo: Custom!
6+
foo: Custom1!
7+
bar: Custom2!
8+
baz: Custom3!
79
}
810

9-
scalar Custom
11+
scalar Custom1
12+
13+
scalar Custom2
14+
15+
scalar Custom3

0 commit comments

Comments
 (0)