2929
3030import com .fasterxml .jackson .annotation .JsonFilter ;
3131import com .fasterxml .jackson .annotation .JsonInclude ;
32- import com .fasterxml .jackson .core .JsonGenerator ;
33- import com .fasterxml .jackson .databind .DeserializationFeature ;
34- import com .fasterxml .jackson .databind .ObjectMapper ;
35- import com .fasterxml .jackson .databind .SerializerProvider ;
36- import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
37- import com .fasterxml .jackson .databind .ser .FilterProvider ;
38- import com .fasterxml .jackson .databind .ser .PropertyWriter ;
39- import com .fasterxml .jackson .databind .ser .impl .SimpleBeanPropertyFilter ;
40- import com .fasterxml .jackson .databind .ser .impl .SimpleFilterProvider ;
32+ import tools .jackson .core .JsonGenerator ;
33+ import tools .jackson .databind .DeserializationFeature ;
34+ import tools .jackson .databind .SerializationContext ;
35+ import tools .jackson .databind .exc .InvalidDefinitionException ;
36+ import tools .jackson .databind .json .JsonMapper ;
37+ import tools .jackson .databind .ser .PropertyWriter ;
38+ import tools .jackson .databind .ser .std .SimpleBeanPropertyFilter ;
39+ import tools .jackson .databind .ser .std .SimpleFilterProvider ;
4140
4241import org .springframework .beans .BeanUtils ;
4342import org .springframework .beans .factory .config .YamlPropertiesFactoryBean ;
5453 */
5554public final class MetadataUtil {
5655
57- private static final ObjectMapper MAPPER = new VerifierObjectMapper ();
56+ private static final JsonMapper MAPPER = buildJsonMapper ();
5857
5958 private MetadataUtil () {
6059 throw new IllegalStateException ("Can't instantiate a utility class" );
@@ -96,7 +95,7 @@ public static <T> T merge(T objectToMerge, Object patch) {
9695 catch (Exception e ) {
9796 if (e .getClass ().toString ().contains ("InaccessibleObjectException" )
9897 || (e instanceof InvalidDefinitionException
99- && e .getMessage ().contains ("InaccessibleObjectException" ))) {
98+ && e .getMessage ().contains ("InaccessibleObjectException" ))) {
10099 // JDK 16 workaround - ObjectMapper seems not be JDK16 compatible
101100 // with the setup present in Spring Cloud Contract. So we will not
102101 // allow patching but we will just copy values from the patch to
@@ -107,11 +106,12 @@ public static <T> T merge(T objectToMerge, Object patch) {
107106 Properties properties = yamlProcessor .getObject ();
108107 T props = (T ) new Binder (
109108 new MapConfigurationPropertySource (properties .entrySet ()
110- .stream ()
111- .collect (Collectors .toMap (entry -> entry .getKey ().toString (),
112- entry -> entry .getValue ().toString ()))))
113- .bind ("" , objectToMerge .getClass ())
114- .get ();
109+ .stream ()
110+ .collect (Collectors .toMap (entry -> entry .getKey ()
111+ .toString (),
112+ entry -> entry .getValue ().toString ()))))
113+ .bind ("" , objectToMerge .getClass ())
114+ .get ();
115115 BeanUtils .copyProperties (props , objectToMerge );
116116 return objectToMerge ;
117117 }
@@ -208,19 +208,20 @@ public int hashCode() {
208208
209209 }
210210
211- }
212-
213- class VerifierObjectMapper extends ObjectMapper {
214-
215- VerifierObjectMapper () {
216- setDefaultPropertyInclusion (JsonInclude .Include .NON_NULL )
217- .setDefaultPropertyInclusion (JsonInclude .Include .NON_DEFAULT )
218- .setDefaultPropertyInclusion (JsonInclude .Include .NON_EMPTY )
219- .setDefaultPropertyInclusion (JsonInclude .Include .NON_ABSENT );
220- configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
221- FilterProvider filters = new SimpleFilterProvider ().addFilter ("non default properties" , new MyFilter ());
222- addMixIn (Object .class , PropertyFilterMixIn .class );
223- setFilterProvider (filters );
211+ private static JsonMapper buildJsonMapper () {
212+ return JsonMapper .builder ()
213+ .withConfigOverride (Object .class , o -> o .setIncludeAsProperty (JsonInclude .Value
214+ .construct (JsonInclude .Include .NON_NULL , JsonInclude .Include .NON_NULL )))
215+ .withConfigOverride (Object .class , o -> o .setIncludeAsProperty (JsonInclude .Value
216+ .construct (JsonInclude .Include .NON_DEFAULT , JsonInclude .Include .NON_DEFAULT )))
217+ .withConfigOverride (Object .class , o -> o .setIncludeAsProperty (JsonInclude .Value
218+ .construct (JsonInclude .Include .NON_EMPTY , JsonInclude .Include .NON_EMPTY )))
219+ .withConfigOverride (Object .class , o -> o .setIncludeAsProperty (JsonInclude .Value
220+ .construct (JsonInclude .Include .NON_ABSENT , JsonInclude .Include .NON_ABSENT )))
221+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
222+ .addMixIn (Object .class , PropertyFilterMixIn .class )
223+ .filterProvider (new SimpleFilterProvider ().addFilter ("non default properties" , new MyFilter ()))
224+ .build ();
224225 }
225226
226227}
@@ -234,17 +235,17 @@ class MyFilter extends SimpleBeanPropertyFilter implements Serializable {
234235
235236 private static final Map <Class , Object > CACHE = new ConcurrentHashMap <>();
236237
238+
237239 @ Override
238- public void serializeAsField (Object pojo , JsonGenerator jgen , SerializerProvider provider , PropertyWriter writer )
239- throws Exception {
240+ public void serializeAsProperty (Object pojo , JsonGenerator jgen , SerializationContext provider , PropertyWriter writer ) throws Exception {
240241 if (pojo instanceof Map || pojo instanceof Collection ) {
241- writer .serializeAsField (pojo , jgen , provider );
242+ writer .serializeAsProperty (pojo , jgen , provider );
242243 return ;
243244 }
244245 Object defaultInstance = defaultInstance (pojo );
245246 if (defaultInstance instanceof CantInstantiateThisClass
246247 || !valueSameAsDefault (pojo , defaultInstance , writer .getName ())) {
247- writer .serializeAsField (pojo , jgen , provider );
248+ writer .serializeAsProperty (pojo , jgen , provider );
248249 }
249250 }
250251
@@ -274,7 +275,6 @@ boolean valueSameAsDefault(Object pojo, Object defaultInstance, String fieldName
274275 throw new IllegalStateException (e );
275276 }
276277 }
277-
278278}
279279
280280class CantInstantiateThisClass {
0 commit comments