Skip to content

Commit 558a1ce

Browse files
authored
Merge pull request #2566 from microsoft/fix/multi-line-scalar
fix: yaml multi-line literals maintain their lines
2 parents 2d9b2c5 + b053848 commit 558a1ce

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Microsoft.OpenApi.YamlReader/Microsoft.OpenApi.YamlReader.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<PrivateAssets>all</PrivateAssets>
3434
</PackageReference>
3535

36-
<PackageReference Include="SharpYaml" Version="2.1.3" />
36+
<PackageReference Include="SharpYaml" Version="2.1.4" />
3737
<PackageReference Include="System.Text.Json" Version="[8.0.5,)" />
3838
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-hh2w-p6rv-4g7w" />
3939
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-8g4q-xg66-9fp4" />

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,19 @@ private static YamlScalarNode ToYamlScalar(this JsonValue val)
151151
// Strings that look like numbers, booleans, or null need to be quoted
152152
// to preserve their string type when round-tripping
153153
var needsQuoting = NeedsQuoting(stringValue);
154+
155+
var containsNewLine = stringValue.Contains('\n');
156+
157+
var style = (needsQuoting, containsNewLine) switch
158+
{
159+
(true, _) => ScalarStyle.DoubleQuoted,
160+
(false, true) => ScalarStyle.Literal,
161+
(false, false) => ScalarStyle.Plain
162+
};
154163

155164
return new YamlScalarNode(stringValue)
156165
{
157-
Style = needsQuoting ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain
166+
Style = style
158167
};
159168
}
160169

test/Microsoft.OpenApi.Readers.Tests/YamlConverterTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,31 @@ public void BooleanPropertyNamesShouldRemainStringsFromYaml()
282282
// Then
283283
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
284284
}
285+
286+
[Fact]
287+
public void LineBreaksShouldRoundTrip()
288+
{
289+
var yamlInput =
290+
"""
291+
python: |-
292+
from openai import OpenAI
293+
294+
client = OpenAI(
295+
api_key="My API Key",
296+
)
297+
page = client.beta.assistants.list()
298+
page = page.data[0]
299+
print(page.id)
300+
""";
301+
// When
302+
var jsonNode = ConvertYamlStringToJsonNode(yamlInput);
303+
var convertedBack = jsonNode.ToYamlNode();
304+
var convertedBackOutput = ConvertYamlNodeToString(convertedBack);
305+
306+
// Then
307+
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
308+
}
309+
285310
private static JsonNode ConvertYamlStringToJsonNode(string yamlInput)
286311
{
287312
var yamlDocument = new YamlStream();

0 commit comments

Comments
 (0)