Class SchemaUtil
java.lang.Object
com.inductiveautomation.ignition.gateway.dataroutes.openapi.SchemaUtil
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:
-
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);
-
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();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A builder for creating JSON schemas. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
json-schema vendor extension key for form settings. -
Method Summary
Modifier and TypeMethodDescriptionstatic SchemaUtil.Builder
builder()
Create a new schema builder.static void
buildSecretConfigSchema
(SchemaUtil.Builder schema, com.inductiveautomation.ignition.common.gson.JsonObject schemaValues) 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.static com.inductiveautomation.ignition.common.gson.JsonObject
Reflectively create a JSON schema from a type.static com.inductiveautomation.ignition.common.gson.JsonObject
fromType
(Class<?> type, Consumer<SchemaUtil.Builder> builderConsumer) Reflectively create a JSON schema from a type.static Number
parseNumber
(Class<?> clazz, String value) Parse a Number from a String.
-
Field Details
-
FORM_SETTINGS_KEY
json-schema vendor extension key for form settings. Value=`x-form`- See Also:
-
-
Method Details
-
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
Reflectively create a JSON schema from a type. This will create a schema with a property for each field found usingClass.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 usingClass.getDeclaredFields()
, and allows for further customization of the schema using the builderConsumer- Parameters:
type
- Type to reflectively create a schema frombuilderConsumer
- 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
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.
-