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
This commit is contained in:
Sébastien Deleuze 2023-01-20 10:07:05 +01:00
parent 0e8838db90
commit fd84b0997a
3 changed files with 7 additions and 12 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.KotlinDetector; import org.springframework.core.KotlinDetector;
import org.springframework.http.ProblemDetail; import org.springframework.http.ProblemDetail;
@ -835,7 +834,7 @@ public class Jackson2ObjectMapperBuilder {
objectMapper.configure(mapperFeature, enabled); objectMapper.configure(mapperFeature, enabled);
} }
else { else {
throw new FatalBeanException("Unknown feature class: " + feature.getClass().getName()); throw new IllegalArgumentException("Unknown feature class: " + feature.getClass().getName());
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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;
import kotlin.ranges.IntRange; import kotlin.ranges.IntRange;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.FatalBeanException;
import org.springframework.http.ProblemDetail; import org.springframework.http.ProblemDetail;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/** /**
@ -105,7 +103,7 @@ class Jackson2ObjectMapperBuilderTests {
@Test @Test
void unknownFeature() { void unknownFeature() {
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
Jackson2ObjectMapperBuilder.json().featuresToEnable(Boolean.TRUE).build()); Jackson2ObjectMapperBuilder.json().featuresToEnable(Boolean.TRUE).build());
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.FatalBeanException;
import org.springframework.http.ProblemDetail; import org.springframework.http.ProblemDetail;
import static org.assertj.core.api.Assertions.assertThat; 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}. * Test cases for {@link Jackson2ObjectMapperFactoryBean}.
@ -86,8 +85,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
@Test @Test
public void unknownFeature() { public void unknownFeature() {
this.factory.setFeaturesToEnable(Boolean.TRUE); this.factory.setFeaturesToEnable(Boolean.TRUE);
assertThatExceptionOfType(FatalBeanException.class).isThrownBy( assertThatIllegalArgumentException().isThrownBy(this.factory::afterPropertiesSet);
this.factory::afterPropertiesSet);
} }
@Test @Test