Search in sources :

Example 31 with ObjectType

use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.

the class SchemaTypeConversion method convertType.

public static QName convertType(final MetadataType type, ExpressionSupport expressionSupport) {
    final boolean dynamic = acceptsExpressions(expressionSupport);
    final Reference<QName> qName = new Reference<>(null);
    type.accept(new MetadataTypeVisitor() {

        @Override
        public void visitBoolean(BooleanType booleanType) {
            qName.set(dynamic ? EXPRESSION_BOOLEAN : SUBSTITUTABLE_BOOLEAN);
        }

        @Override
        public void visitNumber(NumberType numberType) {
            if (getId(numberType).isPresent()) {
                Class<Number> type = JavaTypeUtils.getType(numberType);
                if (anyOf(type, Integer.class, int.class)) {
                    qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
                } else if (anyOf(type, Double.class, double.class)) {
                    qName.set(dynamic ? EXPRESSION_DOUBLE : SUBSTITUTABLE_DECIMAL);
                } else if (anyOf(type, Long.class, long.class)) {
                    qName.set(dynamic ? EXPRESSION_LONG : SUBSTITUTABLE_LONG);
                } else {
                    qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
                }
            } else {
                if (numberType.getAnnotation(IntAnnotation.class).isPresent()) {
                    qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
                } else {
                    qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
                }
            }
        }

        @Override
        public void visitString(StringType stringType) {
            qName.set(dynamic ? EXPRESSION_STRING : STRING);
        }

        @Override
        public void visitDateTime(DateTimeType dateTimeType) {
            onDate();
        }

        @Override
        public void visitDate(DateType dateType) {
            onDate();
        }

        @Override
        public void visitArrayType(ArrayType arrayType) {
            qName.set(dynamic ? EXPRESSION_LIST : SUBSTITUTABLE_NAME);
        }

        @Override
        public void visitObject(ObjectType objectType) {
            if (isMap(objectType)) {
                qName.set(dynamic ? EXPRESSION_MAP : SUBSTITUTABLE_NAME);
            } else {
                defaultVisit(objectType);
            }
        }

        @Override
        protected void defaultVisit(MetadataType metadataType) {
            qName.set(STRING);
        }

        private void onDate() {
            qName.set(dynamic ? EXPRESSION_DATE_TIME : SUBSTITUTABLE_DATE_TIME);
        }

        private boolean anyOf(Class<Number> type, Class<?>... targets) {
            for (Class<?> target : targets) {
                if (type.equals(target)) {
                    return true;
                }
            }
            return false;
        }
    });
    return qName.get();
}
Also used : StringType(org.mule.metadata.api.model.StringType) QName(javax.xml.namespace.QName) Reference(org.mule.runtime.api.util.Reference) BooleanType(org.mule.metadata.api.model.BooleanType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) NumberType(org.mule.metadata.api.model.NumberType) DateTimeType(org.mule.metadata.api.model.DateTimeType) DateType(org.mule.metadata.api.model.DateType)

Example 32 with ObjectType

use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.

the class InputParametersTypeModelValidator method validateType.

private void validateType(String message, NamedObject namedObject, MetadataType type, ProblemsReporter problems, Set<Class<?>> validatedTypes) {
    ReflectionCache reflectionCache = new ReflectionCache();
    getClassForValidation(type).ifPresent(parameterType -> type.accept(new MetadataTypeVisitor() {

        @Override
        public void visitObject(ObjectType objectType) {
            if (validatedTypes.add(parameterType)) {
                Collection<ObjectFieldType> parameters = objectType.getFields();
                Set<String> fieldsWithGetters = getFieldsWithGetters(parameterType, reflectionCache).stream().map(TypeUtils::getAlias).map(String::toLowerCase).collect(toSet());
                Set<String> parameterWithoutGetters = parameters.stream().filter(p -> {
                    StereotypeTypeAnnotation stereotypes = p.getAnnotation(StereotypeTypeAnnotation.class).orElse(null);
                    return stereotypes != null ? stereotypes.getAllowedStereotypes().isEmpty() : true;
                }).map(f -> f.getKey().getName().getLocalPart()).filter(fieldName -> !fieldsWithGetters.contains(fieldName.toLowerCase())).collect(toSet());
                if (!parameterWithoutGetters.isEmpty()) {
                    problems.addError(new Problem(namedObject, format("%s of type '%s' which contains fields (%s) that doesn't have the corresponding getter methods or getter methods that doesn't correspond to any of the present fields", message, parameterType.getName(), parameterWithoutGetters.stream().collect(joining(", ")))));
                }
            }
        }

        @Override
        public void visitArrayType(ArrayType arrayType) {
            validateType(message, namedObject, arrayType.getType(), problems, validatedTypes);
        }
    }));
}
Also used : Optional.empty(java.util.Optional.empty) StereotypeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.StereotypeTypeAnnotation) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) NamedObject(org.mule.runtime.api.meta.NamedObject) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) HashSet(java.util.HashSet) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ArrayType(org.mule.metadata.api.model.ArrayType) IdempotentExtensionWalker(org.mule.runtime.api.meta.model.util.IdempotentExtensionWalker) Problem(org.mule.runtime.extension.api.loader.Problem) Collectors.toSet(java.util.stream.Collectors.toSet) IntrospectionUtils.getFieldsWithGetters(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFieldsWithGetters) TypeUtils(org.mule.runtime.extension.api.declaration.type.TypeUtils) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) ObjectType(org.mule.metadata.api.model.ObjectType) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) Collection(java.util.Collection) Set(java.util.Set) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) SourceCallbackModel(org.mule.runtime.api.meta.model.source.SourceCallbackModel) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) NameUtils.getComponentModelTypeName(org.mule.runtime.extension.api.util.NameUtils.getComponentModelTypeName) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) StereotypeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.StereotypeTypeAnnotation) Problem(org.mule.runtime.extension.api.loader.Problem) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) TypeUtils(org.mule.runtime.extension.api.declaration.type.TypeUtils) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor)

