-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.