|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2019-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.api.client.v1.model; |
| 8 | + |
| 9 | +import com.datadog.api.client.AbstractOpenApiSchema; |
| 10 | +import com.datadog.api.client.JSON; |
| 11 | +import com.datadog.api.client.UnparsedObject; |
| 12 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 13 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 14 | +import com.fasterxml.jackson.core.JsonParser; |
| 15 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 16 | +import com.fasterxml.jackson.core.JsonToken; |
| 17 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 18 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 19 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 24 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 25 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 26 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 27 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
| 28 | +import jakarta.ws.rs.core.GenericType; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.HashSet; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.logging.Level; |
| 35 | +import java.util.logging.Logger; |
| 36 | + |
| 37 | +@jakarta.annotation.Generated( |
| 38 | + value = "https://github.com/DataDog/datadog-api-client-java/blob/master/.generator") |
| 39 | +@JsonDeserialize( |
| 40 | + using = SyntheticsTestRequestDNSServerPort.SyntheticsTestRequestDNSServerPortDeserializer.class) |
| 41 | +@JsonSerialize( |
| 42 | + using = SyntheticsTestRequestDNSServerPort.SyntheticsTestRequestDNSServerPortSerializer.class) |
| 43 | +public class SyntheticsTestRequestDNSServerPort extends AbstractOpenApiSchema { |
| 44 | + private static final Logger log = |
| 45 | + Logger.getLogger(SyntheticsTestRequestDNSServerPort.class.getName()); |
| 46 | + |
| 47 | + @JsonIgnore public boolean unparsed = false; |
| 48 | + |
| 49 | + public static class SyntheticsTestRequestDNSServerPortSerializer |
| 50 | + extends StdSerializer<SyntheticsTestRequestDNSServerPort> { |
| 51 | + public SyntheticsTestRequestDNSServerPortSerializer( |
| 52 | + Class<SyntheticsTestRequestDNSServerPort> t) { |
| 53 | + super(t); |
| 54 | + } |
| 55 | + |
| 56 | + public SyntheticsTestRequestDNSServerPortSerializer() { |
| 57 | + this(null); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void serialize( |
| 62 | + SyntheticsTestRequestDNSServerPort value, JsonGenerator jgen, SerializerProvider provider) |
| 63 | + throws IOException, JsonProcessingException { |
| 64 | + jgen.writeObject(value.getActualInstance()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static class SyntheticsTestRequestDNSServerPortDeserializer |
| 69 | + extends StdDeserializer<SyntheticsTestRequestDNSServerPort> { |
| 70 | + public SyntheticsTestRequestDNSServerPortDeserializer() { |
| 71 | + this(SyntheticsTestRequestDNSServerPort.class); |
| 72 | + } |
| 73 | + |
| 74 | + public SyntheticsTestRequestDNSServerPortDeserializer(Class<?> vc) { |
| 75 | + super(vc); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public SyntheticsTestRequestDNSServerPort deserialize( |
| 80 | + JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { |
| 81 | + JsonNode tree = jp.readValueAsTree(); |
| 82 | + Object deserialized = null; |
| 83 | + Object tmp = null; |
| 84 | + boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS); |
| 85 | + int match = 0; |
| 86 | + JsonToken token = tree.traverse(jp.getCodec()).nextToken(); |
| 87 | + // deserialize Long |
| 88 | + try { |
| 89 | + boolean attemptParsing = true; |
| 90 | + // ensure that we respect type coercion as set on the client ObjectMapper |
| 91 | + if (Long.class.equals(Integer.class) |
| 92 | + || Long.class.equals(Long.class) |
| 93 | + || Long.class.equals(Float.class) |
| 94 | + || Long.class.equals(Double.class) |
| 95 | + || Long.class.equals(Boolean.class) |
| 96 | + || Long.class.equals(String.class)) { |
| 97 | + attemptParsing = typeCoercion; |
| 98 | + if (!attemptParsing) { |
| 99 | + attemptParsing |= |
| 100 | + ((Long.class.equals(Integer.class) || Long.class.equals(Long.class)) |
| 101 | + && token == JsonToken.VALUE_NUMBER_INT); |
| 102 | + attemptParsing |= |
| 103 | + ((Long.class.equals(Float.class) || Long.class.equals(Double.class)) |
| 104 | + && (token == JsonToken.VALUE_NUMBER_FLOAT |
| 105 | + || token == JsonToken.VALUE_NUMBER_INT)); |
| 106 | + attemptParsing |= |
| 107 | + (Long.class.equals(Boolean.class) |
| 108 | + && (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE)); |
| 109 | + attemptParsing |= (Long.class.equals(String.class) && token == JsonToken.VALUE_STRING); |
| 110 | + } |
| 111 | + } |
| 112 | + if (attemptParsing) { |
| 113 | + tmp = tree.traverse(jp.getCodec()).readValueAs(Long.class); |
| 114 | + // TODO: there is no validation against JSON schema constraints |
| 115 | + // (min, max, enum, pattern...), this does not perform a strict JSON |
| 116 | + // validation, which means the 'match' count may be higher than it should be. |
| 117 | + deserialized = tmp; |
| 118 | + match++; |
| 119 | + |
| 120 | + log.log(Level.FINER, "Input data matches schema 'Long'"); |
| 121 | + } |
| 122 | + } catch (Exception e) { |
| 123 | + // deserialization failed, continue |
| 124 | + log.log(Level.FINER, "Input data does not match schema 'Long'", e); |
| 125 | + } |
| 126 | + |
| 127 | + // deserialize String |
| 128 | + try { |
| 129 | + boolean attemptParsing = true; |
| 130 | + // ensure that we respect type coercion as set on the client ObjectMapper |
| 131 | + if (String.class.equals(Integer.class) |
| 132 | + || String.class.equals(Long.class) |
| 133 | + || String.class.equals(Float.class) |
| 134 | + || String.class.equals(Double.class) |
| 135 | + || String.class.equals(Boolean.class) |
| 136 | + || String.class.equals(String.class)) { |
| 137 | + attemptParsing = typeCoercion; |
| 138 | + if (!attemptParsing) { |
| 139 | + attemptParsing |= |
| 140 | + ((String.class.equals(Integer.class) || String.class.equals(Long.class)) |
| 141 | + && token == JsonToken.VALUE_NUMBER_INT); |
| 142 | + attemptParsing |= |
| 143 | + ((String.class.equals(Float.class) || String.class.equals(Double.class)) |
| 144 | + && (token == JsonToken.VALUE_NUMBER_FLOAT |
| 145 | + || token == JsonToken.VALUE_NUMBER_INT)); |
| 146 | + attemptParsing |= |
| 147 | + (String.class.equals(Boolean.class) |
| 148 | + && (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE)); |
| 149 | + attemptParsing |= |
| 150 | + (String.class.equals(String.class) && token == JsonToken.VALUE_STRING); |
| 151 | + } |
| 152 | + } |
| 153 | + if (attemptParsing) { |
| 154 | + tmp = tree.traverse(jp.getCodec()).readValueAs(String.class); |
| 155 | + // TODO: there is no validation against JSON schema constraints |
| 156 | + // (min, max, enum, pattern...), this does not perform a strict JSON |
| 157 | + // validation, which means the 'match' count may be higher than it should be. |
| 158 | + deserialized = tmp; |
| 159 | + match++; |
| 160 | + |
| 161 | + log.log(Level.FINER, "Input data matches schema 'String'"); |
| 162 | + } |
| 163 | + } catch (Exception e) { |
| 164 | + // deserialization failed, continue |
| 165 | + log.log(Level.FINER, "Input data does not match schema 'String'", e); |
| 166 | + } |
| 167 | + |
| 168 | + SyntheticsTestRequestDNSServerPort ret = new SyntheticsTestRequestDNSServerPort(); |
| 169 | + if (match == 1) { |
| 170 | + ret.setActualInstance(deserialized); |
| 171 | + } else { |
| 172 | + Map<String, Object> res = |
| 173 | + new ObjectMapper() |
| 174 | + .readValue( |
| 175 | + tree.traverse(jp.getCodec()).readValueAsTree().toString(), |
| 176 | + new TypeReference<Map<String, Object>>() {}); |
| 177 | + ret.setActualInstance(new UnparsedObject(res)); |
| 178 | + } |
| 179 | + return ret; |
| 180 | + } |
| 181 | + |
| 182 | + /** Handle deserialization of the 'null' value. */ |
| 183 | + @Override |
| 184 | + public SyntheticsTestRequestDNSServerPort getNullValue(DeserializationContext ctxt) |
| 185 | + throws JsonMappingException { |
| 186 | + throw new JsonMappingException( |
| 187 | + ctxt.getParser(), "SyntheticsTestRequestDNSServerPort cannot be null"); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // store a list of schema names defined in oneOf |
| 192 | + public static final Map<String, GenericType> schemas = new HashMap<String, GenericType>(); |
| 193 | + |
| 194 | + public SyntheticsTestRequestDNSServerPort() { |
| 195 | + super("oneOf", Boolean.FALSE); |
| 196 | + } |
| 197 | + |
| 198 | + public SyntheticsTestRequestDNSServerPort(Long o) { |
| 199 | + super("oneOf", Boolean.FALSE); |
| 200 | + setActualInstance(o); |
| 201 | + } |
| 202 | + |
| 203 | + public SyntheticsTestRequestDNSServerPort(String o) { |
| 204 | + super("oneOf", Boolean.FALSE); |
| 205 | + setActualInstance(o); |
| 206 | + } |
| 207 | + |
| 208 | + static { |
| 209 | + schemas.put("Long", new GenericType<Long>() {}); |
| 210 | + schemas.put("String", new GenericType<String>() {}); |
| 211 | + JSON.registerDescendants( |
| 212 | + SyntheticsTestRequestDNSServerPort.class, Collections.unmodifiableMap(schemas)); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public Map<String, GenericType> getSchemas() { |
| 217 | + return SyntheticsTestRequestDNSServerPort.schemas; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Set the instance that matches the oneOf child schema, check the instance parameter is valid |
| 222 | + * against the oneOf child schemas: Long, String |
| 223 | + * |
| 224 | + * <p>It could be an instance of the 'oneOf' schemas. The oneOf child schemas may themselves be a |
| 225 | + * composed schema (allOf, anyOf, oneOf). |
| 226 | + */ |
| 227 | + @Override |
| 228 | + public void setActualInstance(Object instance) { |
| 229 | + if (JSON.isInstanceOf(Long.class, instance, new HashSet<Class<?>>())) { |
| 230 | + super.setActualInstance(instance); |
| 231 | + return; |
| 232 | + } |
| 233 | + if (JSON.isInstanceOf(String.class, instance, new HashSet<Class<?>>())) { |
| 234 | + super.setActualInstance(instance); |
| 235 | + return; |
| 236 | + } |
| 237 | + |
| 238 | + if (JSON.isInstanceOf(UnparsedObject.class, instance, new HashSet<Class<?>>())) { |
| 239 | + super.setActualInstance(instance); |
| 240 | + return; |
| 241 | + } |
| 242 | + throw new RuntimeException("Invalid instance type. Must be Long, String"); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Get the actual instance, which can be the following: Long, String |
| 247 | + * |
| 248 | + * @return The actual instance (Long, String) |
| 249 | + */ |
| 250 | + @Override |
| 251 | + public Object getActualInstance() { |
| 252 | + return super.getActualInstance(); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Get the actual instance of `Long`. If the actual instance is not `Long`, the ClassCastException |
| 257 | + * will be thrown. |
| 258 | + * |
| 259 | + * @return The actual instance of `Long` |
| 260 | + * @throws ClassCastException if the instance is not `Long` |
| 261 | + */ |
| 262 | + public Long getLong() throws ClassCastException { |
| 263 | + return (Long) super.getActualInstance(); |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Get the actual instance of `String`. If the actual instance is not `String`, the |
| 268 | + * ClassCastException will be thrown. |
| 269 | + * |
| 270 | + * @return The actual instance of `String` |
| 271 | + * @throws ClassCastException if the instance is not `String` |
| 272 | + */ |
| 273 | + public String getString() throws ClassCastException { |
| 274 | + return (String) super.getActualInstance(); |
| 275 | + } |
| 276 | +} |
0 commit comments