|
16 | 16 |
|
17 | 17 | package com.google.genai; |
18 | 18 |
|
| 19 | +import com.fasterxml.jackson.core.JsonParser; |
| 20 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 24 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 25 | +import com.google.genai.types.ContentReferenceImage; |
| 26 | +import com.google.genai.types.ControlReferenceImage; |
| 27 | +import com.google.genai.types.MaskReferenceImage; |
| 28 | +import com.google.genai.types.RawReferenceImage; |
| 29 | +import com.google.genai.types.ReferenceImage; |
| 30 | +import com.google.genai.types.StyleReferenceImage; |
| 31 | +import com.google.genai.types.SubjectReferenceImage; |
| 32 | +import java.io.IOException; |
| 33 | + |
19 | 34 | public final class TestUtils { |
20 | 35 | static final String API_KEY = "api-key"; |
21 | 36 | static final String PROJECT = "project"; |
22 | 37 | static final String LOCATION = "location"; |
23 | 38 |
|
| 39 | + private static ObjectMapper testObjectMapper; |
| 40 | + |
| 41 | + public static ObjectMapper getTestObjectMapper() { |
| 42 | + if (testObjectMapper == null) { |
| 43 | + testObjectMapper = JsonSerializable.objectMapper.copy(); |
| 44 | + SimpleModule customModule = new SimpleModule(); |
| 45 | + customModule.addDeserializer(ReferenceImage.class, new ReferenceImageDeserializer()); |
| 46 | + testObjectMapper.registerModule(customModule); |
| 47 | + } |
| 48 | + return testObjectMapper; |
| 49 | + } |
| 50 | + |
| 51 | + private static class ReferenceImageDeserializer extends StdDeserializer<ReferenceImage> { |
| 52 | + public ReferenceImageDeserializer() { |
| 53 | + this(null); |
| 54 | + } |
| 55 | + |
| 56 | + public ReferenceImageDeserializer(Class<?> vc) { |
| 57 | + super(vc); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public ReferenceImage deserialize(JsonParser jp, DeserializationContext ctxt) |
| 62 | + throws IOException { |
| 63 | + JsonNode node = jp.getCodec().readTree(jp); |
| 64 | + if (node.isObject()) { |
| 65 | + com.fasterxml.jackson.databind.node.ObjectNode objNode = |
| 66 | + (com.fasterxml.jackson.databind.node.ObjectNode) node; |
| 67 | + if (objNode.has("maskImageConfig")) { |
| 68 | + objNode.set("config", objNode.get("maskImageConfig")); |
| 69 | + } |
| 70 | + if (objNode.has("styleImageConfig")) { |
| 71 | + objNode.set("config", objNode.get("styleImageConfig")); |
| 72 | + } |
| 73 | + if (objNode.has("controlImageConfig")) { |
| 74 | + objNode.set("config", objNode.get("controlImageConfig")); |
| 75 | + } |
| 76 | + if (objNode.has("subjectImageConfig")) { |
| 77 | + objNode.set("config", objNode.get("subjectImageConfig")); |
| 78 | + } |
| 79 | + if (objNode.has("contentImageConfig")) { |
| 80 | + objNode.set("config", objNode.get("contentImageConfig")); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (node.has("referenceType")) { |
| 85 | + String type = node.get("referenceType").asText(); |
| 86 | + if ("REFERENCE_TYPE_RAW".equals(type)) { |
| 87 | + return jp.getCodec().treeToValue(node, RawReferenceImage.class); |
| 88 | + } else if ("REFERENCE_TYPE_MASK".equals(type)) { |
| 89 | + return jp.getCodec().treeToValue(node, MaskReferenceImage.class); |
| 90 | + } else if ("REFERENCE_TYPE_CONTROL".equals(type)) { |
| 91 | + return jp.getCodec().treeToValue(node, ControlReferenceImage.class); |
| 92 | + } else if ("REFERENCE_TYPE_STYLE".equals(type)) { |
| 93 | + return jp.getCodec().treeToValue(node, StyleReferenceImage.class); |
| 94 | + } else if ("REFERENCE_TYPE_SUBJECT".equals(type)) { |
| 95 | + return jp.getCodec().treeToValue(node, SubjectReferenceImage.class); |
| 96 | + } else if ("REFERENCE_TYPE_CONTENT".equals(type)) { |
| 97 | + return jp.getCodec().treeToValue(node, ContentReferenceImage.class); |
| 98 | + } |
| 99 | + } |
| 100 | + throw new IOException("Unknown or missing referenceType for ReferenceImage"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
24 | 104 | private TestUtils() {} |
25 | 105 |
|
26 | 106 | /** Creates a client given the vertexAI and replayId. Can be used in replay tests. */ |
|
0 commit comments