use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class ValidationMetaProviderTest method testNotNullDisablesNullablity.
@Test
public void testNotNullDisablesNullablity() {
setup(true);
MetaResourceBase meta = resourceMetaProvider.getMeta(Task.class);
MetaAttribute attr = meta.getAttribute("name");
Assert.assertFalse(attr.isNullable());
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class TSMetaDataObjectTransformation method generateResourceFields.
private static void generateResourceFields(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject meta) {
TSInterfaceType attributesType = new TSInterfaceType();
attributesType.setName(ATTRIBUTES_CLASS_NAME);
attributesType.setExported(true);
TSInterfaceType relationshipsType = new TSInterfaceType();
relationshipsType.setName(RELATIONSHIPS_CLASS_NAME);
relationshipsType.setExported(true);
TSIndexSignature relationshipsIndexSignature = new TSIndexSignature();
relationshipsIndexSignature.setKeyType(TSPrimitiveType.STRING);
relationshipsIndexSignature.setValueType(NgrxJsonApiLibrary.RESOURCE_RELATIONSHIP);
relationshipsIndexSignature.setParent(relationshipsType);
relationshipsType.setIndexSignature(relationshipsIndexSignature);
// TODO remo: interface support
MetaKey primaryKey = meta.getPrimaryKey();
for (MetaAttribute attr : meta.getDeclaredAttributes()) {
if (primaryKey != null && primaryKey.getUniqueElement().equals(attr)) {
continue;
}
generateResourceField(attr, context, interfaceType, attributesType, relationshipsType);
}
if (!isEmpty(relationshipsType)) {
TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
module.getElements().add(relationshipsType);
relationshipsType.setParent(module);
TSField relationshipsField = new TSField();
relationshipsField.setName("relationships");
relationshipsField.setType(relationshipsType);
relationshipsField.setNullable(true);
interfaceType.getDeclaredMembers().add(relationshipsField);
}
if (!isEmpty(attributesType)) {
TSModule module = TypescriptUtils.getNestedTypeContainer(interfaceType, true);
module.getElements().add(attributesType);
attributesType.setParent(module);
TSField attributesField = new TSField();
attributesField.setName("attributes");
attributesField.setType(attributesType);
attributesField.setNullable(true);
interfaceType.getDeclaredMembers().add(attributesField);
}
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class TSMetaDataObjectTransformation method generateAttributes.
private static void generateAttributes(TSMetaTransformationContext context, TSInterfaceType interfaceType, MetaDataObject element) {
for (MetaAttribute attr : element.getDeclaredAttributes()) {
MetaType elementType = attr.getType().getElementType();
TSField field = new TSField();
field.setName(attr.getName());
field.setType((TSType) context.transform(elementType, TSMetaTransformationOptions.EMPTY));
field.setNullable(true);
interfaceType.getDeclaredMembers().add(field);
}
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class JoinRegistry method getEntityAttribute.
public E getEntityAttribute(MetaAttributePath attrPath) {
MetaAttributePath associationPath = extractAssociationPath(attrPath);
MetaAttributePath primitivePath = attrPath.subPath(associationPath.length());
@SuppressWarnings("unchecked") E from = (E) getOrCreateJoin(associationPath);
if (primitivePath.length() == 0) {
return from;
}
MetaAttributePath currentPath = associationPath;
E criteriaPath = null;
for (MetaAttribute pathElement : primitivePath) {
currentPath = currentPath.concat(pathElement);
E currentCriteriaPath = criteriaPath != null ? criteriaPath : from;
if (pathElement instanceof MetaMapAttribute) {
if (criteriaPath != null)
throw new IllegalStateException("Cannot join to map");
criteriaPath = joinMap(currentCriteriaPath, pathElement);
} else {
// we may need to downcast if attribute is defined on a subtype
MetaDataObject parent = pathElement.getParent().asDataObject();
Class<?> pathType = parent.getImplementationClass();
Class<?> currentType = backend.getJavaElementType(currentCriteriaPath);
boolean isSubType = !pathType.isAssignableFrom(currentType);
if (isSubType) {
currentCriteriaPath = backend.joinSubType(currentCriteriaPath, pathType);
}
criteriaPath = backend.getAttribute(currentCriteriaPath, pathElement);
}
}
return criteriaPath;
}
use of io.crnk.meta.model.MetaAttribute in project crnk-framework by crnk-project.
the class MetaFilteringTest method checkReadOnlyAttribute.
@Test
public void checkReadOnlyAttribute() throws IOException {
RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
ResourceInformation resourceInformation = entry.getResourceInformation();
ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.FORBIDDEN);
Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
QuerySpec querySpec = new QuerySpec(MetaResource.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
ResourceList<MetaResource> list = repository.findAll(querySpec);
Assert.assertEquals(1, list.size());
MetaResource taskMeta = list.get(0);
Assert.assertTrue(taskMeta.hasAttribute("project"));
MetaAttribute projectAttr = taskMeta.getAttribute("project");
Assert.assertFalse(projectAttr.isInsertable());
Assert.assertFalse(projectAttr.isUpdatable());
}
Aggregations