lasdf1234 commented on code in PR #14520: URL: https://github.com/apache/gravitino/pull/14520#discussion_r4452808225
########## 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: Fixed in 27ea828586. `enabled` now goes through Jackson's `Boolean` binding (`treeToValue`), so `"typo"`, `{}`, and `[]` fail deserialization instead of becoming `false`. Covered in `TestPolicyCreateRequest`. ########## 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: Fixed in 27ea828586. The deserializer now rejects properties outside `name`, `comment`, `policyType`, `enabled`, and `content`, so `"enabld": false` is a 400 again instead of creating an enabled policy. Covered in `TestPolicyCreateRequest`. -- 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]
