Skip to content

Commit

Permalink
Fix simple scenarios for combining contexts (#6)
Browse files Browse the repository at this point in the history
* Fix simple scenarios for combining contexts

* feedback
  • Loading branch information
krwq authored Jun 7, 2022
1 parent ba3b401 commit fe6264d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,10 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe

sb.Append($@"
private static {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerContextTypeRef} context)
private {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerContextTypeRef}? context)
{{
{contextTypeRef} {JsonContextVarName} = ({contextTypeRef})context;
{JsonSerializerOptionsTypeRef} options = context.Options;
{contextTypeRef} {JsonContextVarName} = ({contextTypeRef}?)context ?? this;
{JsonSerializerOptionsTypeRef} options = {JsonContextVarName}.Options;
{JsonPropertyInfoTypeRef}[] {PropVarName} = {propertyArrayInstantiationValue};
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ protected JsonSerializerContext(JsonSerializerOptions? options, bool bindOptions
if (bindOptionsToContext)
{
options.TypeInfoResolver = this;
Debug.Assert(_options == options, "options.TypeInfoResolver setter did not assign options");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal override JsonParameterInfoValues[] GetParameterInfoValues()
{
JsonSerializerContext? context = Options.SerializerContext;
JsonParameterInfoValues[] array;
if (context == null || CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null)
if (CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null)
{
ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeCtorParams(context, Type);
return null!;
Expand All @@ -120,7 +120,7 @@ internal void AddPropertiesUsingSourceGenInfo()

JsonSerializerContext? context = Options.SerializerContext;
JsonPropertyInfo[] array;
if (context == null || PropInitFunc == null || (array = PropInitFunc(context)) == null)
if (PropInitFunc == null || (array = PropInitFunc(context!)) == null)
{
if (typeof(T) == typeof(object))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void CombiningContexts_Serialization<T>(T value, string expectedJs

public static IEnumerable<object[]> GetCombiningContextsData()
{
yield return WrapArgs(new JsonMessage { Message = "Hi" }, """{ "Message" : { "Hi" } }""");
yield return WrapArgs(new JsonMessage { Message = "Hi" }, """{ "Message" : "Hi", "Length" : 2 }""");
yield return WrapArgs(new Person("John", "Doe"), """{ "FirstName" : "John", "LastName" : "Doe" }""");
static object[] WrapArgs<T>(T value, string expectedJson) => new object[] { value, expectedJson };
}
Expand Down

0 comments on commit fe6264d

Please sign in to comment.