|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
26 | 26 |
|
27 | 27 | import com.fasterxml.jackson.annotation.JsonCreator;
|
28 | 28 | import com.fasterxml.jackson.annotation.JsonCreator.Mode;
|
| 29 | +import com.fasterxml.jackson.annotation.JsonFormat; |
29 | 30 | import com.fasterxml.jackson.annotation.JsonInclude;
|
30 | 31 | import com.fasterxml.jackson.core.JsonGenerator;
|
31 | 32 | import com.fasterxml.jackson.core.JsonParser;
|
|
40 | 41 | import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
|
41 | 42 | import com.fasterxml.jackson.databind.SerializationFeature;
|
42 | 43 | import com.fasterxml.jackson.databind.SerializerProvider;
|
| 44 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
43 | 45 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
44 | 46 | import com.fasterxml.jackson.databind.util.StdDateFormat;
|
45 | 47 | import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
|
57 | 59 | import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
58 | 60 |
|
59 | 61 | import static org.assertj.core.api.Assertions.assertThat;
|
| 62 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
60 | 63 | import static org.mockito.Mockito.mock;
|
61 | 64 |
|
62 | 65 | /**
|
@@ -301,6 +304,25 @@ void customTimeZoneFormattingADate() {
|
301 | 304 | });
|
302 | 305 | }
|
303 | 306 |
|
| 307 | + @Test |
| 308 | + void enableDefaultLeniency() { |
| 309 | + this.contextRunner.withPropertyValues("spring.jackson.default-leniency:true").run((context) -> { |
| 310 | + ObjectMapper mapper = context.getBean(ObjectMapper.class); |
| 311 | + Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class); |
| 312 | + assertThat(person.getBirthDate()).isNotNull(); |
| 313 | + }); |
| 314 | + } |
| 315 | + |
| 316 | + @Test |
| 317 | + void disableDefaultLeniency() { |
| 318 | + this.contextRunner.withPropertyValues("spring.jackson.default-leniency:false").run((context) -> { |
| 319 | + ObjectMapper mapper = context.getBean(ObjectMapper.class); |
| 320 | + assertThatThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class)) |
| 321 | + .isInstanceOf(InvalidFormatException.class).hasMessageContaining("expected format") |
| 322 | + .hasMessageContaining("yyyyMMdd"); |
| 323 | + }); |
| 324 | + } |
| 325 | + |
304 | 326 | @Test
|
305 | 327 | void additionalJacksonBuilderCustomization() {
|
306 | 328 | this.contextRunner.withUserConfiguration(ObjectMapperBuilderCustomConfig.class).run((context) -> {
|
@@ -537,4 +559,19 @@ String getProperty3() {
|
537 | 559 |
|
538 | 560 | }
|
539 | 561 |
|
| 562 | + static class Person { |
| 563 | + |
| 564 | + @JsonFormat(pattern = "yyyyMMdd") |
| 565 | + private Date birthDate; |
| 566 | + |
| 567 | + Date getBirthDate() { |
| 568 | + return this.birthDate; |
| 569 | + } |
| 570 | + |
| 571 | + void setBirthDate(Date birthDate) { |
| 572 | + this.birthDate = birthDate; |
| 573 | + } |
| 574 | + |
| 575 | + } |
| 576 | + |
540 | 577 | }
|
0 commit comments