Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to use customRuleFactory #1613

Closed
AdamPurnomo opened this issue May 15, 2024 · 1 comment
Closed

Failed to use customRuleFactory #1613

AdamPurnomo opened this issue May 15, 2024 · 1 comment

Comments

@AdamPurnomo
Copy link

I am trying to write a custom rule factor where it would wrap the type into a field wrapper. This field wrapper will be used to distinguish the serialization of the following object

User user1 = new User().withName("Adam");

String user1Serialized = OBJECT_MAPPER.writeToString(user1); // -> { "name": "Adam" }

User user2 = new User().withName("Adam").withEmail(null);

String user2Serialized = OBJECT_MAPPER.writeToString(user2); // -> { "name": "Adam", "email": null }

The implementation of field wrapper is as follow

@JsonSerialize(using = FieldWrapperSerializer.class)
public class FieldWrapper<T> {
    private T value;
    private boolean explicitlySet = false;

    public FieldWrapper() {}

    public FieldWrapper(T value) {
        this.value = value;
        this.explicitlySet = true;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
        this.explicitlySet = true;
    }

    public boolean isExplicitlySet() {
        return explicitlySet;
    }
}


public class FieldWrapperSerializer extends JsonSerializer<FieldWrapper<?>> {
    @Override
    public void serialize(FieldWrapper<?> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value.isExplicitlySet()) {
            gen.writeObject(value.getValue());
        }
    }
}

I also implement why own custom type rule and rule factor

public class NullTypeRule extends TypeRule {

    public NullTypeRule(RuleFactory ruleFactory) {
        super(ruleFactory);
    }

    @Override
    public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer jClassContainer, Schema schema) {
        JType fieldType = super.apply(nodeName, node, parent, jClassContainer, schema);
        return fieldType.owner().ref(FieldWrapper.class).narrow(fieldType);
    }
}



public class NullRuleFactory extends RuleFactory {
    @Override
    public Rule<JClassContainer, JType> getTypeRule() {
        return new NullTypeRule(this);
    }
}

In my build.gradle

jsonSchema2Pojo {
    source = files("${sourceSets.main.output.resourcesDir}/com/myproject/schema")
    targetPackage = 'com.myproject.generated'
    customRuleFactory = 'com.myproject.NullRuleFactory'
    generateBuilders = true
    useLongIntegers = true
}

I got the following error

Caused by: java.lang.ClassNotFoundException: com.myproject.NullRuleFactory. Can I get help on this? Thank you.

@unkish
Copy link
Collaborator

unkish commented May 17, 2024

Hi

java.lang.ClassNotFoundException indicates that the cause for issue is that indicated class is not accessible during jsonschema2pojo plugin execution phase.

One could:

  1. publish artifact with com.myproject.NullRuleFactory + related classes and add it to jsonschema2pojo plugin dependencies
  2. use approach similar to jsonSchema2Pojo customAnnotator not working #1488 (comment)
  3. use some other approach

@unkish unkish closed this as completed May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants