antonmihaylov / OpenXmlTemplates

Word .docx templating system that is designer (no scripting tags) and server-friendly (no word installation required)
GNU Lesser General Public License v3.0
89 stars 25 forks source link

Tweaks to let templating continue when variable source has a null object #17

Closed dgwaldo closed 3 years ago

dgwaldo commented 3 years ago

We have many complex templates. One thing that we were struggling with was that our json objects sometimes have null properties.

The schema might look like this...

{  
    "employee": {  
        "name":       "sonoo",   
        "salary":      56000,   
        "married":    true  
    }  
} 

Or, like this...

{  
    "employee": null
}  

Our template would always contain the variable value. However, as the code is currently you'd always end up with an exception. "The variable "x" can't be processed. Most likely a nested structure that is referenced is not a dictionary or the variable identifier was entered incorrectly"

This was undesirable because templates needed to stay fixed, contain the templated path, but not error out. ThrowIfNotFound was unhelpful because everything was being caught be the first if statement.

With this modification ThrowIfNotFound now works as expected.

antonmihaylov commented 3 years ago

Makes sense, the schema should be flexible. Thank you for your contribution!

antonmihaylov commented 3 years ago

Published the update to nuget (version 2.0.2). Should be available soon https://www.nuget.org/packages/OpenXMLTemplates/2.0.2