FindStyles: honor custom @JsonSerialize so ImportLayoutStyle.layout isn't dropped#7486
Merged
Jenson3210 merged 1 commit intomainfrom Apr 28, 2026
Merged
FindStyles: honor custom @JsonSerialize so ImportLayoutStyle.layout isn't dropped#7486Jenson3210 merged 1 commit intomainfrom
Jenson3210 merged 1 commit intomainfrom
Conversation
…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.
timtebeek
approved these changes
Apr 28, 2026
Contributor
Author
|
@steve-aom-elliott @MBoegers tagged you as you both have people interested in styles |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
FindStyleswas dumping eachStylevia SnakeYAML withBeanAccess.FIELD, which bypassed any class-level@JsonSerialize. ForImportLayoutStylethat meant theBlockinstances (sealed interface,private finalfields, no setters) could not be introspected and the recipe reported emptylayout: []/packagesToFold: []even when the merged style was fully populated — exactly the field people most often want to verify.Styleis now routed throughObjectMappers.propertyBasedMapper(...).convertValue(style, Map.class)first, so any custom serializer (e.g.ImportLayoutStyle.Serializer, which renders Blocks as readable strings likeimport java.*,<blank line>,import all other imports) is invoked before SnakeYAML sees the value. TheBeanAccess.FIELDsetup and the class-tag-suppressingRepresenterare no longer needed and have been removed.importLayoutStyleSerializesBlocksAsStringsregression test that builds anImportLayoutStyleviaBuilderwith a real layout +packagesToFoldand asserts the output contains the readable Block strings and notlayout: []/packagesToFold: []. The previous tests only exercised trivial POJO styles and would never have caught this.Before
After
The fix is general: any
Stylewith a custom@JsonSerializebenefits, not justImportLayoutStyle.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 confirmSerializer/Deserializerround-trip is untouched./gradlew :rewrite-core:licenseFormat :rewrite-test:licenseFormat