Skip to content

Commit 0529b73

Browse files
authored
Merge pull request #200 from TrackableEntities/fix-handlebars-encoding2
Fix Handlebars Encoding Issue
2 parents 5d85c1e + 31f5ac7 commit 0529b73

10 files changed

+306
-11
lines changed

Diff for: src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>6.0.1</Version>
6+
<Version>6.0.2</Version>
77
<Authors>Tony Sneed</Authors>
88
<Company>Tony Sneed</Company>
99
<Title>Entity Framework Core Scaffolding with Handlebars</Title>
@@ -12,7 +12,7 @@
1212
<PackageProjectUrl>https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars</PackageProjectUrl>
1313
<PackageIcon>icon.png</PackageIcon>
1414
<PackageTags>scaffolding reverse-engineer entity-framework-core handlebars</PackageTags>
15-
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.1</PackageReleaseNotes>
15+
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v6.0.2</PackageReleaseNotes>
1616
<LangVersion>latest</LangVersion>
1717
<IncludeSource>true</IncludeSource>
1818
<SignAssembly>true</SignAssembly>
@@ -27,7 +27,7 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Handlebars.Net" Version="2.0.9" />
30+
<PackageReference Include="Handlebars.Net" Version="2.0.10" />
3131
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" />
3232
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.0" />
3333
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0" />

Diff for: src/EntityFrameworkCore.Scaffolding.Handlebars/ServiceCollectionExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.EntityFrameworkCore.Scaffolding;
99
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
1010
using Microsoft.Extensions.DependencyInjection;
11+
using HandlebarsLib = HandlebarsDotNet.Handlebars;
1112

