Skip to content

FindStyles: honor custom @JsonSerialize so ImportLayoutStyle.layout isn't dropped#7486

Merged
Jenson3210 merged 1 commit intomainfrom
Jenson3210/findstyles-layout
Apr 28, 2026
Merged

FindStyles: honor custom @JsonSerialize so ImportLayoutStyle.layout isn't dropped#7486
Jenson3210 merged 1 commit intomainfrom
Jenson3210/findstyles-layout

Conversation

@Jenson3210
Copy link
Copy Markdown
Contributor

Summary

  • FindStyles was dumping each Style via SnakeYAML with BeanAccess.FIELD, which bypassed any class-level @JsonSerialize. For ImportLayoutStyle that meant the Block instances (sealed interface, private final fields, no setters) could not be introspected and the recipe reported empty layout: [] / packagesToFold: [] even when the merged style was fully populated — exactly the field people most often want to verify.
  • Each Style is now routed through ObjectMappers.propertyBasedMapper(...).convertValue(style, Map.class) first, so any custom serializer (e.g. ImportLayoutStyle.Serializer, which renders Blocks as readable strings like import java.*, <blank line>, import all other imports) is invoked before SnakeYAML sees the value. The BeanAccess.FIELD setup and the class-tag-suppressing Representer are no longer needed and have been removed.
  • Added an importLayoutStyleSerializesBlocksAsStrings regression test that builds an ImportLayoutStyle via Builder with a real layout + packagesToFold and asserts the output contains the readable Block strings and not layout: [] / packagesToFold: []. The previous tests only exercised trivial POJO styles and would never have caught this.

Before

- org.openrewrite.java.style.ImportLayoutStyle:
    classCountToUseStarImport: 9999
    layout: [
      ]
    nameCountToUseStarImport: 9999
    packagesToFold: [
      ]

After

- org.openrewrite.java.style.ImportLayoutStyle:
    classCountToUseStarImport: 9999
    nameCountToUseStarImport: 9999
    layout:
    - import java.*
    - <blank line>
    - import all other imports
    - <blank line>
    - import static all other imports
    packagesToFold:
    - import java.util.*

The fix is general: any Style with a custom @JsonSerialize benefits, not just ImportLayoutStyle.

Test plan

  • ./gradlew :rewrite-test:test --tests "org.openrewrite.FindStylesTest" (incl. new regression test)
  • ./gradlew :rewrite-core:test :rewrite-test:test
  • ./gradlew :rewrite-java:test --tests "*ImportLayoutStyleTest*" to confirm Serializer/Deserializer round-trip is untouched
  • ./gradlew :rewrite-core:licenseFormat :rewrite-test:licenseFormat

…Serialize is honored

FindStyles dumped each Style with SnakeYAML using BeanAccess.FIELD,
which bypassed any class-level @JsonSerialize. For ImportLayoutStyle
this meant the Block instances (sealed interface, private final
fields, no setters) could not be introspected, so the recipe reported
empty `layout: []` and `packagesToFold: []` even when the merged
style had them populated.

Convert each Style via ObjectMapper.convertValue(...) into a plain
Map first, so any custom serializer (e.g. ImportLayoutStyle.Serializer
which renders Blocks as readable strings like `import java.*`,
`<blank line>`) is invoked before SnakeYAML sees the value. The
BeanAccess.FIELD setup and class-tag suppressing Representer are no
longer needed.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Apr 28, 2026
@Jenson3210 Jenson3210 marked this pull request as ready for review April 28, 2026 09:58
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Apr 28, 2026
@Jenson3210 Jenson3210 merged commit 3361a68 into main Apr 28, 2026
1 check passed
@Jenson3210 Jenson3210 deleted the Jenson3210/findstyles-layout branch April 28, 2026 11:37
@Jenson3210
Copy link
Copy Markdown
Contributor Author

@steve-aom-elliott @MBoegers tagged you as you both have people interested in styles

@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants