Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Customizing the Parameters DSL

Many times we realize that in order to provide the best possible experience for the user, we need to customize or limit certain aspects of a given parameter of a component.

Receiving Strings As Reference Or As Value

Let’s say we have an operation like the following:


         
      
1
2
3
public void send(@Connection MyConnection connection, Object value) {
    connection.doSend(value);
}

Which will be invoked like this:


         
      
1
<demo:send value="requestPayload"/>

By default, Mule will consider that requestPayload is actually a reference to an Object in the registry, like a global list or POJO. If instead we want the extension to actually receive the requestPayload String, then we need to do a slightly modification in the operation declaration, using the @ParameterDsl` annotation:


         
      
1
2
3
public void send(@Connection MyConnection connection, @ParameterDsl(allowReferences = false) Object value) {
    connection.doSend(value);
}

Since we explicitly disable the usage of references, now the operation will indeed receive the String value.

Defining How Complex Types Can Be Used

Any Module is able to expose POJOs that conform part of the Module API, like the following:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// This is the POJO declaration
public class Door {

  @Parameter
  private boolean isLocked;

  @Parameter
  private String color;

}

// This is the configuration
public class HouseConfig {

  @Parameter
  private Door mainDoor;

}

When having this kind of parameters, the Module developer has the responsibility to configure how the Door can be used in the DSL. For this, we have the @TypeDsl annotation, that allows you to configure two important things:

  • allowInlineDefinition: Defines if the POJO (in the example, the Door) can be declared inline for the element that uses it as parameter. If this value is true, then the user can declare:


         
      
1
2
3
<demo:config name="myConfig"> (1)
    <demo:door isLocked="true" color="red"/> (2)
</demo:config>
1 The config is declared as the container of the parameter
2 A Door is declared inline of the config

         
      
1
2
3
<demo:door name="MyGlobalDoor" isLocked="true" color="red"/> (1)

<demo:config name="myConfig" door-ref="MyGlobalDoor"/> (2)
1 A Door is declared as a global element of the application
2 The config is declared separately, referencing the global Door element by name with the door-ref parameter