use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class ArrayPropertyConverter method convert.
@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
return cs.thenCompose(context -> {
jsonSchema.setType(context.getProperty().getType().toLowerCase(ROOT));
final String prefix = context.getProperty().getPath() + "[]";
final List<SimplePropertyDefinition> arrayElements = properties.stream().filter(child -> child.getPath().startsWith(prefix)).collect(toList());
if (arrayElements.stream().anyMatch(e -> e.getPath().startsWith(prefix + '.'))) {
// complex object
final JsonSchema items = new JsonSchema();
items.setType("object");
items.setProperties(new HashMap<>());
jsonSchema.setItems(items);
return CompletableFuture.allOf(arrayElements.stream().map(PropertyContext::new).map(CompletableFuture::completedFuture).map(e -> new JsonSchemaConverter(jsonb, items, emptyList()).convert(e)).toArray(CompletableFuture[]::new)).thenApply(done -> context);
} else if (!arrayElements.isEmpty()) {
// primitive
final String type = arrayElements.get(0).getType();
final JsonSchema item = new JsonSchema();
item.setTitle(jsonSchema.getTitle());
item.setType("enum".equalsIgnoreCase(type) ? "string" : type.toLowerCase(ROOT));
jsonSchema.setItems(item);
}
return CompletableFuture.completedFuture(context);
});
}
use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class DataListWidgetConverter method convert.
@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
return cs.thenApply(context -> {
final UiSchema schema = newUiSchema(context);
schema.setWidget("datalist");
final JsonSchema jsonSchema = new JsonSchema();
jsonSchema.setType("string");
schema.setSchema(jsonSchema);
if (context.getProperty().getValidation().getEnumValues() != null) {
schema.setTitleMap(context.getProperty().getProposalDisplayNames() != null ? context.getProperty().getProposalDisplayNames().entrySet().stream().map(v -> {
final UiSchema.NameValue nameValue = new UiSchema.NameValue();
nameValue.setName(v.getKey());
nameValue.setValue(v.getValue());
return nameValue;
}).collect(toList()) : context.getProperty().getValidation().getEnumValues().stream().sorted().map(v -> {
final UiSchema.NameValue nameValue = new UiSchema.NameValue();
nameValue.setName(v);
nameValue.setValue(v);
return nameValue;
}).collect(toList()));
jsonSchema.setEnumValues(context.getProperty().getValidation().getEnumValues());
} else {
schema.setTitleMap(emptyList());
jsonSchema.setEnumValues(emptyList());
}
return context;
});
}
use of org.talend.sdk.component.form.model.jsonschema.JsonSchema in project component-runtime by Talend.
the class JsonSchemaConverterTest method ensurePrimitiveSerialization.
@Test
void ensurePrimitiveSerialization() throws Exception {
try (final Jsonb jsonb = JsonbBuilder.create()) {
final JsonSchema schema = new JsonSchema();
schema.setDefaultValue(5);
assertEquals("{\"default\":5}", jsonb.toJson(schema));
schema.setDefaultValue("yes");
assertEquals("{\"default\":\"yes\"}", jsonb.toJson(schema));
schema.setDefaultValue(asList("a", "b"));
assertEquals("{\"default\":[\"a\",\"b\"]}", jsonb.toJson(schema));
final Model model = new Model();
model.id = "1";
schema.setDefaultValue(model);
assertEquals("{\"default\":{\"id\":\"1\"}}", jsonb.toJson(schema));
schema.setDefaultValue(singletonList(model));
assertEquals("{\"default\":[{\"id\":\"1\"}]}", jsonb.toJson(schema));
}
}
Aggregations