use of org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty in project mule by mulesoft.
the class ParameterGroupModelValidatorTestCase method skipEmptyGroupValidationInRuntimeMode.
@Test
public void skipEmptyGroupValidationInRuntimeMode() {
when(extensionModel.getModelProperty(CompileTimeModelProperty.class)).thenReturn(empty());
ParameterGroupDescriptor descriptor = new ParameterGroupDescriptor("name", new TypeWrapper(EmptyGroupPojo.class, typeLoader), null, mock(AnnotatedElement.class), null);
when(groupModel.getModelProperty(ParameterGroupModelProperty.class)).thenReturn(of(new ParameterGroupModelProperty(descriptor)));
validate(extensionModel, validator);
}
use of org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty in project mule by mulesoft.
the class SoapInvokeOperationDeclarer method declareMetadataKeyParameters.
/**
* Given the Invoke Operation Declarer declares all the parameters that the operation has.
*
* @param operation the invoke operation declarer.
*/
private void declareMetadataKeyParameters(OperationDeclarer operation, ClassTypeLoader loader, ReflectionCache reflectionCache) {
TypeWrapper keyType = new TypeWrapper(WebServiceTypeKey.class, loader);
ParameterGroupDeclarer group = operation.onParameterGroup(KEYS_GROUP).withModelProperty(new ParameterGroupModelProperty(new ParameterGroupDescriptor(KEYS_GROUP, keyType)));
StringType stringType = TYPE_BUILDER.stringType().build();
group.withRequiredParameter(SERVICE_PARAM).withModelProperty(new DeclaringMemberModelProperty(getField(WebServiceTypeKey.class, SERVICE_PARAM, reflectionCache).get())).ofType(stringType).withModelProperty(new MetadataKeyPartModelProperty(1)).withLayout(getLayout(1));
group.withRequiredParameter(OPERATION_PARAM).ofType(stringType).withModelProperty(new DeclaringMemberModelProperty(getField(WebServiceTypeKey.class, OPERATION_PARAM, reflectionCache).get())).withModelProperty(new MetadataKeyPartModelProperty(2)).withLayout(getLayout(2));
}
use of org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty in project mule by mulesoft.
the class ImplicitObjectUtils method buildImplicitResolverSet.
/**
* Creates a {@link ResolverSet} based on the default values for the {@link ParameterModel}s in the {@code parametrizedModel}.
* <p>
* If a {@link ParameterModel} returns {@code null} for {@link ParameterModel#getDefaultValue()} then it's ignored
*
* @param model a model holding the {@link ParameterModel}s to consider
* @param muleContext the Mule node.
* @return a {@link ResolverSet}
*/
public static ResolverSet buildImplicitResolverSet(ParameterizedModel model, ReflectionCache reflectionCache, MuleContext muleContext) {
ResolverSet resolverSet = new HashedResolverSet(muleContext);
ParametersResolver parametersResolver = ParametersResolver.fromDefaultValues(model, muleContext, reflectionCache);
for (ParameterGroupModel groupModel : model.getParameterGroupModels()) {
Optional<ParameterGroupDescriptor> descriptor = groupModel.getModelProperty(ParameterGroupModelProperty.class).map(ParameterGroupModelProperty::getDescriptor);
if (descriptor.isPresent() && groupModel.getParameterModels().stream().noneMatch(ParameterModel::isRequired)) {
String groupKey = getContainerName(descriptor.get().getContainer());
resolverSet.add(groupKey, NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), descriptor.get().getMetadataType(), reflectionCache, muleContext, parametersResolver));
} else {
groupModel.getParameterModels().forEach(parameterModel -> {
Object defaultValue = parameterModel.getDefaultValue();
ValueResolver<?> resolver;
if (defaultValue instanceof String) {
resolver = getExpressionBasedValueResolver((String) defaultValue, parameterModel.getType(), muleContext);
} else {
resolver = new StaticValueResolver<>(null);
}
if (parameterModel.getModelProperty(NullSafeModelProperty.class).isPresent()) {
MetadataType metadataType = parameterModel.getModelProperty(NullSafeModelProperty.class).get().defaultType();
resolver = NullSafeValueResolverWrapper.of(resolver, metadataType, reflectionCache, muleContext, parametersResolver);
}
resolverSet.add(parameterModel.getName(), resolver);
});
}
}
return resolverSet;
}
use of org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty in project mule by mulesoft.
the class ObjectBasedParameterValueResolver method getParameterValue.
@Override
public Object getParameterValue(String parameterName) throws ValueResolvingException {
try {
Optional<Field> field = getField(object.getClass(), parameterName, reflectionCache);
if (field.isPresent()) {
return getFieldValue(object, parameterName, reflectionCache);
} else {
for (ParameterGroupModel parameterGroupModel : parameterizedModel.getParameterGroupModels()) {
Optional<ParameterGroupModelProperty> modelProperty = parameterGroupModel.getModelProperty(ParameterGroupModelProperty.class);
if (modelProperty.isPresent()) {
ParameterGroupModelProperty property = modelProperty.get();
Field container = (Field) property.getDescriptor().getContainer();
Object parameterGroup = getFieldValue(object, container.getName(), reflectionCache);
Optional<Field> desiredField = getField(parameterGroup.getClass(), parameterName, reflectionCache);
if (desiredField.isPresent()) {
return getFieldValue(parameterGroup, parameterName, reflectionCache);
}
}
}
}
} catch (Exception e) {
throw new ValueResolvingException("An error occurred trying to obtain the value for the parameter: " + parameterName);
}
throw new ValueResolvingException("Unable to resolve value for the parameter: " + parameterName);
}
use of org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty in project mule by mulesoft.
the class ParameterModelsLoaderDelegate method declaredAsGroup.
private List<ParameterDeclarer> declaredAsGroup(HasParametersDeclarer component, ParameterDeclarationContext declarationContext, ExtensionParameter groupParameter) throws IllegalParameterModelDefinitionException {
ParameterGroup groupAnnotation = groupParameter.getAnnotation(ParameterGroup.class).orElse(null);
if (groupAnnotation == null) {
return emptyList();
}
final String groupName = groupAnnotation.name();
if (DEFAULT_GROUP_NAME.equals(groupName)) {
throw new IllegalParameterModelDefinitionException(format("%s '%s' defines parameter group of name '%s' which is the default one. " + "@%s cannot be used with the default group name", getComponentDeclarationTypeName(((Declarer) component).getDeclaration()), ((NamedDeclaration) ((Declarer) component).getDeclaration()).getName(), groupName, ParameterGroup.class.getSimpleName()));
}
final Type type = groupParameter.getType();
final List<FieldElement> nestedGroups = type.getAnnotatedFields(ParameterGroup.class);
if (!nestedGroups.isEmpty()) {
throw new IllegalParameterModelDefinitionException(format("Class '%s' is used as a @%s but contains fields which also hold that annotation. Nesting groups is not allowed. " + "Offending fields are: [%s]", type.getName(), ParameterGroup.class.getSimpleName(), nestedGroups.stream().map(element -> element.getName()).collect(joining(","))));
}
if (groupParameter.isAnnotatedWith(org.mule.runtime.extension.api.annotation.param.Optional.class)) {
throw new IllegalParameterModelDefinitionException(format("@%s can not be applied alongside with @%s. Affected parameter is [%s].", org.mule.runtime.extension.api.annotation.param.Optional.class.getSimpleName(), ParameterGroup.class.getSimpleName(), groupParameter.getName()));
}
ParameterGroupDeclarer declarer = component.onParameterGroup(groupName);
if (declarer.getDeclaration().getModelProperty(ParameterGroupModelProperty.class).isPresent()) {
throw new IllegalParameterModelDefinitionException(format("Parameter group '%s' has already been declared on %s '%s'", groupName, getComponentDeclarationTypeName(((Declarer) component).getDeclaration()), ((NamedDeclaration) ((Declarer) component).getDeclaration()).getName()));
} else {
declarer.withModelProperty(new ParameterGroupModelProperty(new ParameterGroupDescriptor(groupName, type, groupParameter.getType().asMetadataType(), // TODO: Eliminate dependency to Annotated Elements
groupParameter.getDeclaringElement().orElse(null), groupParameter)));
}
final List<FieldElement> annotatedParameters = type.getAnnotatedFields(Parameter.class);
type.getAnnotation(ExclusiveOptionals.class).ifPresent(annotation -> {
Set<String> optionalParamNames = annotatedParameters.stream().filter(f -> !f.isRequired()).map(WithAlias::getAlias).collect(toSet());
declarer.withExclusiveOptionals(optionalParamNames, annotation.isOneRequired());
});
declarer.withDslInlineRepresentation(groupAnnotation.showInDsl());
groupParameter.getAnnotation(DisplayName.class).ifPresent(displayName -> declarer.withDisplayModel(DisplayModel.builder().displayName(displayName.value()).build()));
parseLayoutAnnotations(groupParameter, LayoutModel.builder()).ifPresent(declarer::withLayout);
declarer.withModelProperty(new ExtensionParameterDescriptorModelProperty(groupParameter));
if (!annotatedParameters.isEmpty()) {
return declare(component, annotatedParameters, declarationContext, declarer);
} else {
return declare(component, getFieldsWithGetters(type), declarationContext, declarer);
}
}
Aggregations