-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ObjectMapperConfig.java
267 lines (235 loc) · 12.8 KB
/
ObjectMapperConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright 2019 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.restassured.config;
import io.restassured.common.mapper.resolver.ObjectMapperResolver;
import io.restassured.mapper.ObjectMapper;
import io.restassured.mapper.ObjectMapperType;
import io.restassured.path.json.mapper.factory.*;
import io.restassured.path.xml.mapper.factory.DefaultJAXBObjectMapperFactory;
import io.restassured.path.xml.mapper.factory.DefaultJakartaEEObjectMapperFactory;
import io.restassured.path.xml.mapper.factory.JAXBObjectMapperFactory;
import io.restassured.path.xml.mapper.factory.JakartaEEObjectMapperFactory;
import org.apache.commons.lang3.Validate;
/**
* Allows you to specify configuration for the object mapping functionality.
*/
public class ObjectMapperConfig implements Config {
private final ObjectMapper defaultObjectMapper;
private final ObjectMapperType defaultObjectMapperType;
private final GsonObjectMapperFactory gsonObjectMapperFactory;
private final Jackson1ObjectMapperFactory jackson1ObjectMapperFactory;
private final Jackson2ObjectMapperFactory jackson2ObjectMapperFactory;
private final JAXBObjectMapperFactory jaxbObjectMapperFactory;
private final JohnzonObjectMapperFactory johnzonObjectMapperFactory;
private final JsonbObjectMapperFactory jsonbObjectMapperFactory;
private final JakartaEEObjectMapperFactory jakartaEEObjectMapperFactory;
private final boolean isUserConfigured;
/**
* Default object mapper configuration that uses no explicit object mapper. An object mapper
* will be found automatically in classpath if available. For more details see <a href="http://code.google.com/p/rest-assured/wiki/Usage#Object_Mapping">documentation</a>.
* <p/>
* Also default object mapper factories will be used.
*/
public ObjectMapperConfig() {
defaultObjectMapper = null;
defaultObjectMapperType = null;
gsonObjectMapperFactory = new DefaultGsonObjectMapperFactory();
jackson1ObjectMapperFactory = new DefaultJackson1ObjectMapperFactory();
jackson2ObjectMapperFactory = new DefaultJackson2ObjectMapperFactory();
jaxbObjectMapperFactory = newJaxbObjectMapperFactoryOrNullIfNotInClasspath();
johnzonObjectMapperFactory = new DefaultJohnzonObjectMapperFactory();
jsonbObjectMapperFactory = new DefaultYassonObjectMapperFactory();
jakartaEEObjectMapperFactory = newJakartaEEObjectMapperFactoryOrNullIfNotInClasspath();
isUserConfigured = false;
}
/**
* Creates an object mapper configuration that uses the specified object mapper as default.
*
* @param defaultObjectMapperType The object mapper to use. If <code>null</code> then classpath scanning will be used.
*/
public ObjectMapperConfig(ObjectMapperType defaultObjectMapperType) {
this(null, defaultObjectMapperType, new DefaultGsonObjectMapperFactory(), new DefaultJackson1ObjectMapperFactory(),
new DefaultJackson2ObjectMapperFactory(), newJaxbObjectMapperFactoryOrNullIfNotInClasspath(),
new DefaultJohnzonObjectMapperFactory(), new DefaultYassonObjectMapperFactory(), newJakartaEEObjectMapperFactoryOrNullIfNotInClasspath(),
true);
}
/**
* Creates an object mapper configuration that uses the specified object mapper as default.
*
* @param defaultObjectMapper The object mapper to use. If <code>null</code> then classpath scanning will be used.
*/
public ObjectMapperConfig(ObjectMapper defaultObjectMapper) {
this(defaultObjectMapper, null, new DefaultGsonObjectMapperFactory(), new DefaultJackson1ObjectMapperFactory(),
new DefaultJackson2ObjectMapperFactory(), newJaxbObjectMapperFactoryOrNullIfNotInClasspath(),
new DefaultJohnzonObjectMapperFactory(), new DefaultYassonObjectMapperFactory(), newJakartaEEObjectMapperFactoryOrNullIfNotInClasspath(),
true);
}
private ObjectMapperConfig(ObjectMapper defaultObjectMapper, ObjectMapperType defaultObjectMapperType,
GsonObjectMapperFactory gsonObjectMapperFactory, Jackson1ObjectMapperFactory jackson1ObjectMapperFactory,
Jackson2ObjectMapperFactory jackson2ObjectMapperFactory, JAXBObjectMapperFactory jaxbObjectMapperFactory,
JohnzonObjectMapperFactory johnzonObjectMapperFactory, JsonbObjectMapperFactory jsonbObjectMapperFactory,
JakartaEEObjectMapperFactory jakartaEEObjectMapperFactory, boolean isUserConfigured) {
Validate.notNull(gsonObjectMapperFactory, GsonObjectMapperFactory.class.getSimpleName() + " cannot be null");
Validate.notNull(jackson1ObjectMapperFactory, Jackson1ObjectMapperFactory.class.getSimpleName() + " cannot be null");
Validate.notNull(jackson2ObjectMapperFactory, Jackson2ObjectMapperFactory.class.getSimpleName() + " cannot be null");
// Note that we explicitly don't check if JakartaEEObjectMapperFactory or JAXBObjectMapperFactory is null here, since
// they are allowed to be null (they're optional).
this.defaultObjectMapperType = defaultObjectMapperType;
this.defaultObjectMapper = defaultObjectMapper;
this.gsonObjectMapperFactory = gsonObjectMapperFactory;
this.jackson1ObjectMapperFactory = jackson1ObjectMapperFactory;
this.jackson2ObjectMapperFactory = jackson2ObjectMapperFactory;
this.jaxbObjectMapperFactory = jaxbObjectMapperFactory;
this.johnzonObjectMapperFactory = johnzonObjectMapperFactory;
this.jsonbObjectMapperFactory = jsonbObjectMapperFactory;
this.jakartaEEObjectMapperFactory = jakartaEEObjectMapperFactory;
this.isUserConfigured = isUserConfigured;
}
public ObjectMapperType defaultObjectMapperType() {
return defaultObjectMapperType;
}
public boolean hasDefaultObjectMapperType() {
return defaultObjectMapperType != null;
}
/**
* Creates an object mapper configuration that uses the specified object mapper as default.
*
* @param defaultObjectMapperType The object mapper to use. If <code>null</code> then classpath scanning will be used.
*/
public ObjectMapperConfig defaultObjectMapperType(ObjectMapperType defaultObjectMapperType) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
public ObjectMapper defaultObjectMapper() {
return defaultObjectMapper;
}
public boolean hasDefaultObjectMapper() {
return defaultObjectMapper != null;
}
/**
* Creates an object mapper configuration that uses the specified object mapper as default.
*
* @param defaultObjectMapper The object mapper to use. If <code>null</code> then classpath scanning will be used.
*/
public ObjectMapperConfig defaultObjectMapper(ObjectMapper defaultObjectMapper) {
return new ObjectMapperConfig(defaultObjectMapper);
}
public GsonObjectMapperFactory gsonObjectMapperFactory() {
return gsonObjectMapperFactory;
}
/**
* Specify a custom Gson object mapper factory.
*
* @param gsonObjectMapperFactory The object mapper factory
*/
public ObjectMapperConfig gsonObjectMapperFactory(GsonObjectMapperFactory gsonObjectMapperFactory) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
public Jackson1ObjectMapperFactory jackson1ObjectMapperFactory() {
return jackson1ObjectMapperFactory;
}
/**
* Specify a custom Jackson 1.0 object mapper factory.
*
* @param jackson1ObjectMapperFactory The object mapper factory
*/
public ObjectMapperConfig jackson1ObjectMapperFactory(Jackson1ObjectMapperFactory jackson1ObjectMapperFactory) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
public Jackson2ObjectMapperFactory jackson2ObjectMapperFactory() {
return jackson2ObjectMapperFactory;
}
/**
* Specify a custom Jackson 1.0 object mapper factory.
*
* @param jackson2ObjectMapperFactory The object mapper factory
*/
public ObjectMapperConfig jackson2ObjectMapperFactory(Jackson2ObjectMapperFactory jackson2ObjectMapperFactory) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
public JAXBObjectMapperFactory jaxbObjectMapperFactory() {
return jaxbObjectMapperFactory;
}
public JakartaEEObjectMapperFactory jakartaEEObjectMapperFactory() {
return jakartaEEObjectMapperFactory;
}
public JohnzonObjectMapperFactory johnzonObjectMapperFactory() {
return johnzonObjectMapperFactory;
}
public JsonbObjectMapperFactory jsonbObjectMapperFactory() {
return jsonbObjectMapperFactory;
}
/**
* Specify a custom JSON-B object mapper factory.
*
* @param jsonbObjectMapperFactory The object mapper factory
*/
public ObjectMapperConfig jsonbObjectMapperFactory(JsonbObjectMapperFactory jsonbObjectMapperFactory) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
/**
* Specify a custom JAXB object mapper factory.
*
* @param jaxbObjectMapperFactory The object mapper factory
*/
public ObjectMapperConfig jaxbObjectMapperFactory(JAXBObjectMapperFactory jaxbObjectMapperFactory) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
/**
* Specify a custom JakartaEE object mapper factory.
*
* @param jakartaEEObjectMapperFactory The object mapper factory
*/
public ObjectMapperConfig jakartaEEObjectMapperFactory(JakartaEEObjectMapperFactory jakartaEEObjectMapperFactory) {
return new ObjectMapperConfig(defaultObjectMapper, defaultObjectMapperType, gsonObjectMapperFactory,
jackson1ObjectMapperFactory, jackson2ObjectMapperFactory, jaxbObjectMapperFactory,
johnzonObjectMapperFactory, jsonbObjectMapperFactory, jakartaEEObjectMapperFactory, true);
}
/**
* @return A static way to create a new ObjectMapperConfig instance without calling "new" explicitly. Mainly for syntactic sugar.
*/
public static ObjectMapperConfig objectMapperConfig() {
return new ObjectMapperConfig();
}
/**
* Syntactic sugar.
*
* @return The same object mapper config instance.
*/
public ObjectMapperConfig and() {
return this;
}
public boolean isUserConfigured() {
return isUserConfigured;
}
private static JAXBObjectMapperFactory newJaxbObjectMapperFactoryOrNullIfNotInClasspath() {
return ObjectMapperResolver.isJAXBInClassPath() ? new DefaultJAXBObjectMapperFactory() : null;
}
private static JakartaEEObjectMapperFactory newJakartaEEObjectMapperFactoryOrNullIfNotInClasspath() {
return ObjectMapperResolver.isJakartaEEInClassPath() ? new DefaultJakartaEEObjectMapperFactory() : null;
}
}