Skip to content

Commit

Permalink
Handle JsonElement JsonValueKind.Null when deserializing (#6481)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
timward60 and michaelstaib committed Nov 21, 2023
1 parent 6c23914 commit e2732f1
Show file tree
Hide file tree
Showing 120 changed files with 2,839 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ private void AddDeserializeMethod(
.AddCode(jsonElementNullCheck)
.AddEmptyLine();

// When deserializing arrays of nullable values (e.g. [User] => [ { ... }, null, { ... }]) the second
// element will be not null, but instead a JSON element of kind JsonValueKind.Null.
var jsonElementNullValueKindCheck = IfBuilder
.New()
.SetCondition($"{_obj}.Value.ValueKind == System.Text.Json.JsonValueKind.Null")
.AddCode(
typeReference.IsNonNull()
? ExceptionBuilder.New(TypeNames.ArgumentNullException)
: CodeLineBuilder.From("return null;"));

methodBuilder
.AddCode(jsonElementNullValueKindCheck)
.AddEmptyLine();

AddDeserializeMethodBody(classBuilder, methodBuilder, typeReference, processed);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ public GetJsonBuilder(global::StrawberryShake.IEntityStore entityStore, global::
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _anyParser.Parse(obj.Value!);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,11 @@ public GetFooBuilder(global::StrawberryShake.IEntityStore entityStore, global::S
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var bars = new global::System.Collections.Generic.List<global::StrawberryShake.EntityIdOrData?>();
foreach (global::System.Text.Json.JsonElement child in obj.Value.EnumerateArray())
{
Expand All @@ -880,6 +885,11 @@ public GetFooBuilder(global::StrawberryShake.IEntityStore entityStore, global::S
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("Baz", global::System.StringComparison.Ordinal) ?? false)
{
Expand Down Expand Up @@ -937,6 +947,11 @@ public GetFooBuilder(global::StrawberryShake.IEntityStore entityStore, global::S
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

return _stringParser.Parse(obj.Value.GetString()!);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

global::StrawberryShake.EntityId entityId = _idSerializer.Parse(obj.Value);
entityIds.Add(entityId);
if (entityId.Name.Equals("Droid", global::System.StringComparison.Ordinal))
Expand Down Expand Up @@ -1945,6 +1950,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _stringParser.Parse(obj.Value.GetString()!);
}

Expand All @@ -1955,6 +1965,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("FriendsConnection", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -1971,6 +1986,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var characters = new global::System.Collections.Generic.List<global::StrawberryShake.EntityId?>();
foreach (global::System.Text.Json.JsonElement child in obj.Value.EnumerateArray())
{
Expand All @@ -1987,6 +2007,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

global::StrawberryShake.EntityId entityId = _idSerializer.Parse(obj.Value);
entityIds.Add(entityId);
if (entityId.Name.Equals("Droid", global::System.StringComparison.Ordinal))
Expand Down Expand Up @@ -2058,6 +2083,11 @@ public OnReviewSubBuilder(global::StrawberryShake.IEntityStore entityStore, glob
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("Review", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -2074,6 +2104,11 @@ public OnReviewSubBuilder(global::StrawberryShake.IEntityStore entityStore, glob
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _stringParser.Parse(obj.Value.GetString()!);
}

Expand All @@ -2084,6 +2119,11 @@ public OnReviewSubBuilder(global::StrawberryShake.IEntityStore entityStore, glob
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _intParser.Parse(obj.Value.GetInt32()!);
}

Expand All @@ -2094,6 +2134,11 @@ public OnReviewSubBuilder(global::StrawberryShake.IEntityStore entityStore, glob
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

return _stringParser.Parse(obj.Value.GetString()!);
}
}
Expand Down Expand Up @@ -2137,6 +2182,11 @@ public CreateReviewMutBuilder(global::StrawberryShake.IEntityStore entityStore,
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("Review", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -2153,6 +2203,11 @@ public CreateReviewMutBuilder(global::StrawberryShake.IEntityStore entityStore,
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _intParser.Parse(obj.Value.GetInt32()!);
}

Expand All @@ -2163,6 +2218,11 @@ public CreateReviewMutBuilder(global::StrawberryShake.IEntityStore entityStore,
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

return _stringParser.Parse(obj.Value.GetString()!);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

global::StrawberryShake.EntityId entityId = _idSerializer.Parse(obj.Value);
entityIds.Add(entityId);
if (entityId.Name.Equals("Droid", global::System.StringComparison.Ordinal))
Expand Down Expand Up @@ -1058,6 +1063,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("FriendsConnection", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -1074,6 +1084,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var characters = new global::System.Collections.Generic.List<global::StrawberryShake.EntityId?>();
foreach (global::System.Text.Json.JsonElement child in obj.Value.EnumerateArray())
{
Expand All @@ -1090,6 +1105,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

global::StrawberryShake.EntityId entityId = _idSerializer.Parse(obj.Value);
entityIds.Add(entityId);
if (entityId.Name.Equals("Droid", global::System.StringComparison.Ordinal))
Expand Down Expand Up @@ -1130,6 +1150,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

return _stringParser.Parse(obj.Value.GetString()!);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

global::StrawberryShake.EntityId entityId = _idSerializer.Parse(obj.Value);
entityIds.Add(entityId);
if (entityId.Name.Equals("Droid", global::System.StringComparison.Ordinal))
Expand Down Expand Up @@ -1067,6 +1072,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _stringParser.Parse(obj.Value.GetString()!);
}

Expand All @@ -1077,6 +1087,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("FriendsConnection", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -1093,6 +1108,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var characters = new global::System.Collections.Generic.List<global::StrawberryShake.EntityId?>();
foreach (global::System.Text.Json.JsonElement child in obj.Value.EnumerateArray())
{
Expand All @@ -1109,6 +1129,11 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

global::StrawberryShake.EntityId entityId = _idSerializer.Parse(obj.Value);
entityIds.Add(entityId);
if (entityId.Name.Equals("Droid", global::System.StringComparison.Ordinal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,11 @@ public GetHeroBuilder(global::StrawberryShake.IOperationResultDataFactory<global
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("Droid", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -902,6 +907,11 @@ public GetHeroBuilder(global::StrawberryShake.IOperationResultDataFactory<global
throw new global::System.ArgumentNullException();
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
throw new global::System.ArgumentNullException();
}

return _stringParser.Parse(obj.Value.GetString()!);
}

Expand All @@ -912,6 +922,11 @@ public GetHeroBuilder(global::StrawberryShake.IOperationResultDataFactory<global
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("FriendsConnection", global::System.StringComparison.Ordinal) ?? false)
{
Expand All @@ -928,6 +943,11 @@ public GetHeroBuilder(global::StrawberryShake.IOperationResultDataFactory<global
return null;
}

if (obj.Value.ValueKind == System.Text.Json.JsonValueKind.Null)
{
return null;
}

var characters = new global::System.Collections.Generic.List<global::StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsGetFriendsNoStore.State.ICharacterData?>();
foreach (global::System.Text.Json.JsonElement child in obj.Value.EnumerateArray())
{
Expand Down
Loading

0 comments on commit e2732f1

Please sign in to comment.