Skip to content

Node type 'PLAIN' is not respected for strings looking like numbers or boolean values #616

Closed

Description

@edriouk

Describe the bug
A string value which can be recognized a numeric or boolean value by the default schema s emitted with suttounding double-quotes.

To Reproduce
Create a document with a node with explicit 'PLAIN' type and stringify the document :

const yamlDoc = new YAML.Document();
const rootNode = new YAML.YAMLMap();
const hexNode = new YAML.Scalar('0x002A');
hexNode.type = Scalar.PLAIN;
rootNode.set('hex', hexNode);

const output = yamlDoc.toString(); 

The output string contains value surrounded by double quotes:
hex: "0x002A"

Expected behaviour
The output string should use plain string:
hex: 0x002A

Versions (please complete the following information):

  • Environment: Node.js v20.11.1
  • yaml: 2.7.1

Additional context
Unfortunately this incorrect behavior is even demonstrated by the regression tests for custom tags:
(tests/doc/types.ts:908)
The input is correctly parsed, but emitter output is not equal to the input: double quotes are added around values

Activity

  1. added
    bug
    on Apr 14, 2025
  2. edriouk commented on Apr 14, 2025

    @edriouk
    Author

    PR #615 is created to fix this issue.

  3. edriouk commented on Apr 14, 2025

    @edriouk
    Author

    @eemeli , could you please review the PR?

  4. eemeli commented on Apr 14, 2025

    @eemeli
    Owner

    The current behaviour is intentional, as it's more important that serialising and re-parsing a scalar value will retain the scalar's type, rather than its string representation.

    Could you describe in some more detail how the current behaviour is problematic for you?

  5. edriouk commented on Apr 14, 2025

    @edriouk
    Author

    The original problem is described here: ARM-software/vscode-cmsis-csolution#217
    A Vs Code based tool modifies YAML that is intended to be parser by another application. Thus its should remain of the same schema: hex numbers must be parsed as numbers.
    Since Vs Code extension does not modify NS_APP_START_ADDRESS value, I would expect it remains the same after emitting.
    That is important because it should retains its human-readable format.
    Using 'filesafe' schema is not an option, since I need to preserve also quoting as well as empty and null values.

    I have provided a local workaround for the problem by dynamically redefining test property of the default schema to return false.

    But then I have discovered the 'custom tags' test that runs incorrectly: tagged values can represent anything and must be retained as well. Thais actually should behave as you described serialising and re-parsing a scalar value will retain the scalar's type.
    Therefore I have decided to create this issue and a proper fix for it

  6. eemeli commented on Apr 14, 2025

    @eemeli
    Owner

    Using 'filesafe' schema is not an option, since I need to preserve also quoting as well as empty and null values.

    Presuming that you mean failsafe, are you quite sure that it doesn't work for your needs? And could you show an example of how it doesn't work?

  7. edriouk commented on Apr 15, 2025

    @edriouk
    Author

    Yes,failsafe, it was a typo. Here is an example:

    const src = 'values:\n  null_val:\n';
    const doc = parseDocument(src, { schema: 'failsafe' })
    expect(String(doc)).toBe(src)  // fails 
    

    expected null value:

    values:
       null_val:
    

    received empty string:

    values:
       null_val: ""
    

    And with the following scenario it is is even worse, the code throws an exception :

    const src = 'values:\n  null_val:\n';
    const doc = parseDocument(src)
    expect(res).toBe(src)  // succeeds
    doc.schema =  new Schema({ schema: 'failsafe' })
    expect(String(doc)).toBe(src)  // exception  
    
    

    received exception :
    Tag not resolved for object value

  8. eemeli commented on Apr 15, 2025

    @eemeli
    Owner

    Looks like there's a hard-coded restriction for serialising a string value as an empty scalar. I can't immediately recall why that's in place, as it's not quite right for the failsafe schema (the only one that doesn't parse an empty scalar as null).

    The latter error you're seeing because you've parsed the document with a schema that understood the empty scalar to be null, and the failsafe schema can only represent string scalar values. The error message should be improved, as typeof null === 'object' is always surprising.

    Is the handling of empty values the only issue you've found with using the failsafe schema for your needs?

  9. edriouk commented on Apr 15, 2025

    @edriouk
    Author

    I have just checked:
    yes, handling of empty strings and null values is the only problem so far when using 'failsafe' to stringify a document.
    However, it is essential for our needs.

    Actually I would expect that for any document parse()->toString() produces the very same text by default without any conversions and quoting.

  10. eemeli commented on Apr 15, 2025

    @eemeli
    Owner

    I added fixes for the aforementioned, so closing this issues as resolved. Not quite sure when I'll push out a new release, maybe within a week or two.

    Actually I would expect that for any document parse()->toString() produces the very same text by default without any conversions and quoting.

    This is not guaranteed. The meaning will be retained with parseDocument(), but the exact serialization will not necessarily be. If you need that, you'll need to work with the document's CSV representation.

  11. edriouk commented on Apr 15, 2025

    @edriouk
    Author

    Thank you for the quick response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions