Skip to content

Commit

Permalink
Allow using classes not supported by structs for `DescribableContex…
Browse files Browse the repository at this point in the history
…t` (#1202)

* Check actual type

This allows for types that are not directly supported by structs to be
used in Job DSL. An example for this is hudson.util.Secret, that's used
by by the ssh-credentials plugin. See JENKINS-57435 for details.

* Add test case for fallback to actual type

* Address CodeNarc issues in tests
  • Loading branch information
twz123 authored Apr 14, 2022
1 parent 6eae93a commit 30022ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class DescribableContext implements Context {
return value instanceof Closure || value == null
} else if (parameterType instanceof HeterogeneousObjectType) {
return value instanceof Closure || value == null || parameterType.actualType == Object
} else if (parameterType.actualType instanceof Class) {
return value == null || ((Class) parameterType.actualType).isInstance(value)
}
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import org.kohsuke.stapler.DataBoundConstructor
import org.kohsuke.stapler.DataBoundSetter

class DummyTrigger extends Trigger<Job> {

@SuppressWarnings('EmptyClass')
static class UnsupportedByStructs {
// We are not supported.
}

@DataBoundSetter
String aString

Expand Down Expand Up @@ -48,6 +54,9 @@ class DummyTrigger extends Trigger<Job> {
@DataBoundSetter
List<Thread.State> enumList

@DataBoundSetter
UnsupportedByStructs unsupportedByStructs

@DataBoundConstructor
DummyTrigger() throws ANTLRException {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,19 @@ class DescribableContextSpec extends Specification {
instance.enumList.empty
}

def 'falls back to plain Class if type is not supported by structs'() {
setup:
DescribableContext context = new DescribableContext(new DescribableModel(DummyTrigger), jobManagement)

when:
context.unsupportedByStructs(new DummyTrigger.UnsupportedByStructs())
def instance = context.createInstance()

then:
instance instanceof DummyTrigger
instance.unsupportedByStructs instanceof DummyTrigger.UnsupportedByStructs
}

def 'required parameter missing'() {
setup:
DescribableContext context = new DescribableContext(new DescribableModel(ADuplicateBuilder), jobManagement)
Expand Down

0 comments on commit 30022ab

Please sign in to comment.