use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.
the class XmlExtensionLoaderTestCase method testModuleJsonCustomTypes.
@Test
public void testModuleJsonCustomTypes() throws IOException {
String modulePath = "modules/module-json-custom-types.xml";
ExtensionModel extensionModel = getExtensionModelFrom(modulePath);
assertThat(extensionModel.getName(), is("module-json-custom-types"));
assertThat(extensionModel.getConfigurationModels().size(), is(0));
assertThat(extensionModel.getModelProperty(GlobalElementComponentModelModelProperty.class).isPresent(), is(false));
assertThat(extensionModel.getOperationModels().size(), is(8));
Optional<OperationModel> operationModel = extensionModel.getOperationModel("operation-with-custom-types");
assertThat(operationModel.isPresent(), is(true));
final OperationModel operation = operationModel.get();
assertThat(operation.getAllParameterModels().size(), is(3));
final ParameterModel parameterValueModel = operation.getAllParameterModels().get(0);
assertThat(parameterValueModel.getName(), is("value"));
assertThat(operation.getAllParameterModels().get(1).getName(), is(TARGET_PARAMETER_NAME));
assertThat(operation.getAllParameterModels().get(2).getName(), is(TARGET_VALUE_PARAMETER_NAME));
assertThat(parameterValueModel.getType().getMetadataFormat(), is(MetadataFormat.JSON));
assertThat(parameterValueModel.getType(), instanceOf(ObjectType.class));
assertThat(((ObjectType) parameterValueModel.getType()).getFields().size(), is(2));
assertThat(operation.getOutput().getType().getMetadataFormat(), is(MetadataFormat.JSON));
assertThat(operation.getOutput().getType(), instanceOf(ObjectType.class));
assertThat(((ObjectType) operation.getOutput().getType()).getFields().size(), is(3));
Optional<OperationComponentModelModelProperty> modelProperty = operation.getModelProperty(OperationComponentModelModelProperty.class);
assertThat(modelProperty.isPresent(), is(true));
assertThat(modelProperty.get().getBodyComponentModel().getInnerComponents().size(), is(1));
}
use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.
the class IntrospectionUtilsTestCase method assertDictionary.
private void assertDictionary(MetadataType metadataType, Class<?> valueType) {
assertThat(metadataType, is(instanceOf(ObjectType.class)));
ObjectType dictionaryType = (ObjectType) metadataType;
assertType(dictionaryType, Map.class);
assertThat(dictionaryType.getOpenRestriction().isPresent(), is(true));
assertType(dictionaryType.getOpenRestriction().get(), valueType);
}
use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.
the class InfrastructureElementModelDelegate method createReconnectionConfig.
private void createReconnectionConfig(ParameterValue value, ParameterModel parameterModel, DslElementSyntax paramDsl, InternalComponentConfiguration.Builder parentConfig, DslElementModel.Builder parentElement) {
InternalComponentConfiguration.Builder config = InternalComponentConfiguration.builder().withIdentifier(builder().namespace(CORE_PREFIX).name(RECONNECTION_CONFIG_PARAMETER_NAME).build());
final DslElementModel.Builder<Object> elementBuilder = DslElementModel.builder().withModel(parameterModel).withDsl(paramDsl);
((ParameterObjectValue) value).getParameters().forEach((name, fieldValue) -> fieldValue.accept(new ParameterValueVisitor() {
@Override
public void visitSimpleValue(ParameterSimpleValue text) {
config.withParameter(name, text.getValue());
}
@Override
public void visitObjectValue(ParameterObjectValue objectValue) {
if (name.equals(RECONNECTION_STRATEGY_PARAMETER_NAME)) {
createReconnectionStrategy(fieldValue, ((ObjectType) parameterModel.getType()).getFieldByName(RECONNECTION_STRATEGY_PARAMETER_NAME).get(), paramDsl.getContainedElement(RECONNECTION_STRATEGY_PARAMETER_NAME).get(), config, elementBuilder);
}
}
}));
final ComponentConfiguration result = config.build();
parentConfig.withNestedComponent(result);
parentElement.containing(elementBuilder.withConfig(result).build());
}
use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.
the class ConfigurationBasedElementModelFactory method addElementParameter.
private void addElementParameter(Multimap<ComponentIdentifier, ComponentConfiguration> innerComponents, Map<String, String> parameters, DslElementSyntax groupDsl, DslElementModel.Builder<ParameterGroupModel> groupElementBuilder, ParameterModel paramModel) {
groupDsl.getContainedElement(paramModel.getName()).ifPresent(paramDsl -> {
if (isInfrastructure(paramModel)) {
handleInfrastructure(paramModel, paramDsl, innerComponents, parameters, groupElementBuilder);
return;
}
ComponentConfiguration paramComponent = getSingleComponentConfiguration(innerComponents, getIdentifier(paramDsl));
if (paramDsl.isWrapped()) {
resolveWrappedElement(groupElementBuilder, paramModel, paramDsl, paramComponent);
return;
}
String value = paramDsl.supportsAttributeDeclaration() ? parameters.get(paramDsl.getAttributeName()) : null;
Optional<String> defaultValue = getDefaultValue(paramModel);
if (paramComponent != null || !isBlank(value) || defaultValue.isPresent()) {
DslElementModel.Builder<ParameterModel> paramElementBuilder = DslElementModel.<ParameterModel>builder().withModel(paramModel).withDsl(paramDsl);
if (paramComponent != null && !isContent(paramModel)) {
paramElementBuilder.withConfig(paramComponent);
paramModel.getType().accept(new MetadataTypeVisitor() {
@Override
public void visitArrayType(ArrayType arrayType) {
MetadataType itemType = arrayType.getType();
paramDsl.getGeneric(itemType).ifPresent(itemdsl -> {
ComponentIdentifier itemIdentifier = getIdentifier(itemdsl).get();
paramComponent.getNestedComponents().forEach(c -> {
if (c.getIdentifier().equals(itemIdentifier)) {
itemType.accept(getComponentChildVisitor(paramElementBuilder, c, itemType, VALUE_ATTRIBUTE_NAME, itemdsl, defaultValue, new ArrayDeque<>()));
}
});
});
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
populateMapEntries(objectType, paramDsl, paramElementBuilder, paramComponent);
return;
}
populateObjectFields(objectType, paramComponent, paramDsl, paramElementBuilder, new ArrayDeque<>());
}
});
} else {
if (isBlank(value)) {
if (paramComponent != null && paramComponent.getValue().isPresent() && !isBlank(paramComponent.getValue().get())) {
value = paramComponent.getValue().get().trim();
} else if (defaultValue.isPresent()) {
value = defaultValue.get();
paramElementBuilder.isExplicitInDsl(false);
}
}
paramElementBuilder.withValue(value);
}
groupElementBuilder.containing(paramElementBuilder.build());
}
});
}
use of org.mule.metadata.api.model.ObjectType in project mule by mulesoft.
the class IntrospectionUtils method collectRelativeClasses.
/**
* 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
* @param extensionClassLoader extension class loader
* @return {@link Set<Class<?>>} with the classes reachable from the {@code type}
*/
public static Set<Class<?>> collectRelativeClasses(MetadataType type, ClassLoader extensionClassLoader) {
Set<Class<?>> 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 Class<Object> clazz = getType(objectType).orElse(null);
if (clazz == null || relativeClasses.contains(clazz)) {
return;
}
Optional<ClassInformationAnnotation> classInformation = objectType.getAnnotation(ClassInformationAnnotation.class);
if (classInformation.isPresent()) {
classInformation.get().getGenericTypes().forEach(generic -> relativeClasses.add(loadClass(generic, extensionClassLoader)));
}
relativeClasses.add(clazz);
objectType.getFields().stream().forEach(objectFieldType -> objectFieldType.accept(this));
objectType.getOpenRestriction().ifPresent(t -> t.accept(this));
}
@Override
public void visitString(StringType stringType) {
if (stringType.getMetadataFormat() == JAVA && isEnum(stringType)) {
getType(stringType).ifPresent(relativeClasses::add);
}
}
});
return relativeClasses;
}
Aggregations