File tree Expand file tree Collapse file tree 3 files changed +36
-2
lines changed
src/Microsoft.OpenApi.YamlReader
test/Microsoft.OpenApi.Readers.Tests Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments