use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class UiSpecServiceTest method jsonSchemaArray.
@Test
void jsonSchemaArray() throws Exception {
final Ui payload = service.convert(load("rest-api.json")).toCompletableFuture().get();
final JsonSchema commonConfig = payload.getJsonSchema().getProperties().values().iterator().next().getProperties().entrySet().stream().filter(e -> e.getKey().equals("commonConfig")).findFirst().map(Map.Entry::getValue).orElseThrow(IllegalStateException::new);
assertTrue(commonConfig.getProperties().keySet().stream().anyMatch(p -> p.equals("fields")));
assertTrue(commonConfig.getProperties().keySet().stream().noneMatch(p -> p.equals("fields[]")));
}
use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class UiSpecServiceTest method jsonSchema.
@Test
void jsonSchema() throws Exception {
final Ui payload = service.convert(load("jdbc.json")).toCompletableFuture().get();
final JsonSchema jsonSchema = payload.getJsonSchema();
assertNotNull(jsonSchema);
assertEquals("JDBC Input", jsonSchema.getTitle());
assertEquals("object", jsonSchema.getType());
assertEquals(1, jsonSchema.getProperties().size());
assertProperty(jsonSchema.getProperties().get("configuration"), "object", "Configuration", p -> {
final Map<String, JsonSchema> nestedProperties = p.getProperties();
assertEquals(2, nestedProperties.size());
assertTrue(TreeMap.class.isInstance(nestedProperties));
assertEquals(asList("connection", "query"), new ArrayList<>(nestedProperties.keySet()));
assertProperty(nestedProperties.get("query"), "object", "query", q -> {
final Map<String, JsonSchema> queryNestedProperties = q.getProperties();
assertEquals(2, queryNestedProperties.size());
assertProperty(queryNestedProperties.get("query"), "string", "query", query -> assertNull(query.getProperties()));
assertProperty(queryNestedProperties.get("timeout"), "number", "timeout", timeout -> assertNull(timeout.getProperties()));
});
assertProperty(nestedProperties.get("connection"), "object", "JDBC Connection", q -> {
final Map<String, JsonSchema> queryNestedProperties = q.getProperties();
assertEquals(4, queryNestedProperties.size());
assertProperty(queryNestedProperties.get("password"), "string", "password", pwd -> assertNull(pwd.getProperties()));
assertProperty(queryNestedProperties.get("driver"), "string", "driver", driver -> assertNull(driver.getProperties()));
assertProperty(queryNestedProperties.get("url"), "string", "url", url -> assertNull(url.getProperties()));
assertProperty(queryNestedProperties.get("username"), "string", "username", username -> {
assertNull(username.getProperties());
assertEquals("sa", username.getDefaultValue());
});
});
});
}
use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class JsonSchemaConverterTest method convert.
private Object convert(final String type, final String value) throws Exception {
try (final Jsonb jsonb = JsonbBuilder.create()) {
final JsonSchemaConverter instance = new JsonSchemaConverter(jsonb, new JsonSchema(), emptyList());
final Method convertDefaultValue = JsonSchemaConverter.class.getDeclaredMethod("convertDefaultValue", String.class, String.class);
if (!convertDefaultValue.isAccessible()) {
convertDefaultValue.setAccessible(true);
}
return Optional.class.cast(convertDefaultValue.invoke(instance, type, value)).orElse(null);
}
}
use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class UiSpecService method convert.
private CompletionStage<Ui> convert(final Supplier<String> displayName, final Supplier<String> family, final Supplier<Collection<SimplePropertyDefinition>> properties, final Supplier<Collection<ActionReference>> actions, final Predicate<SimplePropertyDefinition> isRootProperty) {
final Collection<SimplePropertyDefinition> props = properties.get();
final Ui ui = new Ui();
ui.setUiSchema(new ArrayList<>());
ui.setProperties(new HashMap<>());
ui.setJsonSchema(new JsonSchema());
ui.getJsonSchema().setTitle(displayName.get());
ui.getJsonSchema().setType("object");
ui.getJsonSchema().setRequired(props.stream().filter(isRootProperty).filter(p -> new PropertyContext(p).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
final JsonSchemaConverter jsonSchemaConverter = new JsonSchemaConverter(jsonb, ui.getJsonSchema(), props);
final UiSchemaConverter uiSchemaConverter = new UiSchemaConverter(null, family.get(), ui.getUiSchema(), new ArrayList<>(), client, props, actions.get());
final PropertiesConverter propertiesConverter = new PropertiesConverter(jsonb, Map.class.cast(ui.getProperties()), props);
return CompletableFuture.allOf(props.stream().filter(Objects::nonNull).filter(isRootProperty).map(PropertyContext::new).map(CompletableFuture::completedFuture).map(jsonSchemaConverter::convert).map(uiSchemaConverter::convert).map(propertiesConverter::convert).toArray(CompletableFuture[]::new)).thenApply(r -> ui);
}
use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class JsonSchemaConverter method convert.
@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
return cs.thenCompose(context -> {
final JsonSchema jsonSchema = new JsonSchema();
jsonSchema.setTitle(context.getProperty().getDisplayName());
final String type = context.getProperty().getType();
switch(type.toLowerCase(ROOT)) {
case "enum":
return new EnumPropertyConverter(jsonSchema).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
case "array":
return new ArrayPropertyConverter(jsonb, jsonSchema, properties).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
default:
if (context.getProperty().getPath().endsWith("[]")) {
return CompletableFuture.completedFuture(context);
}
jsonSchema.setType(type.toLowerCase(ROOT));
jsonSchema.setRequired(properties.stream().filter(context::isDirectChild).filter(nested -> new PropertyContext(nested).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
return CompletableFuture.completedFuture(context).thenCompose(c -> postHandling(context, jsonSchema, type));
}
});
}
Aggregations