mchades commented on code in PR #12624: URL: https://github.com/apache/gravitino/pull/12624#discussion_r3863085476
########## common/src/main/java/org/apache/gravitino/dto/semantic/AIContextObjectDTO.java: ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.gravitino.dto.semantic; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.Nullable; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.apache.gravitino.semantic.AIContextObject; + +/** DTO for structured Semantic Model AI context. */ +@Getter +@EqualsAndHashCode +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@Builder(setterPrefix = "with") +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({"instructions", "synonyms", "examples"}) +@JsonDeserialize(using = AIContextObjectDTO.Deserializer.class) +public class AIContextObjectDTO { + + @Nullable + @JsonProperty("instructions") + private String instructions; + + @Nullable + @JsonProperty("synonyms") + private String[] synonyms; + + @Nullable + @JsonProperty("examples") + private String[] examples; + + @JsonIgnore + @Getter(AccessLevel.NONE) + @Builder.Default + private Map<String, Object> additionalProperties = new LinkedHashMap<>(); + + /** + * Creates a structured AI context DTO from an API model. + * + * @param aiContextObject The API AI context object. + * @return The structured AI context DTO. + */ + public static AIContextObjectDTO fromAIContextObject(AIContextObject aiContextObject) { + return builder() + .withInstructions(aiContextObject.instructions()) + .withSynonyms(aiContextObject.synonyms()) + .withExamples(aiContextObject.examples()) + .withAdditionalProperties(new LinkedHashMap<>(aiContextObject.additionalProperties())) + .build(); + } + + /** + * Converts this DTO to an API AI context object. + * + * @return The API AI context object. + */ + public AIContextObject toAIContextObject() { + return AIContextObject.builder() + .withInstructions(instructions) + .withSynonyms(synonyms) + .withExamples(examples) + .withAdditionalProperties( + additionalProperties == null ? Collections.emptyMap() : additionalProperties) + .build(); + } + + /** + * Returns unknown AI-context properties in their input order. + * + * @return The additional properties. + */ + @JsonAnyGetter + public Map<String, Object> getAdditionalProperties() { + return additionalProperties == null ? Collections.emptyMap() : additionalProperties; + } + + /** Deserializes structured AI context while retaining unknown JSON values and their order. */ + public static final class Deserializer extends JsonDeserializer<AIContextObjectDTO> { Review Comment: The asymmetry comes from where the information lives. For example: ```json { "instructions": "Use the fiscal year", "priority": 1, "threshold": 0.12345678901234567890 } ``` When serializing, the DTO already separates `instructions` from `additionalProperties`. Jackson writes `instructions` through `@JsonProperty`, while `@JsonAnyGetter` flattens `priority` and `threshold`, so the default serializer already produces the expected JSON shape. When deserializing, all properties arrive as flat JSON fields. Custom logic is needed to recognize `instructions` as a standard field, route the other fields into `additionalProperties`, preserve their order, retain exact `BigInteger`/`BigDecimal` values, and enforce strict field types. Therefore, a custom serializer would only duplicate the existing default behavior. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
