|
3 | 3 |
|
4 | 4 | package com.algolia.model.composition; |
5 | 5 |
|
| 6 | +import com.algolia.exceptions.AlgoliaRuntimeException; |
6 | 7 | import com.fasterxml.jackson.annotation.*; |
| 8 | +import com.fasterxml.jackson.core.*; |
| 9 | +import com.fasterxml.jackson.databind.*; |
7 | 10 | import com.fasterxml.jackson.databind.annotation.*; |
8 | | -import java.util.Objects; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.logging.Logger; |
9 | 13 |
|
10 | 14 | /** An object containing either an `injection` or `multifeed` behavior schema, but not both. */ |
11 | | -public class CompositionBehavior { |
12 | | - |
13 | | - @JsonProperty("injection") |
14 | | - private Injection injection; |
15 | | - |
16 | | - @JsonProperty("multifeed") |
17 | | - private Multifeed multifeed; |
18 | | - |
19 | | - public CompositionBehavior setInjection(Injection injection) { |
20 | | - this.injection = injection; |
21 | | - return this; |
22 | | - } |
23 | | - |
24 | | - /** Get injection */ |
25 | | - @javax.annotation.Nullable |
26 | | - public Injection getInjection() { |
27 | | - return injection; |
28 | | - } |
29 | | - |
30 | | - public CompositionBehavior setMultifeed(Multifeed multifeed) { |
31 | | - this.multifeed = multifeed; |
32 | | - return this; |
33 | | - } |
34 | | - |
35 | | - /** Get multifeed */ |
36 | | - @javax.annotation.Nullable |
37 | | - public Multifeed getMultifeed() { |
38 | | - return multifeed; |
39 | | - } |
40 | | - |
41 | | - @Override |
42 | | - public boolean equals(Object o) { |
43 | | - if (this == o) { |
44 | | - return true; |
| 15 | +@JsonDeserialize(using = CompositionBehavior.Deserializer.class) |
| 16 | +public interface CompositionBehavior { |
| 17 | + class Deserializer extends JsonDeserializer<CompositionBehavior> { |
| 18 | + |
| 19 | + private static final Logger LOGGER = Logger.getLogger(Deserializer.class.getName()); |
| 20 | + |
| 21 | + @Override |
| 22 | + public CompositionBehavior deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { |
| 23 | + JsonNode tree = jp.readValueAsTree(); |
| 24 | + // deserialize CompositionInjectionBehavior |
| 25 | + if (tree.isObject()) { |
| 26 | + try (JsonParser parser = tree.traverse(jp.getCodec())) { |
| 27 | + return parser.readValueAs(CompositionInjectionBehavior.class); |
| 28 | + } catch (Exception e) { |
| 29 | + // deserialization failed, continue |
| 30 | + LOGGER.finest( |
| 31 | + "Failed to deserialize oneOf CompositionInjectionBehavior (error: " + e.getMessage() + ") (type: CompositionInjectionBehavior)" |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + // deserialize CompositionMultifeedBehavior |
| 36 | + if (tree.isObject()) { |
| 37 | + try (JsonParser parser = tree.traverse(jp.getCodec())) { |
| 38 | + return parser.readValueAs(CompositionMultifeedBehavior.class); |
| 39 | + } catch (Exception e) { |
| 40 | + // deserialization failed, continue |
| 41 | + LOGGER.finest( |
| 42 | + "Failed to deserialize oneOf CompositionMultifeedBehavior (error: " + e.getMessage() + ") (type: CompositionMultifeedBehavior)" |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | + throw new AlgoliaRuntimeException(String.format("Failed to deserialize json element: %s", tree)); |
45 | 47 | } |
46 | | - if (o == null || getClass() != o.getClass()) { |
47 | | - return false; |
48 | | - } |
49 | | - CompositionBehavior compositionBehavior = (CompositionBehavior) o; |
50 | | - return Objects.equals(this.injection, compositionBehavior.injection) && Objects.equals(this.multifeed, compositionBehavior.multifeed); |
51 | | - } |
52 | | - |
53 | | - @Override |
54 | | - public int hashCode() { |
55 | | - return Objects.hash(injection, multifeed); |
56 | | - } |
57 | | - |
58 | | - @Override |
59 | | - public String toString() { |
60 | | - StringBuilder sb = new StringBuilder(); |
61 | | - sb.append("class CompositionBehavior {\n"); |
62 | | - sb.append(" injection: ").append(toIndentedString(injection)).append("\n"); |
63 | | - sb.append(" multifeed: ").append(toIndentedString(multifeed)).append("\n"); |
64 | | - sb.append("}"); |
65 | | - return sb.toString(); |
66 | | - } |
67 | 48 |
|
68 | | - /** |
69 | | - * Convert the given object to string with each line indented by 4 spaces (except the first line). |
70 | | - */ |
71 | | - private String toIndentedString(Object o) { |
72 | | - if (o == null) { |
73 | | - return "null"; |
| 49 | + /** Handle deserialization of the 'null' value. */ |
| 50 | + @Override |
| 51 | + public CompositionBehavior getNullValue(DeserializationContext ctxt) throws JsonMappingException { |
| 52 | + throw new JsonMappingException(ctxt.getParser(), "CompositionBehavior cannot be null"); |
74 | 53 | } |
75 | | - return o.toString().replace("\n", "\n "); |
76 | 54 | } |
77 | 55 | } |
0 commit comments