mchades commented on code in PR #14520: URL: https://github.com/apache/gravitino/pull/14520#discussion_r4452706428
########## common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequestDeserializer.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.requests; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import org.apache.gravitino.dto.policy.PolicyContentDTO; + +/** + * Deserializes {@link PolicyCreateRequest}, resolving {@code policyType} before reading {@code + * content} so mis-cased or unknown built-in types are reported instead of failing as malformed + * custom content. + */ +public class PolicyCreateRequestDeserializer extends JsonDeserializer<PolicyCreateRequest> { + + @Override + public PolicyCreateRequest deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException { + ObjectMapper mapper = (ObjectMapper) p.getCodec(); + JsonNode node = mapper.readTree(p); + String policyType = textOrNull(node.get("policyType")); + Class<? extends PolicyContentDTO> contentClass; + try { + contentClass = PolicyWireContentTypes.contentClassFor(policyType); + } catch (IllegalArgumentException e) { + // Wrap so Jersey's JsonMappingExceptionMapper returns 400 with this message. + throw JsonMappingException.from(p, e.getMessage(), e); + } + + PolicyContentDTO content = null; + JsonNode contentNode = node.get("content"); + if (contentNode != null && !contentNode.isNull()) { + content = mapper.treeToValue(contentNode, contentClass); + } + + boolean enabled = true; + JsonNode enabledNode = node.get("enabled"); + if (enabledNode != null && !enabledNode.isNull()) { + enabled = enabledNode.asBoolean(); Review Comment: `JsonNode.asBoolean()` silently returns `false` for values such as `"typo"`, `{}`, and `[]`. With otherwise valid custom content, these requests now return 200 and create a disabled policy; the previous deserializer returned 400. Please retain Jackson's Boolean binding/validation here instead of coercing malformed input, and add a regression test. ########## common/src/main/java/org/apache/gravitino/dto/requests/PolicyCreateRequestDeserializer.java: ########## @@ -0,0 +1,74 @@ +/* + * 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.requests; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import org.apache.gravitino.dto.policy.PolicyContentDTO; + +/** + * Deserializes {@link PolicyCreateRequest}, resolving {@code policyType} before reading {@code + * content} so mis-cased or unknown built-in types are reported instead of failing as malformed + * custom content. + */ +public class PolicyCreateRequestDeserializer extends JsonDeserializer<PolicyCreateRequest> { + + @Override + public PolicyCreateRequest deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException { + ObjectMapper mapper = (ObjectMapper) p.getCodec(); + JsonNode node = mapper.readTree(p); Review Comment: Reading the request as a tree and selecting known keys bypasses Jackson's unknown-property check. A request with `"enabld": false` now returns 200 and creates an enabled policy (`enabled` defaults to true), whereas it previously returned 400. Please preserve rejection of unknown create-request properties and cover this typo case. -- 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]