Example 33 with ObjectType

use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.

the class NullSafeModelValidator method validate.

@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
    ReflectionCache reflectionCache = new ReflectionCache();
    TypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    new ExtensionWalker() {

        @Override
        public void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
            model.getType().accept(new MetadataTypeVisitor() {

                @Override
                public void visitObject(ObjectType objectType) {
                    if (objectType.getMetadataFormat().equals(JAVA) && !isMap(objectType)) {
                        objectType.getAnnotation(TypeIdAnnotation.class).map(TypeIdAnnotation::getValue).ifPresent(typeId -> typeLoader.load(typeId).ifPresent(fieldMetadataType -> objectType.getFields().stream().filter(f -> f.getAnnotation(NullSafeTypeAnnotation.class).isPresent()).forEach(f -> validateField(getLocalPart(f), f, getType(fieldMetadataType), f.getAnnotation(NullSafeTypeAnnotation.class).get()))));
                    }
                }

                private void validateField(String fieldName, ObjectFieldType field, Class<?> declaringClass, NullSafeTypeAnnotation nullSafeTypeAnnotation) {
                    Class<?> nullSafeType = nullSafeTypeAnnotation.getType();
                    Class<?> fieldType = getType(field.getValue());
                    boolean hasDefaultOverride = nullSafeTypeAnnotation.hasDefaultOverride();
                    field.getValue().accept(new BasicTypeMetadataVisitor() {

                        @Override
                        protected void visitBasicType(MetadataType metadataType) {
                            problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                        }

                        @Override
                        public void visitArrayType(ArrayType arrayType) {
                            if (hasDefaultOverride) {
                                problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                            }
                        }

                        @Override
                        public void visitObject(ObjectType objectType) {
                            String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
                            if (!isBlank(requiredFields) && isCompiletime(extensionModel)) {
                                problemsReporter.addError(new Problem(model, format("Class '%s' cannot be used with '@%s' parameter since it contains non optional fields: [%s]", getId(objectType).orElse(""), NullSafe.class.getSimpleName(), requiredFields)));
                            }
                            if (objectType.isOpen()) {
                                if (hasDefaultOverride) {
                                    problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                                }
                                return;
                            }
                            if (hasDefaultOverride && isInstantiable(fieldType, reflectionCache)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                            }
                            if (!isInstantiable(nullSafeType, reflectionCache)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), nullSafeType.getName())));
                            }
                            if (hasDefaultOverride && !fieldType.isAssignableFrom(nullSafeType)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName(), nullSafeType.getName())));
                            }
                        }
                    });
                }
            });
        }
    }.walk(extensionModel);
}
Also used : ExtensionMetadataTypeUtils.getId(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getId) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) ExtensionMetadataTypeUtils.isFlattenedParameterGroup(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isFlattenedParameterGroup) ModelValidationUtils.isCompiletime(org.mule.runtime.module.extension.internal.loader.validation.ModelValidationUtils.isCompiletime) ArrayType(org.mule.metadata.api.model.ArrayType) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) IntrospectionUtils.isInstantiable(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.isInstantiable) Problem(org.mule.runtime.extension.api.loader.Problem) MetadataTypeUtils.getLocalPart(org.mule.metadata.api.utils.MetadataTypeUtils.getLocalPart) TypeIdAnnotation(org.mule.metadata.api.annotation.TypeIdAnnotation) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) TypeLoader(org.mule.metadata.api.TypeLoader) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) MetadataType(org.mule.metadata.api.model.MetadataType) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MetadataType(org.mule.metadata.api.model.MetadataType) TypeLoader(org.mule.metadata.api.TypeLoader) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Problem(org.mule.runtime.extension.api.loader.Problem) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType)

