Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: private constructor no longer usable as creator #74

Closed
scranen opened this issue May 24, 2018 · 1 comment
Closed

Regression: private constructor no longer usable as creator #74

scranen opened this issue May 24, 2018 · 1 comment

Comments

@scranen
Copy link

scranen commented May 24, 2018

The following test passes in 2.9.1, but fails in 2.9.2:

package com.fasterxml.jackson.module.paramnames;

import static org.assertj.core.api.BDDAssertions.then;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.junit.Test;

public class JsonCreatorWithPropertyNamingStrategyTest {

    @Test
    public void testPrivateConstructorWithAnnotations() throws Exception {
        verifyObjectDeserializationWithNamingStrategy(
                PropertyNamingStrategy.SNAKE_CASE,
                "{\"a\":1, \"b\": 2}",
                new ClassWithTwoProperties(1, 2));
    }

    private void verifyObjectDeserializationWithNamingStrategy(
            final PropertyNamingStrategy propertyNamingStrategy, final String json, Object expected)
            throws Exception {
        // given
        ObjectMapper objectMapper =
                new ObjectMapper()
                        .registerModule(new ParameterNamesModule())
                        .setPropertyNamingStrategy(propertyNamingStrategy)
                        .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
                        .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        // when
        Object actual = objectMapper.readValue(json, expected.getClass());

        then(actual).isEqualToComparingFieldByField(expected);
    }

    static class ClassWithTwoProperties {
        public final int a;
        public final int b;

        private ClassWithTwoProperties(@JsonProperty("a") int a, @JsonProperty("b") int b) {
            this.a = a;
            this.b = b;
        }
    }
}

This may or may not be related to the changes that cause this regression.

@cowtowncoder cowtowncoder added 2.9 active Issue being actively investigated and/or worked on and removed active Issue being actively investigated and/or worked on labels Sep 11, 2018
@cowtowncoder
Copy link
Member

I don't know why this used to pass, but I think it should not since you are explicitly disabling auto-detection of creators but are not using @JsonCreator marker.
That is, it seems that earlier visibility checking was done incorrectly.

To resolve this, you would need to change visibility for creators by adding:

                    .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY)

(or whatever baseline visibility you think is acceptable)

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

No branches or pull requests

2 participants