@@ -437,9 +437,18 @@ public static void Options_JsonSerializerContext_DoesNotFallbackToReflection()
437437 }
438438
439439 [ SkipOnTargetFramework ( TargetFrameworkMonikers . NetFramework ) ]
440- [ ConditionalFact ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
441- public static void Options_JsonSerializerContext_GetConverter_DoesNotFallBackToReflectionConverter ( )
440+ [ ConditionalTheory ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
441+ [ InlineData ( false ) ]
442+ [ InlineData ( true ) ]
443+ public static void Options_JsonSerializerContext_GetConverter_DoesNotFallBackToReflectionConverter ( bool isCompatibilitySwitchExplicitlyDisabled )
442444 {
445+ var options = new RemoteInvokeOptions ( ) ;
446+
447+ if ( isCompatibilitySwitchExplicitlyDisabled )
448+ {
449+ options . RuntimeConfigurationOptions . Add ( "System.Text.Json.Serialization.EnableSourceGenReflectionFallback" , false ) ;
450+ }
451+
443452 RemoteExecutor . Invoke ( static ( ) =>
444453 {
445454 JsonContext context = JsonContext . Default ;
@@ -460,7 +469,40 @@ public static void Options_JsonSerializerContext_GetConverter_DoesNotFallBackToR
460469 Assert . Throws < NotSupportedException > ( ( ) => context . Options . GetConverter ( typeof ( MyClass ) ) ) ;
461470 Assert . Throws < NotSupportedException > ( ( ) => JsonSerializer . Serialize ( unsupportedValue , context . Options ) ) ;
462471
463- } ) . Dispose ( ) ;
472+ } , options ) . Dispose ( ) ;
473+ }
474+
475+ [ SkipOnTargetFramework ( TargetFrameworkMonikers . NetFramework ) ]
476+ [ ConditionalFact ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
477+ public static void Options_JsonSerializerContext_Net6CompatibilitySwitch_FallsBackToReflectionResolver ( )
478+ {
479+ var options = new RemoteInvokeOptions
480+ {
481+ RuntimeConfigurationOptions =
482+ {
483+ [ "System.Text.Json.Serialization.EnableSourceGenReflectionFallback" ] = true
484+ }
485+ } ;
486+
487+ RemoteExecutor . Invoke ( static ( ) =>
488+ {
489+ var unsupportedValue = new MyClass { Value = "value" } ;
490+
491+ // JsonSerializerContext does not return metadata for the type
492+ Assert . Null ( JsonContext . Default . GetTypeInfo ( typeof ( MyClass ) ) ) ;
493+
494+ // Serialization fails using the JsonSerializerContext overload
495+ Assert . Throws < InvalidOperationException > ( ( ) => JsonSerializer . Serialize ( unsupportedValue , unsupportedValue . GetType ( ) , JsonContext . Default ) ) ;
496+
497+ // Serialization uses reflection fallback using the JsonSerializerOptions overload
498+ string json = JsonSerializer . Serialize ( unsupportedValue , JsonContext . Default . Options ) ;
499+ JsonTestHelper . AssertJsonEqual ( """{"Value":"value", "Thing":null}""" , json ) ;
500+
501+ // A converter can be resolved when looking up JsonSerializerOptions
502+ JsonConverter converter = JsonContext . Default . Options . GetConverter ( typeof ( MyClass ) ) ;
503+ Assert . IsAssignableFrom < JsonConverter < MyClass > > ( converter ) ;
504+
505+ } , options ) . Dispose ( ) ;
464506 }
465507
466508 [ Fact ]
0 commit comments