1213
// ReSharper disable once CheckNamespace
1314
namespace Microsoft.EntityFrameworkCore.Design
@@ -62,6 +63,7 @@ public static IServiceCollection AddHandlebarsScaffolding(this IServiceCollectio
6263
public static IServiceCollection AddHandlebarsScaffolding(this IServiceCollection services,
6364
Action<HandlebarsScaffoldingOptions> configureOptions)
6465
{
66+
HandlebarsLib.Configuration.NoEscape = true;
6567
var scaffoldingOptions = new HandlebarsScaffoldingOptions();
6668
if (configureOptions == null)
6769
configureOptions = options => options.ReverseEngineerOptions = ReverseEngineerOptions.DbContextAndEntities;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{> imports}}
2+
3+
namespace {{namespace}}
4+
{
5+
{{#if custom-comment}}
6+
/// <summary>
7+
{{custom-comment}}
8+
/// </summary>
9+
{{/if}}
10+
{{#each class-annotations}}
11+
{{{class-annotation}}}
12+
{{/each}}
13+
public partial class {{class}} : {{base-class}}
14+
{
15+
{{{> constructor}}}
16+
{{{> properties}}}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{#if lines}}
2+
public {{class}}()
3+
{
4+
{{#each lines}}
5+
{{property-name}} = new HashSet<{{property-type}}>();
6+
{{/each}}
7+
}
8+
9+
{{/if}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
{{#if use-data-annotations}}
4+
using System.ComponentModel.DataAnnotations;
5+
using System.ComponentModel.DataAnnotations.Schema;
6+
using Microsoft.EntityFrameworkCore;
7+
{{/if}}
8+
{{#each imports}}
9+
using {{import}};
10+
{{/each}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{{#each properties}}
2+
{{#if property-comment}}
3+
4+
/// <summary>
5+
{{property-comment}}
6+
/// </summary>
7+
{{/if}}
8+
{{#each property-annotations}}
9+
{{{property-annotation}}}
10+
{{/each}}
11+
public {{property-type}} {{property-name}} { get; set; }{{#if nullable-reference-types }}{{#unless property-isnullable}} = null!;{{/unless}}{{/if}}
12+
{{/each}}
13+
{{#if nav-properties}}
14+
15+
{{#each nav-properties}}
16+
{{#each nav-property-annotations}}
17+
{{{nav-property-annotation}}}
18+
{{/each}}
19+
{{#if nav-property-collection}}
20+
public virtual ICollection<{{nav-property-type}}> {{nav-property-name}} { get; set; }
21+
{{else}}
22+
public virtual {{nav-property-type}} {{nav-property-name}} { get; set; }{{#if nullable-reference-types}}{{#unless nav-property-isnullable}} = null!;{{/unless}}{{/if}}
23+
{{/if}}
24+
{{/each}}
25+
{{/if}}

Diff for: test/Scaffolding.Handlebars.Tests/ExpectedEntities.cs

+127
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,60 @@ public partial class ProductRenamed
104104
";
105105
}
106106

107+
private static class ExpectedEntitiesNoEncoding
108+
{
109+
public const string CategoryClass =
110+
@"using System;
111+
using System.Collections.Generic;
112+
113+
namespace FakeNamespace
114+
{
115+
/// <summary>
116+
/// 产品
117+
/// </summary>
118+
public partial class Category : Entity<int>
119+
{
120+
public Category()
121+
{
122+
Products = new HashSet<Product>();
123+
}
124+
125+
public int CategoryId { get; set; }
126+
127+
/// <summary>
128+
/// The name of a category
129+
/// </summary>
130+
public string CategoryName { get; set; }
131+
132+
public virtual ICollection<Product> Products { get; set; }
133+
}
134+
}
135+
";
136+
137+
public const string ProductClass =
138+
@"using System;
139+
using System.Collections.Generic;
140+
141+
namespace FakeNamespace
142+
{
143+
/// <summary>
144+
/// 产品
145+
/// </summary>
146+
public partial class Product : Entity<int>
147+
{
148+
public int ProductId { get; set; }
149+
public string ProductName { get; set; }
150+
public decimal? UnitPrice { get; set; }
151+
public bool Discontinued { get; set; }
152+
public byte[] RowVersion { get; set; }
153+
public int? CategoryId { get; set; }
154+
155+
public virtual Category Category { get; set; }
156+
}
157+
}
158+
";
159+
}
160+
107161
private static class ExpectedEntitiesWithAnnotations
108162
{
109163
public const string CategoryClass =
@@ -248,6 +302,79 @@ public partial class ProductRenamed
248302
";
249303
}
250304

305+
private static class ExpectedEntitiesWithAnnotationsNoEncoding
306+
{
307+
public const string CategoryClass =
308+
@"using System;
309+
using System.Collections.Generic;
310+
using System.ComponentModel.DataAnnotations;
311+
using System.ComponentModel.DataAnnotations.Schema;
312+
using Microsoft.EntityFrameworkCore;
313+
314+
namespace FakeNamespace
315+
{
316+
/// <summary>
317+
/// 产品
318+
/// </summary>
319+
[Table(""Category"")]
320+
public partial class Category : Entity<int>
321+
{
322+
public Category()
323+
{
324+
Products = new HashSet<Product>();
325+
}
326+
327+
[Key]
328+
public int CategoryId { get; set; }
329+
330+
/// <summary>
331+
/// The name of a category
332+
/// </summary>
333+
[Required]
334+
[StringLength(15)]
335+
public string CategoryName { get; set; }
336+
337+
[InverseProperty(nameof(Product.Category))]
338+
public virtual ICollection<Product> Products { get; set; }
339+
}
340+
}
341+
";
342+
343+
public const string ProductClass =
344+
@"using System;
345+
using System.Collections.Generic;
346+
using System.ComponentModel.DataAnnotations;
347+
using System.ComponentModel.DataAnnotations.Schema;
348+
using Microsoft.EntityFrameworkCore;
349+
350+
namespace FakeNamespace
351+
{
352+
/// <summary>
353+
/// 产品
354+
/// </summary>
355+
[Table(""Product"")]
356+
[Index(nameof(CategoryId), Name = ""IX_Product_CategoryId"")]
357+
public partial class Product : Entity<int>
358+
{
359+
[Key]
360+
public int ProductId { get; set; }
361+
[Required]
362+
[StringLength(40)]
363+
public string ProductName { get; set; }
364+
[Column(TypeName = ""money"")]
365+
public decimal? UnitPrice { get; set; }
366+
public bool Discontinued { get; set; }
367+
public byte[] RowVersion { get; set; }
368+
public int? CategoryId { get; set; }
369+
370+
[ForeignKey(nameof(CategoryId))]
371+
[InverseProperty(""Products"")]
372+
public virtual Category Category { get; set; }
373+
}
374+
}
375+
";
376+
}
377+
251378
private static class ExpectedEntitiesWithNullableNavigation
252379
{
253380
public const string CategoryClass =

Diff for: test/Scaffolding.Handlebars.Tests/Fakes/FakeCSharpTemplateLanguageService.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ namespace Scaffolding.Handlebars.Tests.Fakes
66
{
77
public class FakeCSharpTemplateLanguageService : ITemplateLanguageService
88
{
9+
private readonly string _entityTypeDirectory;
10+
private readonly string _entityTypePartialsDirectory;
11+
12+
public FakeCSharpTemplateLanguageService(bool useAltTemplates)
13+
{
14+
_entityTypeDirectory = useAltTemplates ? "CSharpEntityTypeAlt" : "CodeTemplates/CSharpEntityType";
15+
_entityTypePartialsDirectory = _entityTypeDirectory + "/Partials";
16+
}
17+
918
public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo()
1019
{
1120
var result = new Dictionary<string, TemplateFileInfo>
@@ -62,31 +71,31 @@ public Dictionary<string, TemplateFileInfo> GetEntitiesTemplateFileInfo()
6271
Constants.EntityTypeTemplate,
6372
new TemplateFileInfo
6473
{
65-
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypeDirectory,
74+
RelativeDirectory = _entityTypeDirectory,
6675
FileName = Constants.EntityTypeTemplate + Constants.TemplateExtension
6776
}
6877
},
6978
{
7079
Constants.EntityTypeImportTemplate,
7180
new TemplateFileInfo
7281
{
73-
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
82+
RelativeDirectory = _entityTypePartialsDirectory,
7483
FileName = Constants.EntityTypeImportTemplate + Constants.TemplateExtension
7584
}
7685
},
7786
{
7887
Constants.EntityTypeCtorTemplate,
7988
new TemplateFileInfo
8089
{
81-
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
90+
RelativeDirectory = _entityTypePartialsDirectory,
8291
FileName = Constants.EntityTypeCtorTemplate + Constants.TemplateExtension
8392
}
8493
},
8594
{
8695
Constants.EntityTypePropertyTemplate,
8796
new TemplateFileInfo
8897
{
89-
RelativeDirectory = Constants.CSharpTemplateDirectories.EntityTypePartialsDirectory,
98+
RelativeDirectory = _entityTypePartialsDirectory,
9099
FileName = Constants.EntityTypePropertyTemplate + Constants.TemplateExtension
91100
}
92101
},

0 commit comments

Comments
 (0)