Skip to content

Commit db91f52

Browse files
authored
fix: Spring mapper props in Tzatziki by default (#60)
* fix: Spring mapper props in Tzatziki by default * fix: Tests are more readable * fix: Ability to disable the copyNamingStrategyFromSpringMapper behaviour * fix: doc for copyNamingStrategyFromSpringMapper behaviour
1 parent 989489b commit db91f52

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

tzatziki-spring/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ HttpInterceptor.disable();
130130
If you wish to intercept requests for another client than the supported ones,
131131
you can have a look at the `com.decathlon.tzatziki.spring.HttpInterceptor` code and write your own interceptor.
132132

133+
## JacksonMapper's property naming strategy override
134+
135+
By default, JacksonMapper will use the Spring context's ObjectMapper naming strategy.
136+
If you want to disable it and override it with another:
137+
138+
Disable the Spring property naming strategy copy behaviour and override with the wanted one in your runner (HelloApplicationSteps):
139+
```java
140+
static {
141+
SpringSteps.copyNamingStrategyFromSpringMapper = false;
142+
JacksonMapper.with(objectMapper -> objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.WANTED_STRATEGY));
143+
}
144+
```
145+
133146
## Steps local to this library
134147

135148
This library doesn't come with a lot of steps, but it will start your Spring automatically

tzatziki-spring/src/main/java/com/decathlon/tzatziki/steps/SpringSteps.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.decathlon.tzatziki.steps;
22

33
import com.decathlon.tzatziki.utils.Guard;
4+
import com.decathlon.tzatziki.utils.JacksonMapper;
45
import com.decathlon.tzatziki.utils.Mapper;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
57
import io.cucumber.java.Before;
68
import io.cucumber.java.en.Given;
79
import io.cucumber.java.en.Then;
@@ -17,7 +19,6 @@
1719
import java.util.Objects;
1820
import java.util.function.Predicate;
1921

20-
import static com.decathlon.tzatziki.utils.Asserts.awaitUntilAsserted;
2122
import static com.decathlon.tzatziki.utils.Asserts.equalsInAnyOrder;
2223
import static com.decathlon.tzatziki.utils.Guard.GUARD;
2324
import static com.decathlon.tzatziki.utils.Guard.always;
@@ -32,9 +33,13 @@ public class SpringSteps {
3233
private ApplicationContext applicationContext;
3334
@Autowired(required = false)
3435
private List<CacheManager> cacheManagers;
36+
@Autowired(required = false)
37+
private ObjectMapper objectMapper;
3538
@LocalServerPort
3639
private int localServerPort;
3740

41+
public static boolean copyNamingStrategyFromSpringMapper = true;
42+
3843
public SpringSteps(ObjectSteps objects, HttpSteps http) {
3944
this.objects = objects;
4045
this.http = http;
@@ -44,11 +49,16 @@ public ApplicationContext applicationContext() {
4449
return applicationContext;
4550
}
4651

47-
@Before
52+
@Before(order = -1)
4853
public void before() {
4954
http.setRelativeUrlRewriter(path -> "http://localhost:%s%s".formatted(localServerPort, path));
5055
if (applicationContext != null) {
5156
we_clear_all_the_caches(always());
57+
58+
if (copyNamingStrategyFromSpringMapper) {
59+
JacksonMapper.with(mapper -> mapper.setPropertyNamingStrategy(objectMapper.getPropertyNamingStrategy()));
60+
copyNamingStrategyFromSpringMapper = false;
61+
}
5262
}
5363
}
5464

tzatziki-spring/src/test/java/com/decathlon/tzatziki/app/TestApplication.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.decathlon.tzatziki.app;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
57
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -21,6 +23,13 @@ public RestTemplate restTemplate() {
2123
return new RestTemplate();
2224
}
2325

26+
@Bean
27+
public ObjectMapper objectMapperSnakeCase() {
28+
final ObjectMapper mapper = new ObjectMapper();
29+
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
30+
return mapper;
31+
}
32+
2433
@Bean
2534
public RestTemplateBuilder restTemplateBuilder() {
2635
return new RestTemplateBuilder();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.decathlon.tzatziki.app.beans;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class NonSnakeCasePojo {
7+
private String nonSnakeCaseField;
8+
}

tzatziki-spring/src/test/java/com/decathlon/tzatziki/steps/TestApplicationSteps.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.decathlon.tzatziki.app.TestApplication;
44
import com.decathlon.tzatziki.spring.HttpInterceptor;
5+
import com.decathlon.tzatziki.utils.JacksonMapper;
6+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
57
import io.cucumber.java.Before;
68
import io.cucumber.java.en.Then;
79
import io.cucumber.spring.CucumberContextConfiguration;
@@ -18,7 +20,6 @@
1820
@ContextConfiguration(initializers = TestApplicationSteps.Initializer.class)
1921
@Slf4j
2022
public class TestApplicationSteps {
21-
2223
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
2324

2425
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

tzatziki-spring/src/test/resources/com/decathlon/tzatziki/steps/spring.feature

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ Feature: to interact with a spring boot service
4848
When we call "http://www.google.com"
4949
Then we receive a status 200
5050
But if calling "http://www.google.com" will return a status FORBIDDEN_403
51-
Then calling "http://www.google.com" returns a status FORBIDDEN_403
51+
Then calling "http://www.google.com" returns a status FORBIDDEN_403
52+
53+
Scenario: we should use Spring Context's mapper PropertyNamingStrategy by default (snake_case)
54+
Then it is not true that a JsonMappingException is thrown when myPojo is a NonSnakeCasePojo:
55+
"""
56+
non_snake_case_field: hello
57+
"""

0 commit comments

Comments
 (0)