Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

Problem

The YamlConverter.ToYamlNode() method was adding extra quotes to string values when converting from JSON to YAML, causing incorrect output:

var json = JsonNode.Parse(@"{
   ""fooString"": ""fooStringValue"",
   ""fooStringOfNumber"": ""200""
}");

var yamlNode = json.ToYamlNode();
// Writes to YAML...

Previous (Incorrect) Output:

fooString: '"fooStringValue"'
fooStringOfNumber: '"200"'

Expected Output:

fooString: fooStringValue
fooStringOfNumber: "200"

Root Cause

The ToYamlScalar method was using JsonValue.ToJsonString() to extract values. This method returns the JSON representation of a value, which includes quotes around strings. When these quoted strings were passed to YamlScalarNode, the YAML serializer treated the quotes as part of the actual string content and escaped them, resulting in double-quoted output.

Solution

Modified ToYamlScalar to:

  1. Extract actual values using TryGetValue<string>() instead of ToJsonString() to get the underlying value without JSON formatting
  2. Apply intelligent quoting based on value type:
    • Regular strings (e.g., "fooStringValue") → output unquoted as fooStringValue
    • Strings that look like numbers/booleans/null (e.g., "200", "true", "null") → output quoted as "200", "true", "null" to preserve type information during round-tripping
    • Strings with line breaks → automatically quoted to preserve formatting
    • Actual numbers and booleans → output unquoted as 200, true

This ensures proper type preservation when converting between JSON and YAML while producing clean, readable YAML output.

Testing

Added 11 comprehensive unit tests covering:

  • Regular strings (should be unquoted)
  • Strings that look like numbers (should be quoted to preserve string type)
  • Actual numeric values (should be unquoted)
  • Strings that look like booleans (should be quoted)
  • Actual boolean values (should be unquoted)
  • Strings that look like null (should be quoted)
  • Mixed type documents
  • The exact scenario from the issue report
  • Strings with line breaks (should preserve line breaks during round-tripping)

All existing tests continue to pass with no regressions.

Fixes #1951

Original prompt

This section details on the original issue you should resolve

<issue_title>YamlConverter from JsonNode to Yaml adds extra quotes</issue_title>
<issue_description>Describe the bug
Given the following input

{
   "fooString": "fooStringValue",
   "fooStringOfNumber": "200"
}

calling

var yamlNode = jsonNode.ToYamlNode();
using var ms = new MemoryStream();
var yamlStream = new YamlStream(new YamlDocument(jsonNode.ToYamlNode()));
var writer = new StreamWriter(outputStream);
yamlStream.Save(writer);
ms.Seek(0);

And reading the string value leads to the following result

fooString: '"fooStringValue"'
fooStringOfNumber: 200

Expected behavior
A clear and concise description of what you expected to happen.

fooString: fooStringValue
fooStringOfNumber: "200"
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #2561


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix YamlConverter to prevent extra quotes in output Fix YamlConverter adding extra quotes to string values when converting from JSON to YAML Oct 22, 2025
Copilot AI requested a review from baywet October 22, 2025 20:55
Copilot finished work on behalf of baywet October 22, 2025 20:55
@baywet
Copy link
Member

baywet commented Oct 23, 2025

@copilot also add a unit test to validate the conversion respects line breaks in string values

Copy link
Contributor Author

Copilot AI commented Oct 23, 2025

@copilot also add a unit test to validate the conversion respects line breaks in string values

Added unit test ToYamlNode_StringWithLineBreaks_PreservesLineBreaks() that validates line breaks are correctly preserved during JSON-to-YAML conversion and round-tripping. The test verifies that strings containing \n characters maintain their line breaks when converted to YAML and back to JSON. Commit: 42c805e

Copilot finished work on behalf of baywet October 23, 2025 11:18
@sonarqubecloud
Copy link

@baywet baywet marked this pull request as ready for review October 23, 2025 17:16
@baywet baywet requested a review from a team as a code owner October 23, 2025 17:16
@baywet baywet enabled auto-merge October 23, 2025 17:16
@baywet baywet merged commit ccfebc8 into main Oct 23, 2025
17 checks passed
@baywet baywet deleted the copilot/fix-yamlconverter-quote-issue branch October 23, 2025 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

YamlConverter from JsonNode to Yaml adds extra quotes Calling OpenApiDocument.Parse without the format parameter should not fail

3 participants