use of com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator in project syndesis by syndesisio.
the class ExtensionSchemaValidationTest method generateBaseExtensionDefinition.
@Test
@Ignore("Used to generate the initial extension definition")
public void generateBaseExtensionDefinition() throws Exception {
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(OBJECT_MAPPER);
com.fasterxml.jackson.module.jsonSchema.JsonSchema schema = schemaGen.generateSchema(Extension.class);
LOG.info(OBJECT_MAPPER.writeValueAsString(schema));
}
use of com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator in project textdb by TextDB.
the class JsonSchemaHelper method generateJsonSchema.
public static void generateJsonSchema(Class<? extends PredicateBase> predicateClass) throws Exception {
if (!operatorTypeMap.containsKey(predicateClass)) {
throw new TexeraException("predicate class " + predicateClass.toString() + " is not registerd in PredicateBase class");
}
// find the operatorType of the predicate class
String operatorType = operatorTypeMap.get(predicateClass);
// find the operator json schema path by its predicate class
Path operatorSchemaPath = getJsonSchemaPath(predicateClass);
// create the json schema file of the operator
Files.deleteIfExists(operatorSchemaPath);
Files.createFile(operatorSchemaPath);
// generate the json schema
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(DataConstants.defaultObjectMapper);
JsonSchema schema = jsonSchemaGenerator.generateSchema(predicateClass);
ObjectNode schemaNode = objectMapper.readValue(objectMapper.writeValueAsBytes(schema), ObjectNode.class);
LinkedHashSet<JsonNode> propertiesNodes = new LinkedHashSet<>();
searchForEntity(schemaNode, "properties", propertiesNodes);
for (JsonNode propertiesJsonNode : propertiesNodes) {
ObjectNode propertiesNode = (ObjectNode) propertiesJsonNode;
// remove the operatorID from the json schema
propertiesNode.remove("operatorID");
// process each property due to frontend form generator requirements
propertiesNode.fields().forEachRemaining(e -> {
String propertyName = e.getKey();
ObjectNode propertyNode = (ObjectNode) e.getValue();
// add a "title" field to each property
propertyNode.put("title", propertyName);
// if property is an enum, set unique items to true
if (propertiesNode.has("enum")) {
propertyNode.put("uniqueItems", true);
}
});
}
// add required/optional properties to the schema
List<String> requiredProperties = getRequiredProperties(predicateClass);
// because draft v4 doesn't allow it
if (!requiredProperties.isEmpty()) {
schemaNode.set("required", objectMapper.valueToTree(requiredProperties));
}
// add property default values to the schema
Map<String, Object> defaultValues = getPropertyDefaultValues(predicateClass);
for (String property : defaultValues.keySet()) {
ObjectNode propertyNode = (ObjectNode) schemaNode.get("properties").get(property);
propertyNode.set("default", objectMapper.convertValue(defaultValues.get(property), JsonNode.class));
}
// add the additionalMetadataNode
ObjectNode additionalMetadataNode = objectMapper.createObjectNode();
// add additional operator metadata to the additionalMetadataNode
Map<String, Object> operatorMetadata = (Map<String, Object>) predicateClass.getMethod("getOperatorMetadata").invoke(null);
for (String key : operatorMetadata.keySet()) {
additionalMetadataNode.set(key, objectMapper.valueToTree(operatorMetadata.get(key)));
}
// add input and output arity to the schema
additionalMetadataNode.put("numInputPorts", OperatorArityConstants.getFixedInputArity(predicateClass));
additionalMetadataNode.put("numOutputPorts", OperatorArityConstants.getFixedOutputArity(predicateClass));
// add advancedOption properties to the schema
List<String> advancedOptionProperties = getAdvancedOptionProperties(predicateClass);
additionalMetadataNode.set("advancedOptions", objectMapper.valueToTree(advancedOptionProperties));
// setup the full metadata node, which contains the schema and additional metadata
ObjectNode fullMetadataNode = objectMapper.createObjectNode();
// add the operator type to the full node
fullMetadataNode.put("operatorType", operatorType);
fullMetadataNode.set("jsonSchema", schemaNode);
fullMetadataNode.set("additionalMetadata", additionalMetadataNode);
Files.write(operatorSchemaPath, objectMapper.writeValueAsBytes(fullMetadataNode));
System.out.println("generating schema of " + operatorType + " completed");
System.out.println(fullMetadataNode);
}
use of com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator in project graylog2-server by Graylog2.
the class Generator method schemaForType.
private Map<String, Object> schemaForType(Type valueType) {
final SchemaFactoryWrapper schemaFactoryWrapper = new SchemaFactoryWrapper() {
@Override
public JsonAnyFormatVisitor expectAnyFormat(JavaType convertedType) {
/*final ObjectSchema s = schemaProvider.objectSchema();
s.putProperty("anyType", schemaProvider.stringSchema());
this.schema = s;
return visitorFactory.anyFormatVisitor(new AnySchema());*/
return super.expectAnyFormat(convertedType);
}
};
final JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(mapper, schemaFactoryWrapper);
try {
final JsonSchema schema = schemaGenerator.generateSchema(mapper.getTypeFactory().constructType(valueType));
final Map<String, Object> schemaMap = mapper.readValue(mapper.writeValueAsBytes(schema), Map.class);
if (schemaMap.containsKey("additional_properties") && !schemaMap.containsKey("properties")) {
schemaMap.put("properties", Collections.emptyMap());
}
if (schemaMap.equals(Collections.singletonMap("type", "any"))) {
return ImmutableMap.of("type", "object", "properties", Collections.emptyMap());
}
return schemaMap;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator in project atlasmap by atlasmap.
the class ActionDetail method setActionSchema.
/**
* Sets the action schema from a field action model class.
* @param clazz field action model class
* @throws JsonMappingException unexpected error
*/
@JsonProperty("actionSchema")
public void setActionSchema(Class<? extends Action> clazz) throws JsonMappingException {
if (clazz == null) {
setActionSchema((ObjectSchema) null);
return;
}
setClassName(clazz.getName());
ObjectMapper mapper = new ObjectMapper().enable(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES);
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
AtlasSchemaFactoryWrapper visitor = new AtlasSchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema schema = visitor.finalSchema();
ObjectSchema objectSchema = schema.asObjectSchema();
// see: https://json-schema.org/understanding-json-schema/reference/generic.html#constant-values
String id = ActionResolver.getInstance().toId(clazz);
objectSchema.setId(id);
AtlasSchemaFactoryWrapper.ExtendedJsonSchema keyField = (AtlasSchemaFactoryWrapper.ExtendedJsonSchema) objectSchema.getProperties().get("@type");
keyField.getMetadata().put("const", id);
setActionSchema(objectSchema);
}
use of com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator in project fabric8 by fabric8io.
the class JsonSchemaLookup method getSchemaForClass.
public String getSchemaForClass(Class<?> clazz) {
LOG.info("Looking up schema for " + clazz.getCanonicalName());
String name = clazz.getName();
try {
ObjectWriter writer = mapper.writer().with(new FourSpacePrettyPrinter());
JsonSchemaGenerator jsg = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = jsg.generateSchema(clazz);
return writer.writeValueAsString(jsonSchema);
} catch (Exception e) {
LOG.log(Level.FINEST, "Failed to generate JSON schema for class " + name, e);
return "";
}
}
Aggregations