diff --git a/src/libraries/System.Net.Http/ref/System.Net.Http.cs b/src/libraries/System.Net.Http/ref/System.Net.Http.cs index d5ba8339edd54..ebbbc59fea446 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -491,7 +491,7 @@ public CacheControlHeaderValue() { } public static System.Net.Http.Headers.CacheControlHeaderValue Parse(string? input) { throw null; } object System.ICloneable.Clone() { throw null; } public override string ToString() { throw null; } - public static bool TryParse(string? input, out System.Net.Http.Headers.CacheControlHeaderValue? parsedValue) { throw null; } + public static bool TryParse(string? input, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Net.Http.Headers.CacheControlHeaderValue? parsedValue) { throw null; } } public partial class ContentDispositionHeaderValue : System.ICloneable { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs index 0ced5749f6e91..0744f2995c972 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs @@ -364,17 +364,17 @@ public override int GetHashCode() public static CacheControlHeaderValue Parse(string? input) { int index = 0; - return (CacheControlHeaderValue)CacheControlHeaderParser.Parser.ParseValue(input, null, ref index); + return (CacheControlHeaderValue)CacheControlHeaderParser.Parser.ParseValue(input, null, ref index) ?? new CacheControlHeaderValue(); } - public static bool TryParse(string? input, out CacheControlHeaderValue? parsedValue) + public static bool TryParse(string? input, [NotNullWhen(true)] out CacheControlHeaderValue? parsedValue) { int index = 0; parsedValue = null; if (CacheControlHeaderParser.Parser.TryParseValue(input, null, ref index, out object? output)) { - parsedValue = (CacheControlHeaderValue?)output; + parsedValue = (CacheControlHeaderValue?)output ?? new CacheControlHeaderValue(); return true; } return false; diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs index 3e897f3f8685e..05f9120f8073c 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs @@ -628,6 +628,22 @@ public void Parse_SetOfInvalidValueStrings_Throws() CheckInvalidParse("\u4F1A", 0); } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + [InlineData(",")] + [InlineData(",,")] + [InlineData(" , , ")] + public void CacheControlHeaderValue_EmptyValue_Parsed(string value) + { + Assert.NotNull(CacheControlHeaderValue.Parse(value)); + + Assert.True(CacheControlHeaderValue.TryParse(value, out CacheControlHeaderValue headerValue)); + Assert.NotNull(headerValue); + } + [Fact] public void TryParse_SetOfValidValueStrings_ParsedCorrectly() {