Skip to content

@RegisterReflectionForBinding should register hints for Jackson annotations #29426

@wilkinsona

Description

@wilkinsona

Affects: 6.0.0-RC3

While looking at spring-projects/spring-boot#32929, I have learned that @RegisterReflectionForBinding generates incomplete reflection hints when using Jackson's @JsonProperty. This app illustrates the problem:

package com.example.demo;

import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.example.demo.JsonPropertyApplication.Dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootApplication
@RegisterReflectionForBinding(Dto.class)
public class JsonPropertyApplication {

	public static void main(String[] args) {
		SpringApplication.run(JsonPropertyApplication.class, args);
	}
	
	@Bean
	public CommandLineRunner commandLineRunner(ObjectMapper mapper) {
		return (args) -> {
			System.out.println(mapper.writeValueAsString(new Dto()));
		};
	}
	
	static class Dto {
		
		@JsonProperty
		private String field = "test";
		
		@JsonProperty
		String packagePrivate() {
			return "does it work?";
		}
		
	}
	
}

When run on the JVM, the following output if produced:

{"field":"test","packagePrivate":"does it work?"}

When run as a native image, the following output is produced:

{"field":"test"}

Looking at BindingReflectionHintsRegistrar, I think there are a couple of problems here:

  • Package-private (and private) methods are ignored
  • Methods with names that don't look like getters or setters are ignored

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: aotAn issue related to Ahead-of-time processing

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions