forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deterministically serialize number and string JSON output
Ever since the introduction of the JsonOutputFormatter way back in 2014 [1] all fields where serialized as JSON string. Except for values looking like an int which where then serialized as JSON numbers. This had some strange unpredictable side effects like PreReleaseNumber being an empty JSON string instead of null or a "2009069" ShortSha being formatted as a JSON number. This change ensures all fields are serialized as JSON string except Major, Minor, Patch, PreReleaseNumber, WeightedPreReleaseNumber, CommitsSinceVersionSource and UncommittedChanges which are now always serialized as JSON number. Deserialisation remains the same as before for all fields so we continue to accept both JSON string and JSON number formatted values. Fixes GitTools#1688 and fixes GitTools#2304. [1] GitTools@f2daf60#diff-fde1a8ff593c6ac2ad558a6f9bb512e0350db91343b185f9b2a00d1d6e848bc3
- Loading branch information
Showing
10 changed files
with
190 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace GitVersion.OutputVariables | ||
{ | ||
public class VersionVariablesJsonModel | ||
{ | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string Major { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string Minor { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string Patch { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string PreReleaseTag { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string PreReleaseTagWithDash { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string PreReleaseLabel { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string PreReleaseLabelWithDash { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string PreReleaseNumber { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string WeightedPreReleaseNumber { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string BuildMetaData { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string BuildMetaDataPadded { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string FullBuildMetaData { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string MajorMinorPatch { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string SemVer { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string LegacySemVer { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string LegacySemVerPadded { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string AssemblySemVer { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string AssemblySemFileVer { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string FullSemVer { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string InformationalVersion { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string BranchName { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string EscapedBranchName { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string Sha { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string ShortSha { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string NuGetVersionV2 { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string NuGetVersion { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string NuGetPreReleaseTagV2 { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string NuGetPreReleaseTag { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string VersionSourceSha { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string CommitsSinceVersionSource { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string CommitsSinceVersionSourcePadded { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonNumberConverter))] | ||
public string UncommittedChanges { get; set; } | ||
[JsonConverter(typeof(VersionVariablesJsonStringConverter))] | ||
public string CommitDate { get; set; } | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/GitVersion.Core/Model/VersionVariablesJsonNumberConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Buffers.Text; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using JetBrains.Annotations; | ||
|
||
namespace GitVersion.OutputVariables | ||
{ | ||
public class VersionVariablesJsonNumberConverter : JsonConverter<string> | ||
{ | ||
public override bool CanConvert(Type typeToConvert) | ||
=> typeToConvert == typeof(string); | ||
|
||
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.Number && typeToConvert == typeof(string)) | ||
return reader.GetString() ?? ""; | ||
|
||
var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; | ||
if (Utf8Parser.TryParse(span, out long number, out var bytesConsumed) && span.Length == bytesConsumed) | ||
return number.ToString(); | ||
|
||
var data = reader.GetString(); | ||
|
||
throw new InvalidOperationException($"'{data}' is not a correct expected value!") | ||
{ | ||
Source = nameof(VersionVariablesJsonNumberConverter) | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, [CanBeNull] string value, JsonSerializerOptions options) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
{ | ||
writer.WriteNullValue(); | ||
} | ||
else if (int.TryParse(value, out var number)) | ||
{ | ||
writer.WriteNumberValue(number); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException($"'{value}' is not a correct expected value!") | ||
{ | ||
Source = nameof(VersionVariablesJsonStringConverter) | ||
}; | ||
} | ||
|
||
} | ||
|
||
public override bool HandleNull => true; | ||
|
||
private static bool NotAPaddedNumber(string value) => value != null && (value == "0" || !value.StartsWith("0")); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/GitVersion.Core/Model/VersionVariablesJsonStringConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Buffers.Text; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using JetBrains.Annotations; | ||
|
||
namespace GitVersion.OutputVariables | ||
{ | ||
public class VersionVariablesJsonStringConverter : JsonConverter<string> | ||
{ | ||
public override bool CanConvert(Type typeToConvert) | ||
=> typeToConvert == typeof(string); | ||
|
||
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.Number && typeToConvert == typeof(string)) | ||
return reader.GetString() ?? ""; | ||
|
||
var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; | ||
if (Utf8Parser.TryParse(span, out long number, out var bytesConsumed) && span.Length == bytesConsumed) | ||
return number.ToString(); | ||
|
||
var data = reader.GetString(); | ||
|
||
throw new InvalidOperationException($"'{data}' is not a correct expected value!") | ||
{ | ||
Source = nameof(VersionVariablesJsonStringConverter) | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, [CanBeNull] string value, JsonSerializerOptions options) | ||
{ | ||
value ??= string.Empty; | ||
writer.WriteStringValue(value); | ||
} | ||
|
||
public override bool HandleNull => true; | ||
|
||
private static bool NotAPaddedNumber(string value) => value != null && (value == "0" || !value.StartsWith("0")); | ||
} | ||
} |