Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle JsonElement JsonValueKind.Null when deserializing #6481

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading