Copilot commented on code in PR #18565:
URL: https://github.com/apache/druid/pull/18565#discussion_r2403446659


##########
processing/src/main/java/org/apache/druid/jackson/StrictTypeIdResolver.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.druid.jackson;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DatabindContext;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.cfg.MapperConfig;
+import com.fasterxml.jackson.databind.jsontype.NamedType;
+import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
+import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
+import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
+import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
+import com.fasterxml.jackson.databind.jsontype.impl.TypeNameIdResolver;
+
+import java.util.Collection;
+
+/**
+ * A strict {@link TypeIdResolver} implementation that validates all incoming 
type ids.
+ * <p>
+ * During deserialization, the type discriminator in the JSON must correspond 
to a registered subtype;
+ * otherwise this resolver will throw an exception instead of silently 
accepting or defaulting.
+ * <p>
+ * An optional default implementation may still be configured and used only 
when the type id is absent.
+ */
+public class StrictTypeIdResolver extends TypeIdResolverBase
+{
+  public static class Builder extends StdTypeResolverBuilder
+  {
+    @Override
+    protected TypeIdResolver idResolver(
+        MapperConfig<?> config,
+        JavaType baseType,
+        PolymorphicTypeValidator subtypeValidator,
+        Collection<NamedType> subtypes,
+        boolean forSer,
+        boolean forDeser
+    )
+    {
+      this._customIdResolver = new StrictTypeIdResolver(config, baseType, 
subtypes, forSer, forDeser);
+      return this._customIdResolver;
+    }
+  }
+
+  protected final JavaType baseType;
+  protected final TypeNameIdResolver delegate;
+
+  StrictTypeIdResolver()
+  {
+    // Required default constructor for Jackson, the instance is never used

Review Comment:
   The comment states 'the instance is never used' but this may be misleading. 
Jackson may instantiate this constructor for reflection purposes. Consider 
clarifying that this constructor creates an unusable instance that should not 
be used for actual resolution.
   ```suggestion
       // Required default constructor for Jackson's reflective instantiation.
       // This creates an unusable instance that should not be used for actual 
resolution.
   ```



##########
processing/src/test/java/org/apache/druid/data/input/impl/DimensionSchemaTest.java:
##########
@@ -46,4 +47,26 @@ public void testStringDimensionSchemaSerde() throws Exception
         OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(schema2), 
DimensionSchema.class)
     );
   }
+
+  @Test
+  public void testDeserializeStrictTypeId() throws Exception
+  {
+    final String invalidType = 
"{\"type\":\"invalid\",\"name\":\"foo\",\"multiValueHandling\":\"ARRAY\",\"createBitmapIndex\":false}";
+    InvalidTypeIdException e = Assert.assertThrows(
+        InvalidTypeIdException.class,
+        () -> OBJECT_MAPPER.readValue(invalidType, DimensionSchema.class)
+    );
+    Assert.assertTrue(e.getMessage().contains(
+        "Could not resolve type id 'invalid' as a subtype of 
`org.apache.druid.data.input.impl.DimensionSchema`: known type ids = [auto, 
double, float, json, long, spatial, string]"));

Review Comment:
   This test is brittle because it relies on the exact error message format and 
the complete list of known type IDs. Consider testing only for key parts of the 
message like 'Could not resolve type id' and 'invalid' to make the test more 
maintainable.
   ```suggestion
       Assert.assertTrue(e.getMessage().contains("Could not resolve type id"));
       Assert.assertTrue(e.getMessage().contains("invalid"));
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to