Example 34 with ObjectType

use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.

the class ParametersResolver method resolveParameterGroups.

/**
 * {@inheritDoc}
 */
@Override
public void resolveParameterGroups(ObjectType objectType, DefaultObjectBuilder builder) {
    Class<?> objectClass = getType(objectType);
    objectType.getFields().stream().filter(ExtensionMetadataTypeUtils::isFlattenedParameterGroup).forEach(groupField -> {
        if (!(groupField.getValue() instanceof ObjectType)) {
            return;
        }
        final ObjectType groupType = (ObjectType) groupField.getValue();
        final Field objectField = getField(objectClass, getLocalPart(groupField));
        DefaultObjectBuilder groupBuilder = getParameterGroupObjectBuilder(groupField);
        builder.addPropertyResolver(objectField.getName(), new ObjectBuilderValueResolver<>(groupBuilder, muleContext));
        resolveParameters(groupType, groupBuilder);
        resolveParameterGroups(groupType, groupBuilder);
    });
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) Field(java.lang.reflect.Field) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)

Example 35 with ObjectType

use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.

the class DsqlQueryMetadataResolver method getOutputType.

/**
 * Automatically resolves the output metadata for the {@link DsqlQuery}.
 * <p>
 * The base entity is resolved using the component {@link QueryEntityResolver} and assuming the key of the entity is the DSQL
 * {@link DsqlQuery#getType() type}.
 *
 * @param context {@link MetadataContext} of the MetaData resolution
 * @param query the {@link DsqlQuery} to resolve the output metadata from.
 */
@Override
public MetadataType getOutputType(MetadataContext context, Object query) throws MetadataResolvingException, ConnectionException {
    if (query instanceof DsqlQuery) {
        DsqlQuery dsqlQuery = (DsqlQuery) query;
        MetadataType entityMetadata = entityResolver.getEntityMetadata(context, dsqlQuery.getType().getName());
        BaseTypeBuilder builder = context.getTypeBuilder();
        final List<Field> fields = dsqlQuery.getFields();
        if (fields.size() == 1 && fields.get(0).getName().equals("*")) {
            return entityMetadata;
        }
        entityMetadata.accept(new MetadataTypeVisitor() {

            @Override
            public void visitObject(ObjectType objectType) {
                ObjectTypeBuilder objectTypeBuilder = builder.objectType();
                objectType.getFields().stream().filter(p -> fields.stream().anyMatch(f -> f.getName().equalsIgnoreCase(p.getKey().getName().getLocalPart()))).forEach(p -> {
                    ObjectFieldTypeBuilder field = objectTypeBuilder.addField();
                    field.key(p.getKey().getName());
                    field.value(p.getValue());
                });
            }
        });
        return builder.build();
    } else {
        return nativeOutputResolver.getOutputType(context, query);
    }
}
Also used : ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) ObjectType(org.mule.metadata.api.model.ObjectType) OutputTypeResolver(org.mule.runtime.api.metadata.resolving.OutputTypeResolver) Field(org.mule.runtime.extension.api.dsql.Field) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder) QueryEntityResolver(org.mule.runtime.api.metadata.resolving.QueryEntityResolver) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) Field(org.mule.runtime.extension.api.dsql.Field) ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder)

Aggregations

ObjectType (org.mule.metadata.api.model.ObjectType)44 MetadataType (org.mule.metadata.api.model.MetadataType)32 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)21 ArrayType (org.mule.metadata.api.model.ArrayType)19 Optional (java.util.Optional)17 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)17 Reference (org.mule.runtime.api.util.Reference)17 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)16 List (java.util.List)15 String.format (java.lang.String.format)14 ClassTypeLoader (org.mule.metadata.api.ClassTypeLoader)14 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)14 ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)14 Set (java.util.Set)13 ParameterizedModel (org.mule.runtime.api.meta.model.parameter.ParameterizedModel)13 Map (java.util.Map)12 DslElementSyntax (org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax)12 Collectors.toList (java.util.stream.Collectors.toList)11 HashSet (java.util.HashSet)10 ExtensionsTypeLoaderFactory (org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory)10