Class Discriminator

java.lang.Object
org.apache.juneau.bean.openapi3.OpenApiElement
org.apache.juneau.bean.openapi3.Discriminator

public class Discriminator extends OpenApiElement
Used to aid in serialization, deserialization, and validation.

The Discriminator Object is used to aid in serialization, deserialization, and validation. It adds support for polymorphism by allowing schemas to be discriminated based on the value of a specific property. This is particularly useful when working with inheritance hierarchies in object-oriented programming.

OpenAPI Specification:

The Discriminator Object is composed of the following fields:

  • propertyName (string, REQUIRED) - The name of the property in the payload that will hold the discriminator value
  • mapping (map of strings) - An object to hold mappings between payload values and schema names or references
Example:

// Construct using SwaggerBuilder. Discriminator x = discriminator() .setPropertyName("petType") .setMapping(map("dog", "#/components/schemas/Dog", "cat", "#/components/schemas/Cat")); // Serialize using JsonSerializer. String json = Json.from(x); // Or just use toString() which does the same as above. json = x.toString();

// Output { "propertyName": "petType", "mapping": { "dog": "#/components/schemas/Dog", "cat": "#/components/schemas/Cat" } }

See Also:
  • Constructor Details

  • Method Details

    • copy

      public Discriminator copy()
      Make a deep copy of this object.
      Returns:
      A deep copy of this object.
    • getPropertyName

      Bean property getter: propertyName.

      A short description of the target documentation.

      Returns:
      The property value, or null if it is not set.
    • setPropertyName

      Bean property setter: propertyName.

      A short description of the target documentation.

      Parameters:
      value - The new value for this property.
      Can be null to unset the property.
      Returns:
      This object
    • getMapping

      Bean property getter: mapping.

      The URL for the target documentation.

      Returns:
      The property value, or null if it is not set.
    • setMapping

      Bean property setter: mapping.

      The URL for the target documentation.

      Parameters:
      value - The new value for this property.
      Property value is required.
      URIs defined by UriResolver can be used for values.
      Can be null to unset the property.
      Returns:
      This object
    • addMapping

      public Discriminator addMapping(String key, String value)
      Adds one or more values to the mapping property.
      Parameters:
      key - The key. Must not be null.
      value - The value. Must not be null.
      Returns:
      This object
    • get

      public <T> T get(String property, Class<T> type)
      Description copied from class: OpenApiElement
      Generic property getter.

      Can be used to retrieve non-standard Swagger fields such as "$ref".

      Overrides:
      get in class OpenApiElement
      Type Parameters:
      T - The datatype to cast the value to.
      Parameters:
      property - The property name to retrieve.
      type - The datatype to cast the value to.
      Returns:
      The property value, or null if the property does not exist or is not set.
    • set

      public Discriminator set(String property, Object value)
      Description copied from class: OpenApiElement
      Generic property setter.

      Can be used to set non-standard Swagger fields such as "$ref".

      Overrides:
      set in class OpenApiElement
      Parameters:
      property - The property name to set. Must not be null.
      value - The new value for the property.
      Returns:
      This object
    • keySet

      public Set<String> keySet()
      Description copied from class: OpenApiElement
      Returns all the keys on this element.
      Overrides:
      keySet in class OpenApiElement
      Returns:
      All the keys on this element.
      Never null.
    • strict

      Description copied from class: OpenApiElement
      Sets strict mode on this bean.
      Overrides:
      strict in class OpenApiElement
      Returns:
      This object
    • strict

      public Discriminator strict(Object value)
      Description copied from class: OpenApiElement
      Sets strict mode on this bean.
      Overrides:
      strict in class OpenApiElement
      Parameters:
      value - The new value for this property.
      Non-boolean values will be converted to boolean using Boolean.valueOf(value.toString()).
      Can be null (interpreted as false).
      Returns:
      This object