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

Cosmos: Escape invalid characters in the id value #25721

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/EFCore.Cosmos/ValueGeneration/Internal/IdValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,42 @@ private void AppendString(StringBuilder builder, object? propertyValue)
switch (propertyValue)
{
case string stringValue:
builder.Append(stringValue.Replace("|", "^|"));
AppendEscape(builder, stringValue);
return;
case IEnumerable enumerable:
foreach (var item in enumerable)
{
builder.Append(item.ToString()!.Replace("|", "^|"));
AppendEscape(builder, item.ToString()!);
builder.Append('|');
}

return;
case DateTime dateTime:
AppendEscape(builder, dateTime.ToString("O"));
return;
default:
builder.Append(propertyValue == null ? "null" : propertyValue.ToString()!.Replace("|", "^|"));
if (propertyValue == null)
{
builder.Append("null");
} else
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else
}
else

{
AppendEscape(builder, propertyValue.ToString()!);
}
return;
}
}

private static StringBuilder AppendEscape(StringBuilder builder, string stringValue)
{
var startingIndex = builder.Length;
return builder.Append(stringValue)
// We need this to avoid collissions with the value separator
.Replace("|", "^|", startingIndex, builder.Length - startingIndex)
// These are invalid characters, see https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.resource.id
.Replace("/", "^2F", startingIndex, builder.Length - startingIndex)
Copy link
Member

Choose a reason for hiding this comment

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

I'm missing context, but why do the extra characters need to be escaped (I get that pipe is our custom separator within generated IDs, but the others)? Is there a specific reason for the choice of caret as the escape character?

Copy link
Member Author

Choose a reason for hiding this comment

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

See https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.documents.resource.id?view=azure-dotnet for the list of invalid characters.

I've tried to URLEncode them, but it seems that the server decodes the values before storing them, so it still fails. Therefore I used the caret as an alternative encoding.

Copy link
Member

Choose a reason for hiding this comment

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

All good, thanks for the info. A link in a comment could be helpful for future readers.

.Replace("\\", "^5C", startingIndex, builder.Length - startingIndex)
.Replace("?", "^3F", startingIndex, builder.Length - startingIndex)
.Replace("#", "^23", startingIndex, builder.Length - startingIndex);
}
}
}
81 changes: 81 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/EndToEndCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,59 @@ public async Task Can_add_update_delete_end_to_end_with_Guid_async()
}
}

[ConditionalFact]
public async Task Can_add_update_delete_end_to_end_with_DateTime_async()
{
var options = Fixture.CreateOptions();

var customer = new CustomerDateTime
{
Id = DateTime.MinValue,
Name = "Theon/\\#\\\\?",
PartitionKey = 42
};

using (var context = new CustomerContextDateTime(options))
{
await context.Database.EnsureCreatedAsync();

var entry = context.Add(customer);

Assert.Equal("CustomerDateTime|0001-01-01T00:00:00.0000000|Theon^2F^5C^23^5C^5C^3F", entry.CurrentValues["__id"]);

await context.SaveChangesAsync();
}

using (var context = new CustomerContextDateTime(options))
{
var customerFromStore = await context.Set<CustomerDateTime>().SingleAsync();

Assert.Equal(customer.Id, customerFromStore.Id);
Assert.Equal("Theon/\\#\\\\?", customerFromStore.Name);

customerFromStore.Value = 23;

await context.SaveChangesAsync();
}

using (var context = new CustomerContextDateTime(options))
{
var customerFromStore = await context.Set<CustomerDateTime>().SingleAsync();

Assert.Equal(customer.Id, customerFromStore.Id);
Assert.Equal(23, customerFromStore.Value);

context.Remove(customerFromStore);

await context.SaveChangesAsync();
}

using (var context = new CustomerContextDateTime(options))
{
Assert.Empty(await context.Set<CustomerDateTime>().ToListAsync());
}
}

private class Customer
{
public int Id { get; set; }
Expand All @@ -477,6 +530,14 @@ private class CustomerGuid
public int PartitionKey { get; set; }
}

private class CustomerDateTime
{
public DateTime Id { get; set; }
public string Name { get; set; }
public int PartitionKey { get; set; }
public int Value { get; set; }
}

private class CustomerNoPartitionKey
{
public int Id { get; set; }
Expand Down Expand Up @@ -515,6 +576,26 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}

private class CustomerContextDateTime : DbContext
{
public CustomerContextDateTime(DbContextOptions dbContextOptions)
: base(dbContextOptions)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CustomerDateTime>(
cb =>
{
cb.Property(c => c.Id);
cb.Property(c => c.PartitionKey).HasConversion<string>();
cb.HasPartitionKey(c => c.PartitionKey);
cb.HasKey(c => new { c.Id, c.Name });
});
}
}

[ConditionalFact]
public async Task Can_add_update_delete_with_collections()
{
Expand Down