Search in sources :

Example 6 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProviderNamingTest method onNoSerializationConfigShouldSerializeField.

@Test
public void onNoSerializationConfigShouldSerializeField() throws Exception {
    sut = new JacksonResourceFieldInformationProvider();
    sut.init(context);
    BeanAttributeInformation field = beanDesc.getAttribute("namingStrategyTest");
    assertThat(sut.getJsonName(field).isPresent()).isFalse();
}
Also used : BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) Test(org.junit.Test)

Example 7 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class ResourceInformationProviderBase method getResourceFields.

protected List<ResourceField> getResourceFields(Class<?> resourceClass) {
    BeanInformation beanDesc = BeanInformation.get(resourceClass);
    List<String> attributeNames = beanDesc.getAttributeNames();
    List<ResourceField> fields = new ArrayList<>();
    Set<String> relationIdFields = new HashSet<>();
    for (String attributeName : attributeNames) {
        BeanAttributeInformation attributeDesc = beanDesc.getAttribute(attributeName);
        if (!isIgnored(attributeDesc)) {
            InformationBuilder informationBuilder = context.getInformationBuilder();
            InformationBuilder.Field fieldBuilder = informationBuilder.createResourceField();
            buildResourceField(beanDesc, attributeDesc, fieldBuilder);
            fields.add(fieldBuilder.build());
        } else if (attributeDesc.getAnnotation(JsonApiRelationId.class).isPresent()) {
            relationIdFields.add(attributeDesc.getName());
        }
    }
    verifyRelationIdFields(resourceClass, relationIdFields, fields);
    return fields;
}
Also used : BeanInformation(io.crnk.core.engine.information.bean.BeanInformation) InformationBuilder(io.crnk.core.engine.information.InformationBuilder) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ArrayList(java.util.ArrayList) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) HashSet(java.util.HashSet)

Example 8 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class LinksInformationSerializer method serialize.

@Override
public void serialize(LinksInformation value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    BeanInformation info = BeanInformation.get(value.getClass());
    for (String attrName : info.getAttributeNames()) {
        BeanAttributeInformation attribute = info.getAttribute(attrName);
        Object linkValue = attribute.getValue(value);
        if (linkValue != null) {
            gen.writeObjectFieldStart(attrName);
            if (linkValue instanceof String) {
                gen.writeStringField(SerializerUtil.HREF, linkValue.toString());
            } else {
                gen.writeObject(linkValue);
            }
            gen.writeEndObject();
        }
    }
    gen.writeEndObject();
}
Also used : BeanInformation(io.crnk.core.engine.information.bean.BeanInformation) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation)

Example 9 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProviderNamingTest method onFieldWithDefaultJsonPropertyShouldReturnBaseName.

@Test
public void onFieldWithDefaultJsonPropertyShouldReturnBaseName() throws Exception {
    BeanAttributeInformation attr = beanDesc.getAttribute("fieldWithDefaultJsonProperty");
    assertThat(sut.getJsonName(attr).isPresent()).isFalse();
}
Also used : BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) Test(org.junit.Test)

Example 10 with BeanAttributeInformation

use of io.crnk.core.engine.information.bean.BeanAttributeInformation in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProviderNamingTest method onMethodNameWithNamingStrategyShouldReturnModifiedName.

@Test
public void onMethodNameWithNamingStrategyShouldReturnModifiedName() throws Exception {
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    sut = new JacksonResourceFieldInformationProvider();
    sut.init(context);
    Method method = TestClass.class.getDeclaredMethod("getAccessorField");
    String name = sut.getName(method).get();
    assertThat(name).isEqualTo("accessor_field");
    BeanAttributeInformation attr = beanDesc.getAttribute("accessorField");
    assertThat(sut.getJsonName(attr).get()).isEqualTo("accessor_field");
}
Also used : Method(java.lang.reflect.Method) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation) Test(org.junit.Test)

Aggregations

BeanAttributeInformation (io.crnk.core.engine.information.bean.BeanAttributeInformation)13 Test (org.junit.Test)9 BeanInformation (io.crnk.core.engine.information.bean.BeanInformation)3 Method (java.lang.reflect.Method)2 InformationBuilder (io.crnk.core.engine.information.InformationBuilder)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 JsonApiRelation (io.crnk.core.resource.annotations.JsonApiRelation)1 SerializeType (io.crnk.core.resource.annotations.SerializeType)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1