Browse Source

Refine Jackson2ObjectMapperBuilder#configureFeature exception handling

This commit changes the FatalBeanException previously thrown for
an IllegalArgumentException which seems more suitable for that
use case.

Closes gh-29859
pull/29868/head
Sébastien Deleuze 3 years ago
parent
commit
fd84b0997a
  1. 5
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  2. 6
      spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java
  3. 8
      spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

5
spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -57,7 +57,6 @@ import com.fasterxml.jackson.dataformat.xml.XmlFactory; @@ -57,7 +57,6 @@ import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.KotlinDetector;
import org.springframework.http.ProblemDetail;
@ -835,7 +834,7 @@ public class Jackson2ObjectMapperBuilder { @@ -835,7 +834,7 @@ public class Jackson2ObjectMapperBuilder {
objectMapper.configure(mapperFeature, enabled);
}
else {
throw new FatalBeanException("Unknown feature class: " + feature.getClass().getName());
throw new IllegalArgumentException("Unknown feature class: " + feature.getClass().getName());
}
}

6
spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -81,12 +81,10 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -81,12 +81,10 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import kotlin.ranges.IntRange;
import org.junit.jupiter.api.Test;
import org.springframework.beans.FatalBeanException;
import org.springframework.http.ProblemDetail;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
@ -105,7 +103,7 @@ class Jackson2ObjectMapperBuilderTests { @@ -105,7 +103,7 @@ class Jackson2ObjectMapperBuilderTests {
@Test
void unknownFeature() {
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() ->
assertThatIllegalArgumentException().isThrownBy(() ->
Jackson2ObjectMapperBuilder.json().featuresToEnable(Boolean.TRUE).build());
}

8
spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -59,11 +59,10 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory; @@ -59,11 +59,10 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.FatalBeanException;
import org.springframework.http.ProblemDetail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Test cases for {@link Jackson2ObjectMapperFactoryBean}.
@ -86,8 +85,7 @@ public class Jackson2ObjectMapperFactoryBeanTests { @@ -86,8 +85,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
@Test
public void unknownFeature() {
this.factory.setFeaturesToEnable(Boolean.TRUE);
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(
this.factory::afterPropertiesSet);
assertThatIllegalArgumentException().isThrownBy(this.factory::afterPropertiesSet);
}
@Test

Loading…
Cancel
Save