77using System . Text . Json ;
88using Azure . Core . Dynamic ;
99using Azure . Core . GeoJson ;
10+ using Azure . Core . Serialization ;
11+ using Microsoft . CSharp . RuntimeBinder ;
1012using NUnit . Framework ;
1113
1214namespace Azure . Core . Tests
@@ -155,6 +157,30 @@ public void CanSetNestedArrayValues()
155157 Assert . AreEqual ( 6 , ( int ) jsonData . Foo [ 2 ] ) ;
156158 }
157159
160+ [ Test ]
161+ public void CannotGetOrSetValuesOnAbsentArrays ( )
162+ {
163+ dynamic value = BinaryData . FromString ( """{"foo": [1, 2]}""" ) . ToDynamicFromJson ( DynamicCaseMapping . PascalToCamel ) ;
164+
165+ Assert . Throws < InvalidOperationException > ( ( ) => { int i = value [ 0 ] ; } ) ;
166+ Assert . Throws < InvalidOperationException > ( ( ) => { value [ 0 ] = 1 ; } ) ;
167+
168+ Assert . Throws < InvalidOperationException > ( ( ) => { int i = value . Foo [ 0 ] [ 0 ] ; } ) ;
169+ Assert . Throws < InvalidOperationException > ( ( ) => { value . Foo [ 0 ] [ 0 ] = 2 ; } ) ;
170+ }
171+
172+ [ Test ]
173+ public void CannotGetOrSetValuesOnAbsentProperties ( )
174+ {
175+ dynamic value = BinaryData . FromString ( """{"foo": 1}""" ) . ToDynamicFromJson ( DynamicCaseMapping . PascalToCamel ) ;
176+
177+ Assert . Throws < InvalidOperationException > ( ( ) => { int i = value . Foo . Bar . Baz ; } ) ;
178+ Assert . Throws < InvalidOperationException > ( ( ) => { value . Foo . Bar . Baz = "hi" ; } ) ;
179+
180+ Assert . Throws < RuntimeBinderException > ( ( ) => { int i = value . A . B . C ; } ) ;
181+ Assert . Throws < RuntimeBinderException > ( ( ) => { value . A . B . C = 1 ; } ) ;
182+ }
183+
158184 [ Test ]
159185 public void CanSetArrayValuesToDifferentTypes ( )
160186 {
@@ -196,6 +222,8 @@ public void CanGetNullPropertyValue()
196222
197223 Assert . IsNull ( ( CustomType ) jsonData . Foo ) ;
198224 Assert . IsNull ( ( int ? ) jsonData . Foo ) ;
225+ Assert . IsNull ( jsonData . Foo ) ;
226+ Assert . IsNull ( jsonData . foo ) ;
199227 }
200228
201229 [ Test ]
@@ -205,6 +233,7 @@ public void CanGetNullArrayValue()
205233
206234 Assert . IsNull ( ( CustomType ) jsonData [ 0 ] ) ;
207235 Assert . IsNull ( ( int ? ) jsonData [ 0 ] ) ;
236+ Assert . IsNull ( jsonData [ 0 ] ) ;
208237 }
209238
210239 [ Test ]
@@ -216,6 +245,8 @@ public void CanSetPropertyValueToNull()
216245
217246 Assert . IsNull ( ( CustomType ) jsonData . Foo ) ;
218247 Assert . IsNull ( ( int ? ) jsonData . Foo ) ;
248+ Assert . IsNull ( jsonData . Foo ) ;
249+ Assert . IsNull ( jsonData . foo ) ;
219250 }
220251
221252 [ Test ]
@@ -227,6 +258,7 @@ public void CanSetArrayValueToNull()
227258
228259 Assert . IsNull ( ( CustomType ) jsonData [ 0 ] ) ;
229260 Assert . IsNull ( ( int ? ) jsonData [ 0 ] ) ;
261+ Assert . IsNull ( jsonData [ 0 ] ) ;
230262 }
231263
232264 [ Test ]
@@ -445,9 +477,11 @@ public void CanCheckOptionalProperty()
445477
446478 // Property is present
447479 Assert . IsFalse ( json . Foo == null ) ;
480+ Assert . AreNotEqual ( null , json . Foo ) ;
448481
449482 // Property is absent
450483 Assert . IsTrue ( json . Bar == null ) ;
484+ Assert . AreEqual ( null , json . Bar ) ;
451485 }
452486
453487 [ Test ]
@@ -471,11 +505,15 @@ public void CanCheckOptionalPropertyWithChanges()
471505
472506 // Properties are present
473507 Assert . IsFalse ( json . Foo == null ) ;
508+ Assert . AreNotEqual ( null , json . Foo ) ;
474509 Assert . IsFalse ( json . Bar . B == null ) ;
510+ Assert . AreNotEqual ( null , json . Bar . B ) ;
475511 Assert . IsFalse ( json . Baz == null ) ;
512+ Assert . AreNotEqual ( null , json . Baz ) ;
476513
477514 // Properties are absent
478515 Assert . IsTrue ( json . Bar . A == null ) ;
516+ Assert . AreEqual ( null , json . Bar . A ) ;
479517 }
480518
481519 [ Test ]
@@ -489,6 +527,7 @@ public void CanSetOptionalProperty()
489527
490528 // Property is absent
491529 Assert . IsTrue ( json . OptionalValue == null ) ;
530+ Assert . AreEqual ( null , json . OptionalValue ) ;
492531
493532 json . OptionalValue = 5 ;
494533
@@ -837,10 +876,13 @@ public void CanDifferentiateBetweenNullAndAbsent()
837876 // GetMember binding mirrors Azure SDK models, so we allow a null check for an optional
838877 // property through the C#-style dynamic interface.
839878 Assert . IsTrue ( json . foo == null ) ;
879+ Assert . AreEqual ( null , json . foo ) ;
840880 Assert . IsTrue ( json . bar == null ) ;
881+ Assert . AreEqual ( null , json . bar ) ;
841882
842883 // Indexer lookup mimics JsonNode behavior and so throws if a property is absent.
843884 Assert . IsTrue ( json [ "foo" ] == null ) ;
885+ Assert . AreEqual ( null , json [ "foo" ] ) ;
844886 Assert . Throws < KeyNotFoundException > ( ( ) => _ = json [ "bar" ] ) ;
845887 Assert . Throws < KeyNotFoundException > ( ( ) => { if ( json [ "bar" ] == null ) { ; } } ) ;
846888 }
0 commit comments