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

Nullability annotation missing on CacheControlHeaderValue.TryParse #74061

Closed
FreeApophis opened this issue Aug 17, 2022 · 11 comments · Fixed by #74863
Closed

Nullability annotation missing on CacheControlHeaderValue.TryParse #74061

FreeApophis opened this issue Aug 17, 2022 · 11 comments · Fixed by #74863
Assignees
Labels
area-System.Net.Http bug good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@FreeApophis
Copy link

FreeApophis commented Aug 17, 2022

Description

In the namespace System.Net.Http.Headers are many very specific header classes which all have their own TryParse method.

For the following classes TryParse was annotated:
AuthenticationHeaderValue, ContentDispositionHeaderValue, ContentRangeHeaderValue, EntityTagHeaderValue, MediaTypeHeaderValue, MediaTypeWithQualityHeaderValue, NameValueHeaderValue, NameValueWithParametersHeaderValue, ProductHeaderValue, ProductInfoHeaderValue, RangeConditionHeaderValue, RangeHeaderValue, RetryConditionHeaderValue, StringWithQualityHeaderValue, TransferCodingHeaderValue, TransferCodingWithQualityHeaderValue, ViaHeaderValue, WarningHeaderValue

It seems like CacheControlHeaderValue was overlooked.

Reproduction Steps

This does not compile without a null-forgiving operator.

#nullable enable

CacheControlHeaderValue ParseHeaderOrThrow(string candidate)
    => CacheControlHeaderValue.TryParse(candidate, out var result)
        ? result
        : throw Exception("parse failed");

Expected behavior

CacheControlHeaderValue.TryParse should have correct nullability annotations like the others, to avoid the unnecessary null-forgiving operator.

Actual behavior

No nullability annotations.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 17, 2022
@ghost
Copy link

ghost commented Aug 17, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

In the namespace System.Net.Http.Headers are many very specific header classes which all have their own TryParse method.

For the following classes TryParse was annotated:
AuthenticationHeaderValue, ContentDispositionHeaderValue, ContentRangeHeaderValue, EntityTagHeaderValue, MediaTypeHeaderValue, MediaTypeWithQualityHeaderValue, NameValueHeaderValue, NameValueWithParametersHeaderValue, ProductHeaderValue, ProductInfoHeaderValue, RangeConditionHeaderValue, RangeHeaderValue, RetryConditionHeaderValue, StringWithQualityHeaderValue, TransferCodingHeaderValue, TransferCodingWithQualityHeaderValue, ViaHeaderValue, WarningHeaderValue

It seems like CacheControlHeaderValue was overlooked.

Reproduction Steps

This does not compile without a null-forgiving operator.

#nullable enable

CacheControlHeaderValue ParseHeaderOrThrow(string candidate)
    => CacheControlHeaderValue.TryParse(candidate, out var result)
        ? result
        : throw Exception("parse failed");

Expected behavior

CacheControlHeaderValue.TryParse should have correct nullability annotations like the others, to avoid the unnecessary null-forgiving operator.

Actual behavior

No nullability annotations.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: FreeApophis
Assignees: -
Labels:

area-System.Net.Http

Milestone: -

@CarnaViire
Copy link
Member

Triage: it is most possibly an oversight, but it is possible there was a reason for it. Nice to have, seems to be the first complaint since introducing nullability annotations. Moving to Future.

@CarnaViire CarnaViire added this to the Future milestone Aug 18, 2022
@CarnaViire CarnaViire removed the untriaged New issue has not been triaged by the area owner label Aug 18, 2022
@MihaZupan
Copy link
Member

MihaZupan commented Aug 18, 2022

Most header values support only a single value via TryParse, but Cache-Control is different.
For example, we will let you parse public,max-age=3600 (two values) and turn that into a single CacheControlHeaderValue.

One side-effect (I don't know whether it was intentional) is that this header will allow parsing empty values.
For example, CacheControlHeaderValue.TryParse(",, ,", out var value) will return true, but value will be null.
But Parse is not annotated correctly. CacheControlHeaderValue.Parse("") will not throw, but just return null.

So I think there are a few ways of looking at this. Either:

  • a) Allowing empty values is correct and we should mark Parse as nullable.
  • b) Allowing empty values is correct and we should start returning empty CacheControlHeaderValues objects and mark TryParse with NotNullWhen(true).
  • c) Empty values shouldn't be allowed and we should start throwing/returning false in those cases and mark TryParse with NotNullWhen(true).

Adding back the untriaged label so we can revisit.

@MihaZupan MihaZupan added bug untriaged New issue has not been triaged by the area owner labels Aug 18, 2022
@MihaZupan MihaZupan removed this from the Future milestone Aug 18, 2022
@karelz
Copy link
Member

karelz commented Aug 25, 2022

Triage: CacheControlHeaderValue is unique, not like others and its ability to accept empty string makes lives of devs hard for all other headers.
We do not like to change the annotation to Nullable -- it would break users as they would have to change their code.
Throwing for empty string is even more breaking.
Returning empty value instead of null sounds like the best option - least impactful on users. We can also change TryParse annotation to NotNullable (which will match all other headers).

@karelz karelz added this to the Future milestone Aug 25, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 25, 2022
@karelz karelz added good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors untriaged New issue has not been triaged by the area owner labels Aug 25, 2022
@karelz
Copy link
Member

karelz commented Aug 25, 2022

This should be fairly straightforward to do. It is not important for any release, but we would take a contribution.
@FreeApophis are you interested?

@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Aug 26, 2022
@pierrebelin
Copy link
Contributor

Hey @karelz, since this issue is not important and not in a rush, can I take this one as first issue to start contributing?

@karelz
Copy link
Member

karelz commented Aug 30, 2022

Great, thanks! Assigning to you @pierrebelin
Let us know if things are more complicated than we expected.

@pierrebelin
Copy link
Contributor

Of course, I'll try to do my best!

I have some issues when I try to build the project. I have installed .NET Runtime/SDK/ASP.NET Core 7.0 preview but it seems that all my dependencies are missing. Do you know what I missed ?

image

@rzikm
Copy link
Member

rzikm commented Aug 30, 2022

@pierrebelin in my experience, VS sometimes struggles with the runtime repo, but as long as it builds without errors you should be fine.

If there are build errors, can you post the log here so we can have a better idea how to help you?

Edit: I assume you have read the contributing guide and followed the steps for building libraries

@rzikm
Copy link
Member

rzikm commented Aug 31, 2022

The last BUILD: Error: line says it all, Visual Studio 2019 or 2022 with C++ tools are required. Did you try importing the .vsconfig in the Visual Studio Installer? Not sure if this thing requires a reboot.

@pierrebelin
Copy link
Contributor

pierrebelin commented Aug 31, 2022

Yes, that's why I deleted my post... I think it's too early to open correctly my eyes. I'm gonna grab a coffee

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 31, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Sep 5, 2022
MihaZupan pushed a commit that referenced this issue Sep 5, 2022
* 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
@MihaZupan MihaZupan modified the milestones: Future, 8.0.0 Sep 5, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Http bug good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants