use of org.mule.metadata.api.model.ObjectFieldType in project mule by mulesoft.
the class ExtensionDefinitionParser method parseField.
private void parseField(ObjectType type, DslElementSyntax typeDsl, ObjectFieldType objectField, Map<String, ParameterRole> parametersRole) {
final MetadataType fieldType = objectField.getValue();
final String fieldName = objectField.getKey().getName().getLocalPart();
final boolean acceptsReferences = ExtensionMetadataTypeUtils.acceptsReferences(objectField);
final Object defaultValue = getDefaultValue(fieldType).orElse(null);
final ExpressionSupport expressionSupport = getExpressionSupport(objectField);
Optional<DslElementSyntax> fieldDsl = typeDsl.getContainedElement(fieldName);
if (!fieldDsl.isPresent() && !isFlattenedParameterGroup(objectField)) {
return;
}
Optional<String> keyName = getInfrastructureParameterName(fieldType);
if (keyName.isPresent()) {
parseObject(fieldName, keyName.get(), (ObjectType) fieldType, defaultValue, expressionSupport, false, acceptsReferences, fieldDsl.get(), emptySet());
return;
}
final boolean isContent = isContent(parametersRole.getOrDefault(fieldName, BEHAVIOUR));
fieldType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
if (!parseAsContent(isContent, metadataType)) {
parseAttributeParameter(fieldName, fieldName, metadataType, defaultValue, expressionSupport, false, emptySet());
}
}
@Override
public void visitString(StringType stringType) {
if (fieldDsl.get().supportsChildDeclaration()) {
String elementName = fieldDsl.get().getElementName();
addParameter(fieldName, fromChildConfiguration(String.class).withWrapperIdentifier(elementName));
addDefinition(baseDefinitionBuilder.withIdentifier(elementName).withTypeDefinition(fromType(String.class)).withTypeConverter(value -> resolverOf(elementName, stringType, value, defaultValue, expressionSupport, false, emptySet(), acceptsReferences)).build());
} else {
defaultVisit(stringType);
}
}
@Override
public void visitObject(ObjectType objectType) {
if (objectType.isOpen()) {
if (!parseAsContent(isContent, objectType)) {
parseMapParameters(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, fieldDsl.get(), emptySet());
}
return;
}
if (isFlattenedParameterGroup(objectField)) {
dslResolver.resolve(objectType).ifPresent(objectDsl -> objectType.getFields().forEach(field -> parseField(objectType, objectDsl, field, parametersRole)));
return;
}
if (parseAsContent(isContent, objectType)) {
return;
}
DslElementSyntax dsl = fieldDsl.get();
if (!parsingContext.isRegistered(dsl.getElementName(), dsl.getPrefix())) {
parsingContext.registerObjectType(dsl.getElementName(), dsl.getPrefix(), type);
parseObjectParameter(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, acceptsReferences, dsl, emptySet());
} else {
parseObject(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, acceptsReferences, dsl, emptySet());
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (!parseAsContent(isContent, arrayType)) {
parseCollectionParameter(fieldName, fieldName, arrayType, defaultValue, expressionSupport, false, fieldDsl.get(), emptySet());
}
}
private boolean parseAsContent(boolean isContent, MetadataType type) {
if (isContent) {
parseFromTextExpression(fieldName, fieldDsl.get(), () -> value -> resolverOf(fieldName, type, value, defaultValue, expressionSupport, false, emptySet(), false));
return true;
}
return false;
}
});
}
use of org.mule.metadata.api.model.ObjectFieldType in project mule by mulesoft.
the class ObjectTypeSchemaDelegate method registerPojoComplexType.
/**
* Registers the {@link TopLevelComplexType} associated to the given {@link ObjectType} in the current namespace
*
* @param type the {@link ObjectType} that will be represented by the registered {@link ComplexType}
* @param baseType the {@code base} for the {@link ComplexType} {@code extension} declaration
* @param description
* @return a new {@link ComplexType} declaration for the given {@link ObjectType}
*/
private ComplexType registerPojoComplexType(ObjectType type, ObjectType baseType, String description) {
String typeId = getId(type);
if (registeredComplexTypesHolders.get(typeId) != null) {
return registeredComplexTypesHolders.get(typeId).getComplexType();
}
QName base = getComplexTypeBase(type, baseType);
Collection<ObjectFieldType> fields;
if (baseType == null) {
fields = type.getFields();
} else {
fields = type.getFields().stream().filter(field -> !baseType.getFields().stream().anyMatch(other -> other.getKey().getName().getLocalPart().equals(field.getKey().getName().getLocalPart()))).collect(toList());
}
ComplexType complexType = declarePojoAsType(type, base, description, fields);
builder.getSchema().getSimpleTypeOrComplexTypeOrGroup().add(complexType);
return complexType;
}
use of org.mule.metadata.api.model.ObjectFieldType in project mule by mulesoft.
the class IntrospectionUtils method collectRelativeClassesAsString.
/**
* Given a {@link MetadataType} it adds all the {@link Class} that are related from that type. This includes generics of an
* {@link ArrayType}, open restriction of an {@link ObjectType} as well as its fields.
*
* @param type {@link MetadataType} to inspect
* @return {@link Set<Class>>} with the classes reachable from the {@code type}
*/
public static Set<String> collectRelativeClassesAsString(MetadataType type) {
Set<String> relativeClasses = new HashSet<>();
type.accept(new MetadataTypeVisitor() {
@Override
public void visitArrayType(ArrayType arrayType) {
arrayType.getType().accept(this);
}
@Override
public void visitObjectField(ObjectFieldType objectFieldType) {
objectFieldType.getValue().accept(this);
}
@Override
public void visitObject(ObjectType objectType) {
if (objectType.getMetadataFormat() != JAVA) {
return;
}
final String clazz = getId(objectType).orElse(null);
if (clazz == null || relativeClasses.contains(clazz)) {
return;
}
Optional<ClassInformationAnnotation> classInformation = objectType.getAnnotation(ClassInformationAnnotation.class);
classInformation.ifPresent(classInformationAnnotation -> relativeClasses.addAll(classInformationAnnotation.getGenericTypes()));
relativeClasses.add(clazz);
objectType.getFields().forEach(objectFieldType -> objectFieldType.accept(this));
objectType.getOpenRestriction().ifPresent(t -> t.accept(this));
}
@Override
public void visitString(StringType stringType) {
if (stringType.getMetadataFormat() == JAVA && isEnum(stringType)) {
getId(stringType).ifPresent(relativeClasses::add);
}
}
});
return relativeClasses;
}
use of org.mule.metadata.api.model.ObjectFieldType in project mule by mulesoft.
the class NullSafeValueResolverWrapper method of.
/**
* Creates a new instance
*
* @param delegate the {@link ValueResolver} to wrap
* @param type the type of the value this resolver returns
* @param reflectionCache the cache for expensive reflection lookups
* @param muleContext the current {@link MuleContext}
* @param <T> the generic type of the produced values
* @return a new null safe {@link ValueResolver}
* @throws IllegalParameterModelDefinitionException if used on parameters of not supported types
*/
public static <T> ValueResolver<T> of(ValueResolver<T> delegate, MetadataType type, ReflectionCache reflectionCache, MuleContext muleContext, ObjectTypeParametersResolver parametersResolver) {
checkArgument(delegate != null, "delegate cannot be null");
Reference<ValueResolver> wrappedResolver = new Reference<>();
type.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
Class clazz = getType(objectType);
if (isMap(objectType)) {
ValueResolver<?> fallback = MapValueResolver.of(clazz, emptyList(), emptyList(), reflectionCache, muleContext);
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
return;
}
String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
if (!isBlank(requiredFields)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it contains non optional fields: [%s]", clazz.getName(), requiredFields));
}
wrappedResolver.set(delegate);
return;
}
ResolverSet resolverSet = new ResolverSet(muleContext);
for (Field field : getFields(clazz)) {
ValueResolver<?> fieldResolver = null;
ObjectFieldType objectField = objectType.getFieldByName(getAlias(field)).orElse(null);
if (objectField == null) {
continue;
}
Optional<String> defaultValue = getDefaultValue(objectField);
// TODO MULE-13066 Extract ParameterResolver logic into a centralized resolver
if (defaultValue.isPresent()) {
fieldResolver = getFieldDefaultValueValueResolver(objectField, muleContext);
} else if (isFlattenedParameterGroup(objectField)) {
DefaultObjectBuilder groupBuilder = new DefaultObjectBuilder<>(getType(objectField.getValue()));
resolverSet.add(field.getName(), new ObjectBuilderValueResolver<T>(groupBuilder, muleContext));
ObjectType childGroup = (ObjectType) objectField.getValue();
parametersResolver.resolveParameters(childGroup, groupBuilder);
parametersResolver.resolveParameterGroups(childGroup, groupBuilder);
} else {
NullSafe nullSafe = field.getAnnotation(NullSafe.class);
if (nullSafe != null) {
MetadataType nullSafeType;
if (Object.class.equals(nullSafe.defaultImplementingType())) {
nullSafeType = objectField.getValue();
} else {
nullSafeType = new BaseTypeBuilder(JAVA).objectType().with(new TypeIdAnnotation(nullSafe.defaultImplementingType().getName())).build();
}
fieldResolver = NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), nullSafeType, reflectionCache, muleContext, parametersResolver);
}
if (field.getAnnotation(ConfigOverride.class) != null) {
ValueResolver<?> fieldDelegate = fieldResolver != null ? fieldResolver : new StaticValueResolver<>(null);
fieldResolver = ConfigOverrideValueResolverWrapper.of(fieldDelegate, field.getName(), reflectionCache, muleContext);
}
}
if (fieldResolver != null) {
resolverSet.add(field.getName(), fieldResolver);
}
}
ObjectBuilder<T> objectBuilder = new DefaultResolverSetBasedObjectBuilder<T>(clazz, resolverSet);
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, new ObjectBuilderValueResolver(objectBuilder, muleContext), muleContext));
}
@Override
public void visitArrayType(ArrayType arrayType) {
Class collectionClass = getType(arrayType);
ValueResolver<?> fallback = CollectionValueResolver.of(collectionClass, emptyList());
wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
}
@Override
protected void defaultVisit(MetadataType metadataType) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it is of a simple type", getType(metadataType).getName()));
}
wrappedResolver.set(delegate);
}
});
return wrappedResolver.get();
}
use of org.mule.metadata.api.model.ObjectFieldType in project mule by mulesoft.
the class DsqlQueryMetadataResolverTestCase method getTrimmedOutputMetadata.
@Test
public void getTrimmedOutputMetadata() throws MetadataResolvingException, ConnectionException {
DsqlQuery dsqlQuery = dsqlParser.parse("dsql:SELECT id FROM Circle WHERE (diameter < 18)");
MetadataType outputMetadata = getQueryMetadataResolver().getOutputType(context, dsqlQuery);
ObjectType type = getAndAssertTypeOf(outputMetadata);
assertThat(type.getFields(), hasSize(1));
ObjectFieldType onlyField = type.getFields().iterator().next();
assertThat(onlyField.getValue(), is(instanceOf(NumberType.class)));
assertThat(onlyField.getKey().getName().getLocalPart(), is("id"));
}
Aggregations