jerryshao commented on code in PR #12624: URL: https://github.com/apache/gravitino/pull/12624#discussion_r3862491926
########## 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() { Review Comment: `getAdditionalProperties()` (and other array/map getters across these new DTOs, e.g. `DatasetDTO.getFields()`, `DatasetDTO.getPrimaryKey()`, `ExpressionDTO.getDialects()`) return the live internal reference rather than a defensive copy, unlike the mirrored API-model layer (e.g. `AIContextObject.additionalProperties()`, `Dataset.primaryKey()`), which always returns a copy. Because this getter also doubles as the `@JsonAnyGetter` used for serialization, an external mutation (`dto.getAdditionalProperties().put("x", "y")`) silently corrupts the DTO's internal state, changes future JSON output, and can break `equals()`/`hashCode()` consistency if the object was already used as a hash key. Worth returning `Collections.unmodifiableMap(...)` / a copy here (and the analogous array getters elsewhere) to match the API model's immutability guarantees. ########## 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> { + + @Override + public AIContextObjectDTO deserialize(JsonParser parser, DeserializationContext context) + throws IOException { + if (!parser.hasToken(JsonToken.START_OBJECT)) { + throw JsonMappingException.from(parser, "Structured AI context must be an object"); + } + + String instructions = null; + String[] synonyms = null; + String[] examples = null; + Map<String, Object> additionalProperties = new LinkedHashMap<>(); + while (parser.nextToken() != JsonToken.END_OBJECT) { + if (!parser.hasToken(JsonToken.FIELD_NAME)) { + throw JsonMappingException.from(parser, "Expected an AI context property name"); + } + String name = parser.currentName(); + JsonToken valueToken = parser.nextToken(); + switch (name) { + case "instructions": + instructions = readString(parser, valueToken, name); + break; + case "synonyms": + synonyms = readStringArray(parser, valueToken, name); + break; + case "examples": + examples = readStringArray(parser, valueToken, name); + break; + default: + additionalProperties.put(name, readJsonValue(parser, valueToken)); + } + } + + return AIContextObjectDTO.builder() + .withInstructions(instructions) + .withSynonyms(synonyms) + .withExamples(examples) + .withAdditionalProperties(additionalProperties) + .build(); + } + + private static String readString(JsonParser parser, JsonToken token, String name) Review Comment: `readString` requires the `VALUE_STRING` token, so an explicit JSON `null` for `instructions`/`synonyms`/`examples` (e.g. `{"instructions": null}`) throws `JsonMappingException("instructions must be a string")` instead of being treated as absent. This is inconsistent with two things: (1) every other `@Nullable` field on these new DTOs goes through ordinary Jackson bean deserialization and accepts explicit `null` fine, and (2) `readJsonValue` a few lines below already accepts `VALUE_NULL` for *unknown* properties, so `{"foo": null}` succeeds when `foo` is unrecognized but fails when `foo` is one of `instructions`/`synonyms`/`examples`. A client that always emits full JSON with explicit nulls for absent optional fields will be rejected here. Consider special-casing `JsonToken.VALUE_NULL` to return `null` before the `VALUE_STRING`/`START_ARRAY` checks in `readString`/`readStringArray`. -- 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]
