Skip to content

Commit

Permalink
Fixed Null Value Handling for Json Resolver (#5649)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
vickytr44 and michaelstaib committed Jan 20, 2023
1 parent 53d7a12 commit b1b8445
Show file tree
Hide file tree
Showing 2 changed files with 482 additions and 71 deletions.
165 changes: 130 additions & 35 deletions src/HotChocolate/Core/src/Types.Json/JsonObjectTypeExtensions.cs
Expand Up @@ -35,25 +35,26 @@ public static class JsonObjectTypeExtensions

descriptor
.Extend()
.OnBeforeCompletion((ctx, def) =>
{
propertyName ??= def.Name;
var type = ctx.GetType<IType>(def.Type!);
var namedType = type.NamedType();
if (type.IsListType())
.OnBeforeCompletion(
(ctx, def) =>
{
throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
}
propertyName ??= def.Name;
var type = ctx.GetType<IType>(def.Type!);
var namedType = type.NamedType();
if (namedType is ScalarType scalarType)
{
InferResolver(ctx.Type, def, scalarType, propertyName);
return;
}
if (type.IsListType())
{
throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
}
throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
});
if (namedType is ScalarType scalarType)
{
InferResolver(ctx.Type, def, scalarType, propertyName);
return;
}
throw ThrowHelper.CannotInferTypeFromJsonObj(ctx.Type.Name);
});

return descriptor;
}
Expand Down Expand Up @@ -87,11 +88,12 @@ public static class JsonObjectTypeExtensions

descriptor
.Extend()
.OnBeforeCreate(def =>
{
def.ResultType = typeof(TResult);
def.PureResolver = ctx => resolve(ctx.Parent<JsonElement>());
});
.OnBeforeCreate(
def =>
{
def.ResultType = typeof(TResult);
def.PureResolver = ctx => resolve(ctx.Parent<JsonElement>());
});

return descriptor;
}
Expand All @@ -108,55 +110,148 @@ public static class JsonObjectTypeExtensions
case ScalarNames.String:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetString();
return;

case ScalarNames.Boolean:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetBoolean();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetBoolean();
};
return;

case ScalarNames.Short:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetInt16();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetInt16();
};
return;

case ScalarNames.Int:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetInt32();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetInt32();
};
return;

case ScalarNames.Long:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetInt64();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetUInt64();
};
return;

case ScalarNames.Float:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetDouble();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetDouble();
};
return;

case ScalarNames.Decimal:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetDecimal();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetDecimal();
};
return;

case ScalarNames.URL:
def.PureResolver = ctx => new Uri(ctx.GetProperty(propertyName)?.GetString()!);
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
if (property is null or { ValueKind: JsonValueKind.Null })
{
return null;
}
return new Uri(property.Value.GetString()!);
};
return;

case ScalarNames.UUID:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetGuid();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetGuid();
};
return;

case ScalarNames.Byte:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetByte();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetByte();
};
return;

case ScalarNames.ByteArray:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetBytesFromBase64();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetBytesFromBase64();
};
return;

case ScalarNames.Date:
def.PureResolver = ctx =>
{
var value = ctx.GetProperty(propertyName)?.GetString();
var property = ctx.GetProperty(propertyName);
if (value is null)
if (property is null or { ValueKind: JsonValueKind.Null })
{
return null;
}
return DateTime.Parse(
value,
property.Value.GetString()!,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
};
return;

case ScalarNames.DateTime:
def.PureResolver = ctx => ctx.GetProperty(propertyName)?.GetDateTimeOffset();
def.PureResolver = ctx =>
{
var property = ctx.GetProperty(propertyName);
return property is null or { ValueKind: JsonValueKind.Null }
? null
: property.Value.GetDateTimeOffset();
};
return;

default:
throw ThrowHelper.CannotInferTypeFromJsonObj(type.Name);
}
Expand Down

0 comments on commit b1b8445

Please sign in to comment.