← All recipes
Open source

Find @JsonSetter(nulls = Nulls.AS_EMPTY) on empty collection fields

Recipe IDorg.openrewrite.java.jackson.FindJsonSetterNullsAsEmptyCollectionsArtifactorg.openrewrite.recipe:rewrite-jackson

Find Map and Collection fields that carry @JsonSetter(nulls = Nulls.AS_EMPTY), are initialized with an empty collection, and are no longer hidden by @JsonIgnore on the field or its getter, or by a class level @JsonIgnoreProperties. In rewrite-jackson 1.17.0 through 1.28.0 the Jackson 2 to 3 migration replaced @JsonIgnore with @JsonSetter(nulls = Nulls.AS_EMPTY) on exactly these fields, which starts serializing properties that were deliberately hidden wherever the field is otherwise visible, such as through a getter. Run this recipe to audit repositories migrated with those versions; every match that should stay out of the JSON output needs @JsonIgnore restored, which the addJsonIgnore option does for you.

Single recipejackson-3Apache 2.0

Usage

This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.java.jackson.FindJsonSetterNullsAsEmptyCollections

If the recipe isn’t available locally, install it with:

mod config recipes jar install org.openrewrite.recipe:rewrite-jackson:RELEASE

Options

NameTypeDescription
addJsonIgnoreBooleanAdd @JsonIgnore back to every match instead of only marking it, removing the @JsonSetter(nulls = Nulls.AS_EMPTY) that replaced it where nulls is its only argument. Only enable this once you have confirmed that none of the matches should be serialized. Defaults to false.

Data tables

Structured output this recipe can produce.

  • Source files that had resultsSource files that were modified by the recipe run.org.openrewrite.table.SourcesFileResults
  • Source files that had search resultsSearch results that were found during the recipe run.org.openrewrite.table.SearchResults
  • Source files that errored on a recipeThe details of all errors produced by a recipe run.org.openrewrite.table.SourcesFileErrors
  • Recipe performanceStatistics used in analyzing the performance of recipes.org.openrewrite.table.RecipeRunStats