Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String types should not be silently converted to Integer or Boolean #220

Closed
winitzki opened this issue Apr 25, 2023 · 0 comments
Closed

Comments

@winitzki
Copy link
Contributor

winitzki commented Apr 25, 2023

If a Yaml value is a String but the actual value appears to be parsable as integer or boolean, scala-yaml automatically translates it into java.lang.Integer and java.lang.Boolean. This changes the yaml schema silently and unexpectedly. With typical kubernetes Yaml where values such as "2" or "true" are of type String , the Yaml cannot be parsed after scala-yaml serializes the data.

Example code:

    val yaml1 = """  revision: "2"  """.as[Any].toOption.get
   // We expect `revision` to be of type `String` and value `"2"`, instead have an integer:
    assert(yaml1.asInstanceOf[Map[String, Any]].apply("revision").isInstanceOf[java.lang.Integer])

    val yaml2 = """  enabled: "true"  """.as[Any].toOption.get
   // We expect `enabled` to be of type `String` and value `"true"`, instead have a Boolean:
    assert(yaml2.asInstanceOf[Map[String, Any]].apply("enabled").isInstanceOf[java.lang.Boolean])

I expected it to be a String in each case, since "2" and "true" are just strings.

Not sure if this is a feature that can be turned off, please advise.

@winitzki winitzki changed the title String types should not be silently converted String types should not be silently converted to Integer or Boolean Apr 25, 2023
lwronski pushed a commit that referenced this issue May 2, 2023
…#221)

* issue220 - do not convert quoted strings into other primitive types

* more tests

* formatting

* add more unit tests

* code cleanup

* code cleanup

* more unit tests

* move tests to ScalarSuite

* minimize changes

---------

Co-authored-by: sergei.winitzki <sergei.winitzki@workday.com>
@winitzki winitzki closed this as completed May 4, 2023
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

No branches or pull requests

1 participant