Skip to content

Fix spaceBeforeSeparator in Jackson Gradle plugin#2103

Merged
nedtwigg merged 14 commits intodiffplug:mainfrom
fandreuz:fix-space-before-separator-jackson
Sep 23, 2025
Merged

Fix spaceBeforeSeparator in Jackson Gradle plugin#2103
nedtwigg merged 14 commits intodiffplug:mainfrom
fandreuz:fix-space-before-separator-jackson

Conversation

@fandreuz
Copy link
Copy Markdown
Contributor

@fandreuz fandreuz commented Apr 30, 2024

spaceBeforeSeparator setter in JsonExtension

Hi, I was trying to set a custom value for spaceBeforeSeparator in Gradle Kotlin DSL, as shown in the Gradle plugin example for the Jackson formatted. However, the relevant property from JacksonJsonConfig was not exposed to JsonExtension.

spaceBeforeSeparator always false

After tweaking the plugin with a local snapshot to add the relevant setter, I noticed that the rule that I was trying to achieve (i.e. spaceBeforeSeparator=true) was not respected by spotless. The effective behavior was that expected with spaceBeforeSeparator=false. I digged a little bit more:

at com.diffplug.spotless.glue.json.JacksonJsonFormatterFunc$SpotlessJsonPrettyPrinter.withSeparators(JacksonJsonFormatterFunc.java:107)
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter.<init>(DefaultPrettyPrinter.java:128)
at com.fasterxml.jackson.core.util.DefaultPrettyPrinter.<init>(DefaultPrettyPrinter.java:104)
at com.diffplug.spotless.glue.json.JacksonJsonFormatterFunc$SpotlessJsonPrettyPrinter.<init>(JacksonJsonFormatterFunc.java:92)
at com.diffplug.spotless.glue.json.JacksonJsonFormatterFunc$SpotlessJsonPrettyPrinter.createInstance(JacksonJsonFormatterFunc.java:100)
at com.diffplug.spotless.glue.json.JacksonJsonFormatterFunc$SpotlessJsonPrettyPrinter.createInstance(JacksonJsonFormatterFunc.java:88)
at com.fasterxml.jackson.databind.ObjectWriter$GeneratorSettings.initialize(ObjectWriter.java:1438)
at com.fasterxml.jackson.databind.ObjectWriter._configureGenerator(ObjectWriter.java:1312)
at com.fasterxml.jackson.databind.ObjectWriter.createGenerator(ObjectWriter.java:751)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1140)
...

SpotlessJsonPrettyPrinter#withSeparators(Separators) is responsible for tweaking _objectFieldValueSeparatorWithSpaces according to the user-provided value of spaceBeforeSeparator. However, this method is called by the default constructor of DefaultPrettyPrinter, before we have a chance to set the value of the field spaceBeforeSeparator. Thus, SpotlessJsonPrettyPrinter#spaceBeforeSeparator is always false when SpotlessJsonPrettyPrinter#withSeparators(Separators) is called.

PR content

In this PR, I address the following:

  • The problem reported in the first paragraph (changes in JsonExtension);
  • The problem reported in the remaining paragraphs (changes in JacksonJsonFormatterFunc).

Let me know if I'm missing something, I've tried the tweaked plugin and I observed the expected behavior.

@nedtwigg
Copy link
Copy Markdown
Member

Thanks for the PR!

  • needs a spotlessApply
  • needs to update all changelogs: root, plugin-gradle, plugin-maven

@fandreuz
Copy link
Copy Markdown
Contributor Author

fandreuz commented Jul 1, 2024

Hi @nedtwigg, thanks for the feedback! Could you have another look now?

@fandreuz
Copy link
Copy Markdown
Contributor Author

@nedtwigg I'm a little bit confused by the test results, for example:
image

which is caused by my change, and by the fact that spaceBeforeSeparator in JacksonJsonConfig is false by default. It seems like the expected test result aren't accurate, am I missing something @nedtwigg ?

@nedtwigg nedtwigg merged commit 9a1f849 into diffplug:main Sep 23, 2025
@nedtwigg
Copy link
Copy Markdown
Member

Published in plugin-gradle 8.0.0 and plugin-maven 3.0.0.

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.

2 participants