Skip to content

Commit e2e15b2

Browse files
authored
Rename default method names for Parser and Validator attributes (graphql-dotnet#4056)
1 parent 841e422 commit e2e15b2

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

docs2/site/docs/migrations/migration8.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public class OutputClass1
220220
// use local private static method
221221
public static string Hello1([Parser(nameof(ParseHelloArgument))] string value) => value;
222222

223-
// use public static method from another class -- looks for ParserClass.Parser
223+
// use public static method from another class -- looks for ParserClass.Parse
224224
public static string Hello2([Parser(typeof(ParserClass))] string value) => value;
225225

226226
// use public static method from another class with a specific name

src/GraphQL.Tests/Attributes/ParserAttributeTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public void null_throws()
1616

1717
[Theory]
1818
[InlineData(typeof(Class1), "Could not find method 'InvalidMethod' on CLR type 'Class1' while initializing argument 'value'. The method must have a single parameter of type object.")]
19-
[InlineData(typeof(Class2), "Could not find method 'Parser' on CLR type 'Dummy' while initializing argument 'value'. The method must have a single parameter of type object.")]
19+
[InlineData(typeof(Class2), "Could not find method 'Parse' on CLR type 'Dummy' while initializing argument 'value'. The method must have a single parameter of type object.")]
2020
[InlineData(typeof(Class3), "Could not find method 'InvalidMethod' on CLR type 'Dummy' while initializing argument 'value'. The method must have a single parameter of type object.")]
2121
[InlineData(typeof(Class4), "Method 'InvalidMethod' on CLR type 'Class4' must have a return type of object.")]
22-
[InlineData(typeof(Class5), "Method 'Parser' on CLR type 'Dummy2' must have a return type of object.")]
22+
[InlineData(typeof(Class5), "Method 'Parse' on CLR type 'Dummy2' must have a return type of object.")]
2323
[InlineData(typeof(Class6), "Method 'InvalidMethod' on CLR type 'Dummy2' must have a return type of object.")]
2424
public void method_not_found_arguments(Type clrType, string expectedMessage)
2525
{
@@ -65,10 +65,10 @@ public class Class6
6565

6666
[Theory]
6767
[InlineData(typeof(Class1b), "Could not find method 'InvalidMethod' on CLR type 'Class1b' while initializing 'Class1b.Hello'. The method must have a single parameter of type object.")]
68-
[InlineData(typeof(Class2b), "Could not find method 'Parser' on CLR type 'Dummy' while initializing 'Class2b.Hello'. The method must have a single parameter of type object.")]
68+
[InlineData(typeof(Class2b), "Could not find method 'Parse' on CLR type 'Dummy' while initializing 'Class2b.Hello'. The method must have a single parameter of type object.")]
6969
[InlineData(typeof(Class3b), "Could not find method 'InvalidMethod' on CLR type 'Dummy' while initializing 'Class3b.Hello'. The method must have a single parameter of type object.")]
7070
[InlineData(typeof(Class4b), "Method 'InvalidMethod' on CLR type 'Class4b' must have a return type of object.")]
71-
[InlineData(typeof(Class5b), "Method 'Parser' on CLR type 'Dummy2' must have a return type of object.")]
71+
[InlineData(typeof(Class5b), "Method 'Parse' on CLR type 'Dummy2' must have a return type of object.")]
7272
[InlineData(typeof(Class6b), "Method 'InvalidMethod' on CLR type 'Dummy2' must have a return type of object.")]
7373
public void method_not_found_input_fields(Type clrType, string expectedMessage)
7474
{
@@ -120,15 +120,15 @@ public class Class6b
120120

121121
public class Dummy
122122
{
123-
public object Parser(object value) => value; // not static
124-
public static void Parser() { } // wrong signature
123+
public object Parse(object value) => value; // not static
124+
public static void Parse() { } // wrong signature
125125
public object InvalidMethod(object value) => value; // not static
126126
public static void InvalidMethod() { } // wrong signature
127127
}
128128

129129
public class Dummy2
130130
{
131-
public static string Parser(object value) => (string)value;
131+
public static string Parse(object value) => (string)value;
132132
public static string InvalidMethod(object value) => (string)value;
133133
}
134134

@@ -236,7 +236,7 @@ public class FieldTests
236236

237237
public class ParserClass
238238
{
239-
public static object Parser(object value) => (string)value + "test2";
239+
public static object Parse(object value) => (string)value + "test2";
240240
}
241241

242242
public class HelperClass

src/GraphQL.Tests/Attributes/ValidatorAttributeTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public void null_throws()
1616

1717
[Theory]
1818
[InlineData(typeof(Class1), "Could not find method 'InvalidMethod' on CLR type 'Class1' while initializing argument 'value'. The method must have a single parameter of type object.")]
19-
[InlineData(typeof(Class2), "Could not find method 'Validator' on CLR type 'Dummy' while initializing argument 'value'. The method must have a single parameter of type object.")]
19+
[InlineData(typeof(Class2), "Could not find method 'Validate' on CLR type 'Dummy' while initializing argument 'value'. The method must have a single parameter of type object.")]
2020
[InlineData(typeof(Class3), "Could not find method 'InvalidMethod' on CLR type 'Dummy' while initializing argument 'value'. The method must have a single parameter of type object.")]
2121
[InlineData(typeof(Class4), "Method 'InvalidMethod' on CLR type 'Class4' must have a void return type.")]
22-
[InlineData(typeof(Class5), "Method 'Validator' on CLR type 'Dummy2' must have a void return type.")]
22+
[InlineData(typeof(Class5), "Method 'Validate' on CLR type 'Dummy2' must have a void return type.")]
2323
[InlineData(typeof(Class6), "Method 'InvalidMethod' on CLR type 'Dummy2' must have a void return type.")]
2424
public void method_not_found_arguments(Type clrType, string expectedMessage)
2525
{
@@ -65,10 +65,10 @@ public class Class6
6565

6666
[Theory]
6767
[InlineData(typeof(Class1b), "Could not find method 'InvalidMethod' on CLR type 'Class1b' while initializing 'Class1b.Hello'. The method must have a single parameter of type object.")]
68-
[InlineData(typeof(Class2b), "Could not find method 'Validator' on CLR type 'Dummy' while initializing 'Class2b.Hello'. The method must have a single parameter of type object.")]
68+
[InlineData(typeof(Class2b), "Could not find method 'Validate' on CLR type 'Dummy' while initializing 'Class2b.Hello'. The method must have a single parameter of type object.")]
6969
[InlineData(typeof(Class3b), "Could not find method 'InvalidMethod' on CLR type 'Dummy' while initializing 'Class3b.Hello'. The method must have a single parameter of type object.")]
7070
[InlineData(typeof(Class4b), "Method 'InvalidMethod' on CLR type 'Class4b' must have a void return type.")]
71-
[InlineData(typeof(Class5b), "Method 'Validator' on CLR type 'Dummy2' must have a void return type.")]
71+
[InlineData(typeof(Class5b), "Method 'Validate' on CLR type 'Dummy2' must have a void return type.")]
7272
[InlineData(typeof(Class6b), "Method 'InvalidMethod' on CLR type 'Dummy2' must have a void return type.")]
7373
public void method_not_found_input_fields(Type clrType, string expectedMessage)
7474
{
@@ -120,15 +120,15 @@ public class Class6b
120120

121121
public class Dummy
122122
{
123-
public void Validator(object value) { _ = value; } // not static
124-
public static void Validator() { } // wrong signature
123+
public void Validate(object value) { _ = value; } // not static
124+
public static void Validate() { } // wrong signature
125125
public void InvalidMethod(object value) { _ = value; } // not static
126126
public static void InvalidMethod() { } // wrong signature
127127
}
128128

129129
public class Dummy2
130130
{
131-
public static string Validator(object value) => (string)value;
131+
public static string Validate(object value) => (string)value;
132132
public static string InvalidMethod(object value) => (string)value;
133133
}
134134

@@ -364,7 +364,7 @@ public class FieldTests
364364

365365
public class ValidatorClass
366366
{
367-
public static void Validator(object value) => throw new InvalidOperationException((string)value + "pass2");
367+
public static void Validate(object value) => throw new InvalidOperationException((string)value + "pass2");
368368
}
369369

370370
public class HelperClass

src/GraphQL/Attributes/ParserAttribute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace GraphQL;
99
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field)]
1010
public sealed class ParserAttribute : GraphQLAttribute
1111
{
12+
private const string DEFAULT_METHOD_NAME = "Parse";
13+
1214
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
1315
private readonly Type? _parserType;
1416
private readonly string _parserMethodName;
@@ -27,17 +29,17 @@ public ParserAttribute(string parserMethodName)
2729
}
2830

2931
/// <summary>
30-
/// Specifies a custom parser method for a field of an input object in a GraphQL schema using the 'Parser' method from a specified type.
32+
/// Specifies a custom parser method for a field of an input object in a GraphQL schema using the 'Parse' method from a specified type.
3133
/// The method must exist on the specified type and must be static and public.
32-
/// The method must have the signature 'object Parser(object value)'.
34+
/// The method must have the signature 'object Parse(object value)'.
3335
/// </summary>
3436
public ParserAttribute(
3537
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
3638
Type parserType)
3739
{
3840
_parserType = parserType
3941
?? throw new ArgumentNullException(nameof(parserType));
40-
_parserMethodName = nameof(FieldType.Parser);
42+
_parserMethodName = DEFAULT_METHOD_NAME;
4143
}
4244

4345
/// <summary>

src/GraphQL/Attributes/ValidateArgumentsAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace GraphQL;
1010
[AttributeUsage(AttributeTargets.Method)]
1111
public sealed class ValidateArgumentsAttribute : GraphQLAttribute
1212
{
13+
private const string DEFAULT_METHOD_NAME = "ValidateArguments";
14+
1315
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
1416
private readonly Type? _validationType;
1517
private readonly string _validationMethodName;
@@ -37,7 +39,7 @@ public ValidateArgumentsAttribute(
3739
{
3840
_validationType = validationType
3941
?? throw new ArgumentNullException(nameof(validationType));
40-
_validationMethodName = nameof(FieldType.ValidateArguments);
42+
_validationMethodName = DEFAULT_METHOD_NAME;
4143
}
4244

4345
/// <summary>

src/GraphQL/Attributes/ValidatorAttribute.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace GraphQL;
99
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
1010
public sealed class ValidatorAttribute : GraphQLAttribute
1111
{
12+
private const string DEFAULT_METHOD_NAME = "Validate";
13+
1214
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
1315
private readonly Type? _validatorType;
1416
private readonly string _validatorMethodName;
@@ -27,17 +29,17 @@ public ValidatorAttribute(string validatorMethodName)
2729
}
2830

2931
/// <summary>
30-
/// Specifies a custom validator method for a field of an input object in a GraphQL schema using the 'Validator' method from a specified type.
32+
/// Specifies a custom validator method for a field of an input object in a GraphQL schema using the 'Validate' method from a specified type.
3133
/// The method must exist on the specified type and must be static and public.
32-
/// The method must have the signature 'void Validator(object value)'.
34+
/// The method must have the signature 'void Validate(object value)'.
3335
/// </summary>
3436
public ValidatorAttribute(
3537
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
3638
Type validatorType)
3739
{
3840
_validatorType = validatorType
3941
?? throw new ArgumentNullException(nameof(validatorType));
40-
_validatorMethodName = nameof(FieldType.Validator);
42+
_validatorMethodName = DEFAULT_METHOD_NAME;
4143
}
4244

4345
/// <summary>

0 commit comments

Comments
 (0)