-
Notifications
You must be signed in to change notification settings - Fork 748
Fix collection type resolution in DefaultConfiguratorRegistry #2833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
timja
merged 3 commits into
jenkinsci:master
from
somiljain2006:Collection-type-parameter-safety
Apr 16, 2026
+120
−13
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
plugin/src/test/java/io/jenkins/plugins/casc/impl/DefaultConfiguratorRegistryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package io.jenkins.plugins.casc.impl; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import hudson.tasks.Builder; | ||
| import io.jenkins.plugins.casc.Configurator; | ||
| import io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator; | ||
| import io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator; | ||
| import java.lang.reflect.Type; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
|
|
||
| public class DefaultConfiguratorRegistryTest { | ||
|
|
||
| @Rule | ||
| public JenkinsRule j = new JenkinsRule(); | ||
|
|
||
| private DefaultConfiguratorRegistry registry; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| registry = new DefaultConfiguratorRegistry(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class DummyTarget<T extends Builder> { | ||
| @SuppressWarnings("rawtypes") | ||
| public List rawList; | ||
|
|
||
| public List<List<String>> nestedList; | ||
| public List<? extends Builder> wildcardList; | ||
| public List<T> typeVarList; | ||
| public List<?> unknownList; | ||
| } | ||
|
|
||
| public static class StringList extends ArrayList<String> {} | ||
|
|
||
| private Type getTypeOf(String fieldName) throws NoSuchFieldException { | ||
| return DummyTarget.class.getDeclaredField(fieldName).getGenericType(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSafelyHandleRawCollections() throws Exception { | ||
| Type rawListType = getTypeOf("rawList"); | ||
|
|
||
| Configurator<?> configurator = registry.lookup(rawListType); | ||
|
|
||
| assertNull("Raw collections should safely fall through and return null", configurator); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSafelyExtractNestedGenerics() throws Exception { | ||
| Type nestedListType = getTypeOf("nestedList"); | ||
|
|
||
| Configurator<?> configurator = registry.lookup(nestedListType); | ||
|
|
||
| assertNotNull("Configurator should be found for nested generic", configurator); | ||
| assertTrue("Should resolve to PrimitiveConfigurator", configurator instanceof PrimitiveConfigurator); | ||
| assertEquals("Target should resolve exactly to String.class", String.class, configurator.getTarget()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSafelyExtractWildcardUpperBounds() throws Exception { | ||
| Type wildcardListType = getTypeOf("wildcardList"); | ||
|
|
||
| Configurator<?> configurator = registry.lookup(wildcardListType); | ||
|
|
||
| assertNotNull("Configurator should be found for wildcard", configurator); | ||
| assertTrue( | ||
| "Should resolve to HeteroDescribableConfigurator", | ||
| configurator instanceof HeteroDescribableConfigurator); | ||
| assertEquals("Target should resolve exactly to Builder.class", Builder.class, configurator.getTarget()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSafelyExtractTypeVariables() throws Exception { | ||
| Type typeVarListType = getTypeOf("typeVarList"); | ||
|
|
||
| Configurator<?> configurator = registry.lookup(typeVarListType); | ||
|
|
||
| assertNotNull("Configurator should be found for TypeVariable", configurator); | ||
| assertTrue( | ||
| "Should resolve to HeteroDescribableConfigurator", | ||
| configurator instanceof HeteroDescribableConfigurator); | ||
| assertEquals("Target should resolve exactly to Builder.class", Builder.class, configurator.getTarget()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldHandleCustomCollectionSubclass() { | ||
| Configurator<?> configurator = registry.lookup(StringList.class); | ||
|
|
||
| assertNotNull("Configurator should be found for custom collection subclass", configurator); | ||
| assertTrue("Should resolve to PrimitiveConfigurator", configurator instanceof PrimitiveConfigurator); | ||
| assertEquals("Target should resolve exactly to String.class", String.class, configurator.getTarget()); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSafelyHandleUnboundedWildcards() throws Exception { | ||
| Type unknownListType = getTypeOf("unknownList"); | ||
|
|
||
| Configurator<?> configurator = registry.lookup(unknownListType); | ||
|
|
||
| assertNull("Unbounded wildcards resolve to Object and should safely return null", configurator); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.