You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
publicclassModel {
privateJsonNodejson;
publicModel(JsonNodejson) {
this.json = json;
}
// This is my custom functionpublicStringdumpModel() {
ObjectMappermapper = newObjectMapper();
try {
returnmapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
} catch (JsonProcessingExceptione) {
thrownewDocxStamperException("Could not dump model.", e);
}
}
publicintsize() {
returnthis.json.size();
}
publicbooleanisEmpty() {
returnthis.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
The text was updated successfully, but these errors were encountered:
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:
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
The text was updated successfully, but these errors were encountered: