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

allow null when parsing job.debug field #10974

Merged
merged 1 commit into from
Nov 20, 2024
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 @@ -59,6 +59,34 @@ public void DeserializeJob()
Assert.Equal("specific-sdk", jobWrapper.Job.Source.Directory);
}

[Fact]
public void DeserializeJob_DebugIsNull()
{
// the `debug` field is defined as a `bool`, but can appear as `null` in the wild
var jobContent = """
{
"job": {
"package-manager": "nuget",
"allowed-updates": [
{
"update-type": "all"
}
],
"source": {
"provider": "github",
"repo": "some-org/some-repo",
"directory": "specific-sdk",
"hostname": null,
"api-endpoint": null
},
"debug": null
}
}
""";
var jobWrapper = RunWorker.Deserialize(jobContent);
Assert.False(jobWrapper.Job.Debug);
}

[Fact]
public void DeserializeJob_FieldsNotYetSupported()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace NuGetUpdater.Core.Run.ApiModel;

public sealed record Job
{
public string PackageManager { get; init; } = "nuget";
public AllowedUpdate[]? AllowedUpdates { get; init; } = null;

[JsonConverter(typeof(NullAsBoolConverter))]
public bool Debug { get; init; } = false;
public object[]? DependencyGroups { get; init; } = null;
public object[]? Dependencies { get; init; } = null;
Expand Down Expand Up @@ -47,3 +52,21 @@ public IEnumerable<string> GetAllDirectories()
}
}
}

public class NullAsBoolConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return false;
}

return reader.GetBoolean();
}

public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}
Loading