Discriminator
It helps in choosing a <structure> from a group of structures based on the value of a particular <property> in them. This property has to be either configured in the <mapping> or in the discriminator-name attribute of the structure to map values with the respective structures.
Possible Locations
Child Elements
- Mapping recurrent
- Always use the discriminator-name attribute when defining a new <structure> for the discriminator.
- When referring to an existing structure for the discriminator, use the discriminator-name attribute that is already provided for it. Use <mapping> for overriding the existing mapping details or for providing a new match when the structure does not hold any discriminator-name values.
- When you refer to existing structures in the discriminator, the mapping details found at the discriminator level will be given the highest priority.
- If you have not used mapping and discriminator-name, the structure name will be considered as the value of the discriminator’s property for structures newly defined for the discriminator. For a referred structure, its ref-name will be used.
Attributes
Name | Type | Description |
---|---|---|
property-name required |
String | Name of the discriminator <property> |
-
The discriminator <property> need not always be a part of the <structure> that is chosen. It can also be a dynamic property created in the runtime to decide which structure has to be picked for the property / <content> based on your use case.
-
The discriminator’s property will be included in the generation of source codes only when it is declared in each of the structure. If it is not included in the structure and declared only at the discriminator level, the property will be excluded in the POJO of generated source code.
Example
In the travelers resource, we have a <property> called service, whose possible values are Flight and Accommodation. Depending on the chosen service, we need to assign a specific <structure> to the property. This requirement can be fulfilled by mapping the values of the property “type” with the corresponding structures.
Using discriminator-name
Utilize the discriminator-name attribute when defining a new <structure>. The discriminator specified in a <structure-group> functions based on the map details provided in this attribute.
These structures can also be declared in the <components>, facilitating their use in a different resource. This minimizes the effort of manually associating values to their respective structures every time you use a discriminator.
Following is a sample created using the discriminator-name for this scenario,
copy<property name="service" type="map"> <structure-group name="services" type="one-of"> <structure name="flight" discriminator-name="Flight"> <property name="flight_no" type="string" /> <property name="departure_airport" type="string" /> <property name="arrival_airport" type="string" /> </structure> <structure name="accommodation" discriminator-name="Accommodation"> <property name="accommodation_name" type="string" /> <property name="check_in_date" type="string" /> <property name="check_out_date" type="string" /> </structure> <discriminator property-name="type" /> <!-- discriminator's property not included in structure --> </structure-group> </property>
ZEST will not include the dynamic property “type” in the source code POJOs, as it is not included in the structures and is declared only at the discriminator level.
Using mapping
Employ the <mapping> when referencing a <structure> from different resource, for which the developer hasn’t specified any discriminator-name. You can also use it to override the previously configured discriminator-name.
In the scenario mentioned earlier, imagine there are individual resources for Flight and Accommodation services, each resource developer has defined a structure for using it in other resources. The flights resource developer has not specified any discriminator-name but has added the discriminator’s property to the structure. The accommodations resource developer has specified Hotel as the discriminator-name.
Below are the structures declared in the <components> of each resource.
copystructure in the flights resource: <components> <structure ref-name="flight_template" name="external_resource_use"> <property name="type" type="string" values="Flight" /> <!-- discriminator's property --> <property name="flight_no" type="string" /> <property name="departure_airport" type="string" /> <property name="arrival_airport" type="string" /> </structure> </components>
copystructure in the accommodations resource:
<components> <structure ref-name="accommodation_template" name="external_resource_use" discriminator-name="Hotel"> <property name="accommodation_name" type="string" /> <property name="check_in_date" type="string" /> <property name="check_out_date" type="string" /> </structure> </components>
Following is the sample of mapping flight_template with the value Flight and overriding the Hotel value with Accommodation for the accommodation_template.
copy<property name="service" type="map"> <structure-group name="services" type="one-of"> <structure-ref name="flights.flight_template" /> <structure-ref name="accommodations.accommodation_template" /> <discriminator property-name="type"> <mapping name="Flight" structure-name="flights.flight_template"/> <mapping name="Accommodation" structure-name="accommodations.accommodation_template"/> <!-- Hotel is overridden with Accommodation --> </discriminator> </structure-group> </property>
In this sample, the discriminator’s <property> is included only in the flight_template structure. So, only the POJO generated for this structure will include the property “type”.
Last Updated 2025-05-30 16:54:59 +0530 +0530
Yes
No
Send your feedback to us