java.lang.Object
com.inductiveautomation.ignition.gateway.dataroutes.openapi.SchemaUtil

public class SchemaUtil extends Object

A utility class for creating JSON schemas in JsonObject form.

If you're trying to parse a schema that you already have in JSON for schema validation, you should use JsonSchema instead.

This class offers two distinct ways to create a schema:

  1. Reflective creation - This is the most convenient way to create a schema, assuming you have a Java Record class that represents your JSON object structure. Using fromType(Class), a schema will automatically be created using reflection that represents the structure, fields, and types found in the record class(es). For example:
                     JsonObject mySchema = SchemaUtil.fromRecordType(MyRecord.class);
                 
  2. Manual creation - This is the most flexible way to create a schema, but also the most verbose. Using builder(), you can manually create a schema by specifying types, constraints, and adding properties. For example:
                     JsonObject mySchema = SchemaUtil.builder()
                         .type(JsonType.OBJECT)
                         .property("name", JsonType.STRING)
                         .property("age", JsonType.NUMBER)
                         .build();
                 

  • Field Details

    • FORM_SETTINGS_KEY

      public static final String FORM_SETTINGS_KEY
      json-schema vendor extension key for form settings. Value=`x-form`
      See Also:
  • Method Details

    • builder

      public static SchemaUtil.Builder builder()
      Create a new schema builder.
    • fromPropertyMap

      public static com.inductiveautomation.ignition.common.gson.JsonObject fromPropertyMap(Map<Property<?>,Field> properties, UnaryOperator<String> nameMapper, com.inductiveautomation.ignition.common.gson.Gson exampleSerializer)
      Builds a schema for an object composed of the given properties.
      Throws:
      IllegalArgumentException - if a circular reference is detected while reflectively creating the schema
    • fromType

      public static com.inductiveautomation.ignition.common.gson.JsonObject fromType(Class<?> type)
      Reflectively create a JSON schema from a type. This will create a schema with a property for each field found using Class.getDeclaredFields().
    • fromType

      public static com.inductiveautomation.ignition.common.gson.JsonObject fromType(Class<?> type, @Nullable Consumer<SchemaUtil.Builder> builderConsumer)
      Reflectively create a JSON schema from a type. This will create a schema with a property for each field found using Class.getDeclaredFields(), and allows for further customization of the schema using the builderConsumer
      Parameters:
      type - Type to reflectively create a schema from
      builderConsumer - A consumer that can further customize the schema
      Returns:
      A JSON schema for the given record type
      Throws:
      IllegalArgumentException - if a circular reference is detected while reflectively creating the schema
    • buildSecretConfigSchema

      public static void buildSecretConfigSchema(SchemaUtil.Builder schema, com.inductiveautomation.ignition.common.gson.JsonObject schemaValues)
    • parseNumber

      public static Number parseNumber(Class<?> clazz, String value)
      Parse a Number from a String.
      Parameters:
      clazz - The class to parse the String value to.
      value - The String value to parse.
      Returns:
      The parsed value as a Number or null if the value was not parsable as the specified class.