jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

`com.fasterxml.jackson.databind.introspect.Annotated.getAnnotation()` method does not return annotation for a Record field

Open mumukiller opened this issue 2 years ago • 2 comments

Sorry if it's duplicate but i did not find anything like that

Describe the bug When using custom annotation, Annotated.getAnnotation() method does not return annotation for a field marked with this annotation.

Version information 2.15.0

To Reproduce This simple test can help:

class AnnotationIssueApplicationTests {

    @Test
    void mask() {
        var entity = new TestEntity("Secret", "Doe");

        ObjectMapper mapper = new ObjectMapper();
        mapper.setAnnotationIntrospector(
                AnnotationIntrospectorPair.pair(
                        mapper.getSerializationConfig().getAnnotationIntrospector(),
                        new MaskSensitiveDataAnnotationIntrospector()));
        try {
            assertNotNull("Should be not null", mapper.writeValueAsString(entity));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface SensitiveField {}

    private record TestEntity(
            @SensitiveField String mySecretFieldPassword,
            String name) {}


    private static class MaskSensitiveDataAnnotationIntrospector extends NopAnnotationIntrospector {

        @Override
        public Object findSerializer(Annotated am) {
            SensitiveField annotation = am.getAnnotation(SensitiveField.class);
            //When i use jackson-core:2.14.1 the test below passes successfully
           //When i use jackson-core:2.15.0 the test fails
            if (am.getName().equals("mySecretFieldPassword") && annotation == null){
                throw new RuntimeException("This should not happen");
            };
            return null;
        }
    }
}

Expected behavior When i use jackson-core:2.14.1 the test below passes successfully When i use jackson-core:2.15.0 the test fails

Additional context This happened when i updated Spring Boot from 3.0.2 to 3.1.0 This is my graddle build

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
implementation 'com.fasterxml.jackson.core:jackson-annotations'
implementation 'com.fasterxml.jackson.core:jackson-core'
//this fixes the issue    implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.1'

    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

mumukiller avatar Jun 09 '23 09:06 mumukiller