forked from OData/AspNetCoreOData
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ReplaceIllegalFieldNameCharactersAttribute, add related tests, fi…
…xes issue OData#1269
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...osoft.AspNetCore.OData/Formatter/Attributes/ReplaceIllegalFieldNameCharactersAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Microsoft.AspNetCore.OData.Formatter.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] | ||
sealed class ReplaceIllegalFieldNameCharactersAttribute : Attribute | ||
{ | ||
//constant collection of illegal characters | ||
private static readonly string[] illegalChars = new string[] { "@", ":", "." }; | ||
public string ReplaceAt { get; } | ||
public string ReplaceColon { get; } | ||
public string ReplaceDot { get; } | ||
|
||
public ReplaceIllegalFieldNameCharactersAttribute(string replaceAt, string replaceColon, string replaceDot) | ||
{ | ||
//assert that the replacements are not illegal either | ||
if (illegalChars.Contains(replaceAt) || illegalChars.Contains(replaceColon) || illegalChars.Contains(replaceDot)) | ||
{ | ||
throw new ArgumentException("Replacement characters cannot be illegal characters"); | ||
} | ||
|
||
ReplaceAt = replaceAt; | ||
ReplaceColon = replaceColon; | ||
ReplaceDot = replaceDot; | ||
} | ||
|
||
public ReplaceIllegalFieldNameCharactersAttribute(string replaceAnyIllegal) | ||
{ | ||
if (illegalChars.Contains(replaceAnyIllegal)) | ||
{ | ||
throw new ArgumentException("Replacement character cannot be an illegal character"); | ||
} | ||
|
||
ReplaceAt = replaceAnyIllegal; | ||
ReplaceColon = replaceAnyIllegal; | ||
ReplaceDot = replaceAnyIllegal; | ||
} | ||
|
||
public ReplaceIllegalFieldNameCharactersAttribute() | ||
{ | ||
ReplaceAt = "_"; | ||
ReplaceColon = "_"; | ||
ReplaceDot = "_"; | ||
} | ||
|
||
public string Replace(string fieldName) | ||
{ | ||
return fieldName.Replace("@", ReplaceAt).Replace(":", ReplaceColon).Replace(".", ReplaceDot); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters