Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>

<PackageReference Include="SharpYaml" Version="2.1.3" />
<PackageReference Include="SharpYaml" Version="2.1.4" />
<PackageReference Include="System.Text.Json" Version="[8.0.5,)" />
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-hh2w-p6rv-4g7w" />
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-8g4q-xg66-9fp4" />
Expand Down
11 changes: 10 additions & 1 deletion src/Microsoft.OpenApi.YamlReader/YamlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,19 @@ private static YamlScalarNode ToYamlScalar(this JsonValue val)
// Strings that look like numbers, booleans, or null need to be quoted
// to preserve their string type when round-tripping
var needsQuoting = NeedsQuoting(stringValue);

var containsNewLine = stringValue.Contains('\n');

var style = (needsQuoting, containsNewLine) switch
{
(true, _) => ScalarStyle.DoubleQuoted,
(false, true) => ScalarStyle.Literal,
(false, false) => ScalarStyle.Plain
};

return new YamlScalarNode(stringValue)
{
Style = needsQuoting ? ScalarStyle.DoubleQuoted : ScalarStyle.Plain
Style = style
};
}

Expand Down
25 changes: 25 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/YamlConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,31 @@ public void BooleanPropertyNamesShouldRemainStringsFromYaml()
// Then
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
}

[Fact]
public void LineBreaksShouldRoundTrip()
{
var yamlInput =
"""
python: |-
from openai import OpenAI

client = OpenAI(
api_key="My API Key",
)
page = client.beta.assistants.list()
page = page.data[0]
print(page.id)
""";
// When
var jsonNode = ConvertYamlStringToJsonNode(yamlInput);
var convertedBack = jsonNode.ToYamlNode();
var convertedBackOutput = ConvertYamlNodeToString(convertedBack);

// Then
Assert.Equal(yamlInput.MakeLineBreaksEnvironmentNeutral(), convertedBackOutput.MakeLineBreaksEnvironmentNeutral());
}

private static JsonNode ConvertYamlStringToJsonNode(string yamlInput)
{
var yamlDocument = new YamlStream();
Expand Down
Loading