Search in sources :

Example 6 with JsonSchema

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);
    });
}
Also used : Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) PropertyConverter(org.talend.sdk.component.form.internal.converter.PropertyConverter) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) Jsonb(javax.json.bind.Jsonb) ROOT(java.util.Locale.ROOT) AllArgsConstructor(lombok.AllArgsConstructor) CompletableFuture(java.util.concurrent.CompletableFuture) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) JsonSchemaConverter(org.talend.sdk.component.form.internal.converter.impl.JsonSchemaConverter) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema)

Example 7 with JsonSchema

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;
    });
}
Also used : UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema) Collectors.toList(java.util.stream.Collectors.toList) CompletionStage(java.util.concurrent.CompletionStage) ActionReference(org.talend.sdk.component.server.front.model.ActionReference) SimplePropertyDefinition(org.talend.sdk.component.server.front.model.SimplePropertyDefinition) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) PropertyContext(org.talend.sdk.component.form.internal.converter.PropertyContext) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) UiSchema(org.talend.sdk.component.form.model.uischema.UiSchema)

Example 8 with JsonSchema

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));
    }
}
Also used : Jsonb(javax.json.bind.Jsonb) JsonSchema(org.talend.sdk.component.form.model.jsonschema.JsonSchema) Test(org.junit.jupiter.api.Test)

Aggregations

JsonSchema (org.talend.sdk.component.form.model.jsonschema.JsonSchema)8 Jsonb (javax.json.bind.Jsonb)6 Collection (java.util.Collection)5 CompletionStage (java.util.concurrent.CompletionStage)5 HashMap (java.util.HashMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 PropertyContext (org.talend.sdk.component.form.internal.converter.PropertyContext)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 Collectors.toList (java.util.stream.Collectors.toList)3 Collectors.toSet (java.util.stream.Collectors.toSet)3 Test (org.junit.jupiter.api.Test)3 Ui (org.talend.sdk.component.form.model.Ui)3 SimplePropertyDefinition (org.talend.sdk.component.server.front.model.SimplePropertyDefinition)3 Arrays.asList (java.util.Arrays.asList)2 Collections.emptyList (java.util.Collections.emptyList)2 Comparator (java.util.Comparator)2 Iterator (java.util.Iterator)2