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

Fix template caching #1761

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/NJsonSchema.CodeGeneration/DefaultTemplateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ protected virtual string GetEmbeddedLiquidTemplate(string language, string templ
}

/// <exception cref="InvalidOperationException">Could not load template.</exception>
private string GetLiquidTemplate(string language, string template)
private string GetLiquidTemplate(string language, string template, string templateDirectory)
{
if (!template.EndsWith('!') && !string.IsNullOrEmpty(_settings.TemplateDirectory))
if (!template.EndsWith('!') && !string.IsNullOrEmpty(templateDirectory))
{
var templateFilePath = Path.Combine(_settings.TemplateDirectory, template + ".liquid");
var templateFilePath = Path.Combine(templateDirectory, template + ".liquid");
if (File.Exists(templateFilePath))
{
return File.ReadAllText(templateFilePath);
Expand All @@ -111,7 +111,7 @@ private string GetLiquidTemplate(string language, string template)
private sealed class LiquidTemplate : ITemplate
{
internal const string TemplateTagName = "template";
private static readonly ConcurrentDictionary<(string, string), IFluidTemplate> Templates = new ConcurrentDictionary<(string, string), IFluidTemplate>();
private static readonly ConcurrentDictionary<(string, string, string), IFluidTemplate> Templates = new ConcurrentDictionary<(string, string, string), IFluidTemplate>();

static LiquidTemplate()
{
Expand All @@ -132,7 +132,7 @@ static LiquidTemplate()

private readonly string _language;
private readonly string _template;
private readonly Func<string, string, string> _templateContentLoader;
private readonly Func<string, string, string, string> _templateContentLoader;
private readonly object _model;
private readonly string _toolchainVersion;
private readonly CodeGeneratorSettingsBase _settings;
Expand All @@ -147,7 +147,7 @@ static LiquidTemplate()
public LiquidTemplate(
string language,
string template,
Func<string, string, string> templateContentLoader,
Func<string, string, string, string> templateContentLoader,
object model,
string toolchainVersion,
CodeGeneratorSettingsBase settings)
Expand All @@ -167,12 +167,14 @@ public string Render()

try
{
// use language and template name as key for faster lookup than using the content
var key = (_language, _template);
var templateDirectory = _settings.TemplateDirectory ?? string.Empty;
// use language, template name and template directory as key for faster lookup than using the content
// template directory as part of key is requred for processing multiple files since files may use different TemplateDirectory for same language
var key = (_language, _template, templateDirectory);
var template = Templates.GetOrAdd(key, _ =>
{
// our matching expects unix new lines
var templateContent = _templateContentLoader(_language, _template);
var templateContent = _templateContentLoader(_language, _template, templateDirectory);
var data = templateContent.Replace("\r", "");
data = "\n" + data;

Expand Down