Skip to content

Conversation

richard-einfinity
Copy link
Contributor

Refactored DeleteOperation and ReplaceOperation to directly include operation.ItemId in URI construction via MakeAbsoluteUri. Updated ExecutableOperation to support an optional itemId parameter in MakeAbsoluteUri. Added MakeAbsoluteUri_WorksWithId theory test to validate various URI construction scenarios.

Refactored `DeleteOperation` and `ReplaceOperation` to directly include `operation.ItemId` in URI construction via `MakeAbsoluteUri`. Updated `ExecutableOperation` to support an optional `itemId` parameter in `MakeAbsoluteUri`. Added `MakeAbsoluteUri_WorksWithId` theory test to validate various URI construction scenarios.
@richard-einfinity
Copy link
Contributor Author

@dotnet-policy-service agree

@richard-einfinity
Copy link
Contributor Author

Resolves #124

Copy link
Collaborator

@adrianhall adrianhall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leave this as approved for 24 hours. If you want to remediate the one comment, it should be a quick fix.

}

if (baseAddress != null)
{
if (baseAddress.IsAbsoluteUri)
{
return new Uri($"{new Uri(baseAddress, relativeOrAbsoluteUri).ToString().TrimEnd('/')}/");
return new Uri($"{new Uri(baseAddress, relativeOrAbsoluteUri).ToString().TrimEnd('/')}/{itemId}");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may need to trim the slash if it is at the end. In some scenarios, the ASP.NET Core routing middleware treats a http://server/foo/ as lexically different to http://server/foo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @adrianhall,

No problem. I'll update to remove the trailing slash and update the tests. I know in my current environment all the other operations work as expected against the server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrianhall hopefully that's it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope - this definitely has an issue. See this one:

return new Uri($"{relativeOrAbsoluteUri.ToString().TrimEnd('/')}{itemId}");

If relativeOrAbsoluteUri ends with a / and itemId is set, then there is no separator.

Probably better to construct a string with the right URI in it. Then return new Uri(uri.TrimEnd('/')); at the end.

@adrianhall adrianhall added this to the 8.0.3 milestone Oct 8, 2024
@adrianhall adrianhall linked an issue Oct 8, 2024 that may be closed by this pull request
adrianhall and others added 2 commits October 8, 2024 13:35
Modified `MakeAbsoluteUri` in `ExecutableOperation.cs` to handle `itemId` more robustly by conditionally formatting it with a leading slash if it is not empty and avoiding trailing slashes before `itemId`. Updated test cases in `OfflineDbContext_Tests.cs`, `AddOperation_Tests.cs`, and `ExecutableOperation_Tests.cs` to reflect the new URI formatting rules, specifically removing trailing slashes before query parameters in the expected URIs.
}

if (baseAddress != null)
{
if (baseAddress.IsAbsoluteUri)
{
return new Uri($"{new Uri(baseAddress, relativeOrAbsoluteUri).ToString().TrimEnd('/')}/");
return new Uri($"{new Uri(baseAddress, relativeOrAbsoluteUri).ToString().TrimEnd('/')}/{itemId}");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope - this definitely has an issue. See this one:

return new Uri($"{relativeOrAbsoluteUri.ToString().TrimEnd('/')}{itemId}");

If relativeOrAbsoluteUri ends with a / and itemId is set, then there is no separator.

Probably better to construct a string with the right URI in it. Then return new Uri(uri.TrimEnd('/')); at the end.

{
itemId = string.IsNullOrWhiteSpace(itemId) ? string.Empty : $"/{itemId}";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrianhall the separator is set here if the itemId is specified.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - I get it. Although, use of IsNullOrWhiteSpace does change the semantics of the method. If someone submits " ", I believe it gets rejected before this point, so should be ok.

Assuming the tests pass, I'll merge this.

@adrianhall
Copy link
Collaborator

Explicitly:

    internal static Uri MakeAbsoluteUri(Uri? baseAddress, Uri relativeOrAbsoluteUri, string? itemId = null)
    {
        itemId ??= string.Empty;

        string uri = string.Empty;
        if (relativeOrAbsoluteUri.IsAbsoluteUri)
        {
            uri = $"{relativeOrAbsoluteUri.ToString().TrimEnd('/')}/{itemId}";
            return new Uri(uri.TrimEnd('/'));
        }

        if (baseAddress?.IsAbsoluteUri ==true)
        {
              uri = $"{new Uri(baseAddress, relativeOrAbsoluteUri).ToString().TrimEnd('/')}/{itemId}";
              return new Uri(uri.TrimEnd('/'));
        }

        throw new UriFormatException("Invalid combination of baseAddress and relativeUri");
    }

@adrianhall adrianhall merged commit 801caee into CommunityToolkit:main Oct 8, 2024
5 checks passed
@richard-einfinity richard-einfinity deleted the bugfix/UriConcatenation branch January 11, 2025 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uri's malformed when using a colon to pack partition keys.
2 participants