boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

Expose annotation collides with ArangoDBs velocity pack Expose #382

Closed konsultaner closed 6 years ago

konsultaner commented 6 years ago

I use boon in a project that also uses arangoDB. My models are stored to the database where I hide fields with

@Expose(serialize = false)
public String myField

boon serializes this model to send it to another client. The problem now is that I actually use the Expose annotation from velocity pack that has the same structure as the boon one. Boon does not type check the annotation. Thats why it does not serialize myField eventhough it is not ment to be.

konsultaner commented 6 years ago

I solved the problem by extending velocy packs VPackJdk8Module to also support another annotation. It may help others:

package de.konsultaner.velocypack;

import com.arangodb.velocypack.VPackAnnotationFieldFilter;
import com.arangodb.velocypack.VPackSetupContext;

public class VPackJdk8Module extends com.arangodb.velocypack.module.jdk8.VPackJdk8Module {
    @Override
    public <C extends VPackSetupContext<C>> void setup(C context) {
        super.setup(context);
        context.annotationFieldFilter(VExpose.class, new VPackAnnotationFieldFilter<VExpose>() {
            @Override
            public boolean serialize(final VExpose annotation) {
                return annotation.serialize();
            }

            @Override
            public boolean deserialize(final VExpose annotation) {
                return annotation.deserialize();
            }
        });
    }
}

and

package de.konsultaner.velocypack;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface VExpose {
    boolean serialize() default true;
    boolean deserialize() default true;
}

I can now use it like this:

@VExpose(serialize = false)
public String myField