File tree Expand file tree Collapse file tree 2 files changed +41
-17
lines changed
src/GraphQL.Client.Serializer.SystemTextJson
tests/GraphQL.Client.Serializer.Tests Expand file tree Collapse file tree 2 files changed +41
-17
lines changed Original file line number Diff line number Diff line change @@ -19,31 +19,30 @@ public override bool CanConvert(Type typeToConvert)
1919 if ( nullableUnderlyingType != null && nullableUnderlyingType . IsValueType )
2020 return false ;
2121
22- bool result ;
2322 var constructors = typeToConvert . GetConstructors ( BindingFlags . Public | BindingFlags . Instance ) ;
2423 if ( constructors . Length != 1 )
2524 {
26- result = false ;
25+ return false ;
2726 }
28- else
27+
28+ var constructor = constructors [ 0 ] ;
29+ var parameters = constructor . GetParameters ( ) ;
30+
31+ if ( parameters . Length <= 0 )
2932 {
30- var constructor = constructors [ 0 ] ;
31- var parameters = constructor . GetParameters ( ) ;
33+ return false ;
34+ }
3235
33- if ( parameters . Length > 0 )
34- {
35- var properties = typeToConvert . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
36- result = parameters
37- . Select ( parameter => properties . Any ( p => NameOfPropertyAndParameter . Matches ( p . Name , parameter . Name , typeToConvert . IsAnonymous ( ) ) ) )
38- . All ( hasMatchingProperty => hasMatchingProperty ) ;
39- }
40- else
41- {
42- result = false ;
43- }
36+ var properties = typeToConvert . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
37+
38+ if ( properties . Length > parameters . Length )
39+ {
40+ return false ;
4441 }
4542
46- return result ;
43+ return properties
44+ . Select ( property => parameters . Any ( parameter => NameOfPropertyAndParameter . Matches ( property . Name , parameter . Name , typeToConvert . IsAnonymous ( ) ) ) )
45+ . All ( hasMatchingParameter => hasMatchingParameter ) ;
4746 }
4847
4948 public override object Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
Original file line number Diff line number Diff line change @@ -190,4 +190,29 @@ public void CanSerializeNullableStruct()
190190
191191 action . Should ( ) . NotThrow ( ) ;
192192 }
193+
194+ [ Fact ]
195+ public async Task CanDeserializeObjectWithBothConstructorAndProperties ( )
196+ {
197+ // Arrange
198+ const string jsonString = @"{ ""data"": { ""property1"": ""value1"", ""property2"": ""value2"" } }" ;
199+ var contentStream = new MemoryStream ( System . Text . Encoding . UTF8 . GetBytes ( jsonString ) ) ;
200+
201+ // Act
202+ var result = await ClientSerializer . DeserializeFromUtf8StreamAsync < WithConstructorAndProperties > ( contentStream , default ) . ConfigureAwait ( false ) ;
203+
204+ // Assert
205+ result . Data . Property1 . Should ( ) . Be ( "value1" ) ;
206+ result . Data . Property2 . Should ( ) . Be ( "value2" ) ;
207+ }
208+
209+ private class WithConstructorAndProperties
210+ {
211+ public WithConstructorAndProperties ( string property2 )
212+ {
213+ Property2 = property2 ;
214+ }
215+ public string ? Property1 { get ; set ; }
216+ public string Property2 { get ; }
217+ }
193218}
You can’t perform that action at this time.
0 commit comments