Skip to content

Commit 5fa5c65

Browse files
authored
Merge pull request #908 from VitaliyKurokhtin/apim-hexstring-notation-support
Adding support to write c-style hex strings as strings
2 parents d2b2774 + e125a56 commit 5fa5c65

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Microsoft.OpenApi/Writers/SpecialCharacterStringExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ internal static string GetYamlCompatibleString(this string input)
187187
return $"'{input}'";
188188
}
189189

190-
// If string can be mistaken as a number, a boolean, or a timestamp,
191-
// wrap it in quote to indicate that this is indeed a string, not a number, a boolean, or a timestamp
190+
// If string can be mistaken as a number, c-style hexadecimal notation, a boolean, or a timestamp,
191+
// wrap it in quote to indicate that this is indeed a string, not a number, c-style hexadecimal notation, a boolean, or a timestamp
192192
if (decimal.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out var _) ||
193+
IsHexadecimalNotation(input) ||
193194
bool.TryParse(input, out var _) ||
194195
DateTime.TryParse(input, out var _))
195196
{
@@ -225,5 +226,10 @@ internal static string GetJsonCompatibleString(this string value)
225226

226227
return $"\"{value}\"";
227228
}
229+
230+
internal static bool IsHexadecimalNotation(string input)
231+
{
232+
return input.StartsWith("0x") && int.TryParse(input.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var _);
233+
}
228234
}
229235
}

test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterSpecialCharacterTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ from inputExpected in new[] {
3838
new[]{ "Test\\Test", "\"Test\\\\Test\""},
3939
new[]{ "Test\"Test", "\"Test\\\"Test\""},
4040
new[]{ "StringsWith\"Quotes\"", "\"StringsWith\\\"Quotes\\\"\""},
41+
new[]{ "0x1234", "\"0x1234\""},
4142
}
4243
from shouldBeTerse in shouldProduceTerseOutputValues
4344
select new object[] { inputExpected[0], inputExpected[1], shouldBeTerse };
@@ -79,6 +80,7 @@ public void WriteStringWithSpecialCharactersAsJsonWorks(string input, string exp
7980
[InlineData("trailingspace ", " 'trailingspace '")]
8081
[InlineData(" trailingspace", " ' trailingspace'")]
8182
[InlineData("terminal:", " 'terminal:'")]
83+
[InlineData("0x1234", " '0x1234'")]
8284
public void WriteStringWithSpecialCharactersAsYamlWorks(string input, string expected)
8385
{
8486
// Arrange

0 commit comments

Comments
 (0)