Skip to content

JsonAnyGetter method serialization can override JsonProperty serialization on serialized name conflictΒ #5342

@alzimmermsft

Description

@alzimmermsft

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

It would appear this change #4775 broke a rather niche scenario where if you had a @JsonProperty("x") and @JsonAnyGetter public Map<String, Object> x it would override the BeanWriterProperty.

The scenario we had tests for shouldn't be used anymore, so this is a low priority for us, just wanted to report as it is a breaking change on upgrade and was really hard to track down.

Version Information

Starting in 2.19.0

Reproduction

Use this class for serialization

public class Repo {
    @JsonIgnore
    private Map<String, Object> additionalProperties;
    @JsonProperty(value = "additionalProperties")
    private Map<String, Object> additionalPropertiesProperty;

    @JsonAnySetter
    private void additionalProperties(String key, Object value) {
        if (additionalProperties == null) {
            additionalProperties = new HashMap<>();
        }
        additionalProperties.put(key.replace("\\.", "."), value);
    }

    @JsonAnyGetter
    public Map<String, Object> additionalProperties() {
        return additionalProperties;
    }

    public Map<String, Object> additionalPropertiesProperty() {
        return additionalPropertiesProperty;
    }

    public void additionalPropertiesProperty(Map<String, Object> additionalPropertiesProperty) {
        this.additionalPropertiesProperty = additionalPropertiesProperty;
    }
}

Create the following object

Repo repo = new Repo();
repo.additionalProperties("foo", "bar");
repo.additionalPropertiesProperty(Collections.singlontonMap("fizz", "buzz");

In Jackson 2.19.0 this serializes as {"foo":"bar"}, before Jackson 2.19.0 this serializes as {"foo":"bar","additionalProperties": {"fizz":"buzz"}}

Expected behavior

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.20Issues planned at 2.20 or later

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions