Skip to content

Commit

Permalink
Add nullability annotation on CacheControlHeaderValue (#74863)
Browse files Browse the repository at this point in the history
* Add nullability annotation on CacheControlHeaderValue

Add nullability annotation on CacheControlHeaderValue.TryParse inside System.Net.Http

Fix #74061

* Returns empty value instead of null

* Parsing returns empty values instead of null

* Remove useless tests + null-forgiving operator

* Remove null-forgiving and add nullable return type
  • Loading branch information
pierrebelin authored Sep 5, 2022
1 parent b90a734 commit 3ec9617
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/libraries/System.Net.Http/ref/System.Net.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 3ec9617

Please sign in to comment.