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

Can not work properly with Contexts without empty constructor #131

Open
igorbga opened this issue Nov 22, 2023 · 0 comments
Open

Can not work properly with Contexts without empty constructor #131

igorbga opened this issue Nov 22, 2023 · 0 comments

Comments

@igorbga
Copy link

igorbga commented Nov 22, 2023

Well, to be honest I think that this might be more of a misunderstanding on my part than a real bug, but I can get no clarification by reading the documentation. Hope you can point me in the right direction.

I have wrapped docx-stamper in a Spring Boot service that receives a DOCX template an a JSON context.

My service can receive "any" JSON as template model so I can not just model a simple predefined POJO for it.

So I want to use Jackson's JsonNode as a context and use it'ss methods in order to traverse the context and get the data.

On my first attempts I just passed the JsonNode as the context and if seemed to work, well at least it did for basic replacmeentes like ${at("/property_in_json").asText()}.

But then I needed a custom function in order to be able to dump my whole model in the document for debugging purposes so I checked the example in the README and made my custom function.

As soon as I did this everything stopped working.

After some code debugging I saw that ProxyBuilder.build just returns the provided context if no custom function is registered but does some kind of Proxy mangling in other cases. And that's the part that is not working. I think it doesn't work because JSonNode itself is an abstract class and does not have an empty constructor or something like that.

As a second (or fifth to be honest) attempt I defined my own context class that basically is an static proxy over JsonNode with the addition of my custom method. Something like this:

public class Model {
    private JsonNode json;

    public Model(JsonNode json) {
        this.json = json;
    }

    // This is my custom function
    public String dumpModel() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
        } catch (JsonProcessingException e) {
            throw new DocxStamperException("Could not dump model.", e);
        }
    }


    public int size() {
        return this.json.size();
    }

    public boolean isEmpty() {
        return this.json.isEmpty();
    };

   //  continues with many more methods that just forward to this.json...
}

This also seemed to work. Well at least basic expressions work and so does "dumpModel" function, but once I try to use "repeatDocPart" it all falls apart. I'm back to the error were the proxy builder tries to build a new instance of my context and fails due to the lack of an empty constructor.

Obviously I can add an empty constructor to my context class and that makes the ProxyBuilder succeed, but the built instance does not have the reference to the JsonNode so is completely useless.

I don't understand very well why the repeat function is trying to recreate a new context; I suspect it has something to do with the fact that apart from the repeting element you must have access to the global context, or at least the global functions inside the repeated part or something like that.

But in essence everything points me to the suspicion that this is not a bug but a huge misunderstanding on my part.

Any guidance in how I should approach this thant what are the known limitations of context object will be much appreciated.

Thanks

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

1 participant