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

Rendering lists in XML may produce unexpected results #459

Open
jsundin opened this issue Apr 28, 2024 · 1 comment
Open

Rendering lists in XML may produce unexpected results #459

jsundin opened this issue Apr 28, 2024 · 1 comment

Comments

@jsundin
Copy link

jsundin commented Apr 28, 2024

This was discovered during evaluation, and is not a case we actually need supported. It does feel unexpected and just thought it was worth mentioning.

Given the following example.pkl file:

adminIdList = List(1, 2, 3)

We can produce the following json file:

$ pkl eval -f json example.pkl 
{
  "adminIdList": [
    1,
    2,
    3
  ]
}

However, when we try to produce the XML representation of this we get:

$ pkl eval -f xml example.pkl 
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <adminIdList>123</adminIdList>
</root>

This does seem unexpected and prone to mistakes, as it has a completely different meaning. There are tests expecting this behaviour, for example input and output.

The following XML could be the expected behaviour for me in this particular case. But honestly, lists and XML can be tricky. How would you deal with adminIdList = null in this case? And how would that solution compare to that of an empty list?

<root>
  <adminIdList>1</adminIdList>
  <adminIdList>2</adminIdList>
  <adminIdList>3</adminIdList>
</root>

The properties renderer just bails out for this case, which could also be reasonable.

The above has been tested using the following versions:

  • Pkl 0.25.3 (Linux 5.15.0-1053-aws, native)
  • Pkl 0.26.0-dev+3a31188 (Linux 5.4.0-177-generic, native)
@holzensp
Copy link
Contributor

holzensp commented Apr 29, 2024

The problem here is that the standard representation of a list of things in XML is to write out the elements consecutively. This is actually quite central in the XML spec.

Given

import "pkl:xml"

class Foo {
  bar: Int
}

elements: Listing<Foo> = new {
  new { bar = 1 }
  new { bar = 2 }
}

output {
  renderer = new xml.Renderer {}
}

you get

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <elements>
    <Foo>
      <bar>1</bar>
    </Foo>
    <Foo>
      <bar>2</bar>
    </Foo>
  </elements>
</root>

It would be unexpected, at least, to get

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <elements>
    <Foo>
      <bar>1</bar>
    </Foo>
  </elements>
  <elements>
    <Foo>
      <bar>2</bar>
    </Foo>
  </elements>
</root>

It is admittedly awkward that 1, 2, and 3 come out as 123, which is "just" because Int is a primitive type. That said, if you need repetition of the element tag, or even just line breaks to separate elements of the list, the solution is to use converters.

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

2 participants