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

Json formatting/style doesn't seem to be respected when adding values/members #4246

Open
dpozinen opened this issue Jun 12, 2024 · 0 comments · May be fixed by #4341
Open

Json formatting/style doesn't seem to be respected when adding values/members #4246

dpozinen opened this issue Jun 12, 2024 · 0 comments · May be fixed by #4341
Labels
bug Something isn't working

Comments

@dpozinen
Copy link
Contributor

What version of OpenRewrite are you using?

  • OpenRewrite 2.12.0
  • Maven plugin 5.23.3

Context

I have created a recipe that adds key+value into a json. It is done via Json.Literal and is inspired by json.ChangeValue. Here's the relevant code:

JsonRightPadded<JsonKey> newKey = new JsonRightPadded<>(
    new Json.Literal(randomId(), Space.EMPTY, Markers.EMPTY, "\"" + key + "\"", key),
    Space.EMPTY,
    Markers.EMPTY
);
Json.Literal newValue = new Json.Literal(randomId(), Space.build(" ", emptyList()), Markers.EMPTY, value, unQuote(value));
Json newMember = new Json.Member(randomId(), space, Markers.EMPTY, newKey, newValue);

List<Json> members = jsonIsEmpty ? new ArrayList<>() : obj.getMembers();
members.add(newMember);
return obj.withMembers(members);

Problem

  • I don't seem to be able to control the padding after the value is inserted
  • If this isn't the only value in the object, the automatic insertion of the comma is on its own line.

My test:

...
rewriteRun(
        spec -> spec.recipe(new AddKeyValue(
            "$.", "key", "\"val\""
        )),
        //language=json
        json("""
                {
                    "l": [ 1, 2 ]
                }
            """,
            """
                {
                    "l": [ 1, 2 ],
                    "key": "val"
                }
            """)
    );
...

fails with:

expected: 
  "{
      "l": [ 1, 2 ],
      "key": "val"
  }"
 but was: 
  "{
      "l": [ 1, 2 ]
  ,
      "key": "val"}"

The same thing happens with Arrays and their Values.

Expected Behaviour

Can be seen in the test failure:

  • When adding an element the comma is added on the same line as the previous element.
  • The closing bracket is not moved from it's initial position

In general I think this is a styling issue, which I understand is not supported for json? I could not find any json.style.* classes, pretty sure TabsAndIndentsStyle for xml solves this issue. CMIIW. Thanks 🙂

@dpozinen dpozinen added the bug Something isn't working label Jun 12, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Aug 9, 2024
@timtebeek timtebeek linked a pull request Aug 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

1 participant