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

Do not serialize null properties #319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Octokit.GraphQL.Core.UnitTests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void InputObject_Parameter()
[Fact]
public void InputObject_Parameter_With_Null_Field()
{
var expected = "query{addComment(input:{subjectId:\"x\",body:null,clientMutationId:\"1\"}){body}}";
var expected = "query{addComment(input:{subjectId:\"x\",clientMutationId:\"1\"}){body}}";

var input = new AddCommentInput
{
Expand Down Expand Up @@ -578,7 +578,7 @@ public void Backslash_In_String_Arg_Is_Escaped()
[Fact]
public void Double_Quotes_In_InputObject_Arg_Are_Escaped()
{
var expected = "query{addComment(input:{subjectId:\"\",body:null,clientMutationId:\"string with \\\"quotes\\\" in it\"}){body}}";
var expected = "query{addComment(input:{subjectId:\"\",clientMutationId:\"string with \\\"quotes\\\" in it\"}){body}}";

var expression = new Query()
.AddComment(new AddCommentInput { ClientMutationId = "string with \"quotes\" in it" })
Expand All @@ -592,7 +592,7 @@ public void Double_Quotes_In_InputObject_Arg_Are_Escaped()
[Fact]
public void Backslash_In_InputObject_Arg_Is_Escaped()
{
var expected = "query{addComment(input:{subjectId:\"\",body:null,clientMutationId:\"string with \\\\ in it\"}){body}}";
var expected = "query{addComment(input:{subjectId:\"\",clientMutationId:\"string with \\\\ in it\"}){body}}";

var expression = new Query()
.AddComment(new AddCommentInput { ClientMutationId = "string with \\ in it" })
Expand Down
34 changes: 21 additions & 13 deletions Octokit.GraphQL.Core/Core/Serializers/QuerySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,35 @@ private void SerializeValue(StringBuilder builder, object value)
//Cache Hit
}

var openedBrace = false;

for (var index = 0; index < properties.Length; index++)
{
var property = properties[index];

if (index == 0)
{
OpenBrace(builder);
}
else
{
builder.Append(",");
}

builder.Append(property.Item1.LowerFirstCharacter()).Append(colon);
SerializeValue(builder, property.Item2.Invoke(value, null));
var propertyValue = property.Item2.Invoke(value, null);

if (index + 1 == properties.Length)
if (propertyValue != null)
{
CloseBrace(builder);
if(openedBrace)
{
builder.Append(',');
}
else
{
OpenBrace(builder);
openedBrace = true;
}

builder.Append(property.Item1.LowerFirstCharacter()).Append(colon);
SerializeValue(builder, propertyValue);
}
}

if(openedBrace)
{
CloseBrace(builder);
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions Octokit.GraphQL.UnitTests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,36 @@ ... on RepositoryOwner {


}

[Fact]
public void UpdateProjectV2ItemFieldValue_OnlyIncludesProvidedValue()
{
var expected = @"mutation {
updateProjectV2ItemFieldValue(input: {
projectId: ""ProjectId"",itemId: ""ItemId"",fieldId: ""FieldId"",value: {
singleSelectOptionId: ""OptionId""
}
}) {
clientMutationId
}
}";

var mutation = new Mutation()
.UpdateProjectV2ItemFieldValue(
new UpdateProjectV2ItemFieldValueInput
{
FieldId = new ID("FieldId"),
ProjectId = new ID("ProjectId"),
ItemId = new ID("ItemId"),
Value = new ProjectV2FieldValue
{
SingleSelectOptionId = "OptionId"
}
}
).Select(x => x.ClientMutationId)
.Compile();

Assert.Equal(expected, mutation.ToString(), ignoreLineEndingDifferences: true);
}
}
}