Skip to content

[API Proposal]: Consider support for IParsable<Uri> on System.Uri #92285

Closed

Description

@javiercn

Edited by @MihaZupan on Sep 22nd to reflect the latest API proposal shape.

Background and motivation

Since it was introduced, IParsable is implemented by most framework types to support converting from a string to the type. In Blazor we support deserializing IParsable types when processing form data, and this is one of the types we need to special case support for because they don't implement IParsable

API Proposal

namespace System;

public class Uri : IParsable<Uri> // New interface with an explicit implementation
{
    static bool IParsable<Uri>.TryParse(string? s, IFormatProvider? provider, out Uri result);
    static Uri IParsable<Uri>.Parse(string s, IFormatProvider? provider);
}

The IFormatProvider will be ignored.
The implementation will delegate to new Uri(s)/Uri.TryCreate(s).
The options used will be UriKind = UriKind.RelativeOrAbsolute, AllowImplicitFilePaths = false.

API Usage

Allows Uri to use the same generic paths in existing implementations.
Same idea as the example given for IParsable<String>:

public static IEnumerable<T> Separate<T>(string source, char separator) where T : IParsable<T>
{
    return source.Split(separator)
        .Select(x => T.Parse(x, CultureInfo.InvariantCulture));
}

Alternative Designs

  • ISpanParsable and IUtf8SpanParsable were mentioned as potential additions, but were currently rejected on the grounds that they would not provide any performance advantages over IParsable.
  • We could choose to allow AllowImplicitFilePaths.

Risks

No response

Activity

  1. added
    api-suggestion
    on Sep 19, 2023
  2. ghost added
    needs-area-label
    on Sep 19, 2023
  3. ghost added
    untriaged
    on Sep 19, 2023
  4. added and removed
    needs-area-label
    on Sep 19, 2023
  5. Sergio0694 commented on Sep 19, 2023

    @Sergio0694
    Contributor

    Shouldn't we also suggest it implementing ISpanParsable<Uri> while at it?

  6. MihaZupan commented on Sep 19, 2023

    @MihaZupan
    Member

    This seems reasonable to me, but the interface implementation should be explicit (no reason to have both Uri.TryCreate and Uri.TryParse exposed).

    We should consider having the behavior be such that it uses AllowImplicitFilePaths = false instead (once #59099 is implemented).

    Shouldn't we also suggest it implementing ISpanParsable<Uri> while at it?

    Note that Uri has a string OriginalString property on it, so we'd have to materialize the string either way.

  7. neon-sunset commented on Sep 19, 2023

    @neon-sunset
    Contributor

    Shouldn't we also suggest it implementing ISpanParsable<Uri> while at it?

    ...and IUtf8SpanParsable<Uri> too.

  8. MihaZupan commented on Sep 21, 2023

    @MihaZupan
    Member

    If the input is a valid Uri, we will be forced to allocate the original input string due to the existing API surface (OriginalString).
    As such, being able to parse from a span would only benefit cases where it's common for the input not to be valid.
    But since we're saying that the methods should work in terms of UriKind.RelativeOrAbsolute, that means almost all inputs will be valid (with rare exceptions like a scheme with over 1024 characters, an invalid hostname, ...).

    In other words, having the overload that takes a span would never help perf-wise because of the existing API shape, at which point there's no real reason to include it.

    IUtf8SpanParsable<Uri>

    What would be the use cases for this? Also, it runs into the same limitations as ISpanParsable.

  9. added this to the Future milestone on Sep 22, 2023
  10. ghost removed
    untriaged
    on Sep 22, 2023
  11. added
    api-ready-for-review
    and removed
    api-suggestion
    on Sep 22, 2023
  12. MihaZupan commented on Sep 22, 2023

    @MihaZupan
    Member

    Marking this one as ready for review with only explicit implementations of IParsable<Uri>.

  13. tannergooding commented on Sep 22, 2023

    @tannergooding
    Member

    What would be the use cases for this? Also, it runs into the same limitations as ISpanParsable.

    Can avoid an additional transcoding step. Allows it to support parsing from generic contexts. Etc

    It's basically all the same stuff everywhere. There are potential perf and usability opportunities. If those don't exist for the type, then we don't need to expose the additional interfaces. But, exposing them can often simplify things.

  14. stephentoub commented on Sep 22, 2023

    @stephentoub
    Member

    Can avoid an additional transcoding step.

    Not if it has to create a string, anyway, in support of Uri.OriginalString.

  15. tannergooding commented on Sep 22, 2023

    @tannergooding
    Member

    Not if it has to create a string, anyway, in support of Uri.OriginalString.

    That depends on how it's implemented.

    If you have a UTF8 span and only expose a string parse API, then for something like TryParse you have to first transcode the UTF8 span to a UTF16 string (also incurring an allocation) and then validate it's correct.

    If you expose TryParse(ROSpan<byte>) then you can perform the transcoding and validation simultaneously or separately (doing validation first) and only allocate the backing string on success, which is effectively a memcpy operation at that point.

  16. stephentoub commented on Sep 22, 2023

    @stephentoub
    Member

    and only allocate the backing string on success

    This is why Miha stated "being able to parse from a span would only benefit cases where it's common for the input not to be valid". Neither of us believe that to be common.

  17. bartonjs commented on Sep 26, 2023

    @bartonjs
    Member

    Video

    Looks good as proposed. Depends on #59099 being implemented first.

    When discussing if the TryParse and Parse should be implicit or explicit, we feel that having both TryCreate and TryParse is sub-optimal.

    namespace System;
    
    public class Uri : IParsable<Uri> // New interface with an explicit implementation
    {
        static bool IParsable<Uri>.TryParse(string? s, IFormatProvider? provider, out Uri result) =>
            TryCreate(uriString, RelativeOrAbsolute, NoImplicitFilePaths, out result);
        static Uri IParsable<Uri>.Parse(string s, IFormatProvider? provider) =>
            new Uri(s, RelativeOrAbsolute, NoImplicitFilePaths);
    }
  18. added
    api-approved
    and removed
    api-ready-for-review
    on Sep 26, 2023
  19. KonH commented on Oct 13, 2023

    @KonH

    @javiercn @bartonjs
    I made first draft of proposed changed, please review - #93479.
    I'm not sure how to properly handle nullability in TryParse method, so decided to just create new instance (it looks little bit better than ! usage).
    And as I see NoImplicitFilePaths is not able to be implemented right now.

  20. self-assigned this
    on Sep 16, 2026
  21. modified the milestones: Future, 12.0.0 on Sep 16, 2026
  22. added
    in-pr
    on Sep 16, 2026
  23. locked and limited conversation to collaborators on Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Netin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions