|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonInclude;
|
4 | 4 | import com.fasterxml.jackson.core.JsonFactory;
|
5 |
| -import com.fasterxml.jackson.databind.BeanDescription; |
6 |
| -import com.fasterxml.jackson.databind.DeserializationFeature; |
7 |
| -import com.fasterxml.jackson.databind.JsonSerializer; |
8 | 5 | import com.fasterxml.jackson.databind.Module;
|
9 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
10 |
| -import com.fasterxml.jackson.databind.SerializationConfig; |
11 |
| -import com.fasterxml.jackson.databind.SerializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.*; |
12 | 7 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
13 | 8 | import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
14 | 9 | import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
15 | 10 | import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
|
16 | 11 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
17 | 12 | import io.swagger.v3.core.jackson.ExampleSerializer;
|
18 |
| -import io.swagger.v3.core.jackson.Schema31Serializer; |
19 | 13 | import io.swagger.v3.core.jackson.MediaTypeSerializer;
|
| 14 | +import io.swagger.v3.core.jackson.Schema31Serializer; |
20 | 15 | import io.swagger.v3.core.jackson.SchemaSerializer;
|
21 |
| -import io.swagger.v3.core.jackson.mixin.Components31Mixin; |
22 |
| -import io.swagger.v3.core.jackson.mixin.ComponentsMixin; |
23 |
| -import io.swagger.v3.core.jackson.mixin.DateSchemaMixin; |
24 |
| -import io.swagger.v3.core.jackson.mixin.Discriminator31Mixin; |
25 |
| -import io.swagger.v3.core.jackson.mixin.DiscriminatorMixin; |
26 |
| -import io.swagger.v3.core.jackson.mixin.ExampleMixin; |
27 |
| -import io.swagger.v3.core.jackson.mixin.ExtensionsMixin; |
28 |
| -import io.swagger.v3.core.jackson.mixin.InfoMixin; |
29 |
| -import io.swagger.v3.core.jackson.mixin.LicenseMixin; |
30 |
| -import io.swagger.v3.core.jackson.mixin.MediaTypeMixin; |
31 |
| -import io.swagger.v3.core.jackson.mixin.OpenAPI31Mixin; |
32 |
| -import io.swagger.v3.core.jackson.mixin.OpenAPIMixin; |
33 |
| -import io.swagger.v3.core.jackson.mixin.OperationMixin; |
34 |
| -import io.swagger.v3.core.jackson.mixin.Schema31Mixin; |
35 |
| -import io.swagger.v3.core.jackson.mixin.SchemaConverterMixin; |
36 |
| -import io.swagger.v3.core.jackson.mixin.SchemaMixin; |
37 |
| -import io.swagger.v3.oas.models.Components; |
38 |
| -import io.swagger.v3.oas.models.ExternalDocumentation; |
39 |
| -import io.swagger.v3.oas.models.OpenAPI; |
40 |
| -import io.swagger.v3.oas.models.Operation; |
41 |
| -import io.swagger.v3.oas.models.PathItem; |
42 |
| -import io.swagger.v3.oas.models.Paths; |
| 16 | +import io.swagger.v3.core.jackson.mixin.*; |
| 17 | +import io.swagger.v3.oas.models.*; |
43 | 18 | import io.swagger.v3.oas.models.callbacks.Callback;
|
44 | 19 | import io.swagger.v3.oas.models.examples.Example;
|
45 | 20 | import io.swagger.v3.oas.models.headers.Header;
|
|
48 | 23 | import io.swagger.v3.oas.models.info.License;
|
49 | 24 | import io.swagger.v3.oas.models.links.Link;
|
50 | 25 | import io.swagger.v3.oas.models.links.LinkParameter;
|
51 |
| -import io.swagger.v3.oas.models.media.DateSchema; |
52 |
| -import io.swagger.v3.oas.models.media.Discriminator; |
53 |
| -import io.swagger.v3.oas.models.media.Encoding; |
54 |
| -import io.swagger.v3.oas.models.media.EncodingProperty; |
55 |
| -import io.swagger.v3.oas.models.media.MediaType; |
56 |
| -import io.swagger.v3.oas.models.media.Schema; |
57 |
| -import io.swagger.v3.oas.models.media.XML; |
| 26 | +import io.swagger.v3.oas.models.media.*; |
58 | 27 | import io.swagger.v3.oas.models.parameters.Parameter;
|
59 | 28 | import io.swagger.v3.oas.models.parameters.RequestBody;
|
60 | 29 | import io.swagger.v3.oas.models.responses.ApiResponse;
|
@@ -90,11 +59,11 @@ public static ObjectMapper createYaml() {
|
90 | 59 | }
|
91 | 60 |
|
92 | 61 | public static ObjectMapper createYaml(boolean openapi31) {
|
93 |
| - YAMLFactory factory = new YAMLFactory(); |
94 |
| - factory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER); |
95 |
| - factory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES); |
96 |
| - factory.enable(YAMLGenerator.Feature.SPLIT_LINES); |
97 |
| - factory.enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS); |
| 62 | + YAMLFactory factory = new YAMLFactory() |
| 63 | + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER) |
| 64 | + .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) |
| 65 | + .enable(YAMLGenerator.Feature.SPLIT_LINES) |
| 66 | + .enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS); |
98 | 67 |
|
99 | 68 | return create(factory, openapi31);
|
100 | 69 | }
|
@@ -163,14 +132,9 @@ public JsonSerializer<?> modifySerializer(
|
163 | 132 | });
|
164 | 133 | }
|
165 | 134 |
|
166 |
| - if (!openapi31) { |
167 |
| - Module deserializerModule = new DeserializationModule(); |
168 |
| - mapper.registerModule(deserializerModule); |
169 |
| - } else { |
170 |
| - Module deserializerModule = new DeserializationModule31(); |
171 |
| - mapper.registerModule(deserializerModule); |
172 |
| - } |
173 |
| - mapper.registerModule(new JavaTimeModule()); |
| 135 | + mapper |
| 136 | + .registerModule(openapi31 ? new DeserializationModule31() : new DeserializationModule()) |
| 137 | + .registerModule(new JavaTimeModule()); |
174 | 138 |
|
175 | 139 | Map<Class<?>, Class<?>> sourceMixins = new LinkedHashMap<>();
|
176 | 140 |
|
@@ -218,29 +182,24 @@ public JsonSerializer<?> modifySerializer(
|
218 | 182 | sourceMixins.put(DateSchema.class, DateSchemaMixin.class);
|
219 | 183 | sourceMixins.put(Discriminator.class, Discriminator31Mixin.class);
|
220 | 184 | }
|
221 |
| - mapper.setMixIns(sourceMixins); |
222 |
| - mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
223 |
| - mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
224 |
| - mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); |
225 |
| - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
226 |
| - mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); |
227 |
| - mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); |
228 |
| - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
229 | 185 |
|
230 |
| - return mapper; |
| 186 | + return mapper.setMixIns(sourceMixins) |
| 187 | + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) |
| 188 | + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) |
| 189 | + .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true) |
| 190 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 191 | + .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false) |
| 192 | + .configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true) |
| 193 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL); |
231 | 194 | }
|
232 | 195 |
|
233 | 196 | public static ObjectMapper createJsonConverter() {
|
234 | 197 |
|
235 |
| - ObjectMapper mapper = new ObjectMapper(); |
236 |
| - |
237 |
| - |
238 |
| - Module deserializerModule = new DeserializationModule(); |
239 |
| - mapper.registerModule(deserializerModule); |
240 |
| - mapper.registerModule(new JavaTimeModule()); |
| 198 | + ObjectMapper mapper = new ObjectMapper() |
| 199 | + .registerModule(new DeserializationModule()) |
| 200 | + .registerModule(new JavaTimeModule()); |
241 | 201 |
|
242 | 202 | Map<Class<?>, Class<?>> sourceMixins = new LinkedHashMap<>();
|
243 |
| - |
244 | 203 | sourceMixins.put(ApiResponses.class, ExtensionsMixin.class);
|
245 | 204 | sourceMixins.put(ApiResponse.class, ExtensionsMixin.class);
|
246 | 205 | sourceMixins.put(Callback.class, ExtensionsMixin.class);
|
@@ -271,35 +230,33 @@ public static ObjectMapper createJsonConverter() {
|
271 | 230 | sourceMixins.put(ServerVariables.class, ExtensionsMixin.class);
|
272 | 231 | sourceMixins.put(Tag.class, ExtensionsMixin.class);
|
273 | 232 | sourceMixins.put(XML.class, ExtensionsMixin.class);
|
274 |
| - |
275 | 233 | sourceMixins.put(Schema.class, SchemaConverterMixin.class);
|
276 |
| - mapper.setMixIns(sourceMixins); |
277 |
| - mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
278 |
| - mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
279 |
| - mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); |
280 |
| - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
281 |
| - mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); |
282 |
| - mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); |
283 |
| - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
284 |
| - |
285 |
| - return mapper; |
| 234 | + |
| 235 | + return mapper.setMixIns(sourceMixins) |
| 236 | + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) |
| 237 | + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) |
| 238 | + .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true) |
| 239 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 240 | + .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false) |
| 241 | + .configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true) |
| 242 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL); |
286 | 243 | }
|
287 | 244 |
|
288 | 245 |
|
289 | 246 | public static ObjectMapper buildStrictGenericObjectMapper() {
|
290 |
| - ObjectMapper mapper = new ObjectMapper(); |
291 |
| - mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
292 |
| - mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); |
293 |
| - mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); |
294 |
| - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
295 |
| - mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); |
| 247 | + ObjectMapper mapper = new ObjectMapper() |
| 248 | + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) |
| 249 | + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) |
| 250 | + .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true) |
| 251 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 252 | + .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); |
296 | 253 | try {
|
297 | 254 | mapper.configure(DeserializationFeature.valueOf("FAIL_ON_TRAILING_TOKENS"), true);
|
298 | 255 | } catch (Throwable e) {
|
299 | 256 | // add only if supported by Jackson version 2.9+
|
300 | 257 | }
|
301 |
| - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
302 |
| - return mapper; |
| 258 | + |
| 259 | + return mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
303 | 260 | }
|
304 | 261 |
|
305 | 262 | }
|
0 commit comments