Skip to content

Commit d6238c2

Browse files
Init OData.Type on complex and entity types. (#219)
**modified: Templates/CSharp/Model/ComplexType.cs.tt** Add public cstor and ODataType initialization for non-abstract complextypes. **modified: Templates/CSharp/Model/EntityType.cs.tt** Add public cstor and ODataType initialization for non-abstract entitytypes. modified: test/Typewriter.Test/Given_a_valid_metadata_file_to_Typewriter.cs modified: test/Typewriter.Test/Resources/dirtyMetadata.xml
1 parent 29d268b commit d6238c2

File tree

4 files changed

+156
-5
lines changed

4 files changed

+156
-5
lines changed

Templates/CSharp/Model/ComplexType.cs.tt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ namespace <#=complex.Namespace.GetNamespaceName()#>
4343
public <#=classType#> <#=typeDeclaration#>
4444
{
4545
<#
46-
// Generate a constructor to initialize the @odata.type property when this type is not abstract and if this
47-
// type's base is abstract and the base is referenced as the type of a structural property. We need this
48-
// to disambiguate the type of the descendant class being sent.
49-
if (complex.IsBaseAbstractAndReferencedAsPropertyType() && !complex.IsAbstract)
46+
// Generate a constructor to initialize the @odata.type property when this type is not abstract.
47+
if (!complex.IsAbstract)
5048
{
5149
#> /// <summary>
5250
/// Initializes a new instance of the <see cref="<#=complexTypeName#>"/> class.

Templates/CSharp/Model/EntityType.cs.tt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,19 @@ namespace <#=entity.Namespace.GetNamespaceName()#>
7575
}
7676
<#
7777
}
78+
else
79+
{
80+
#>
7881

82+
///<summary>
83+
/// The <#=entityName#> constructor
84+
///</summary>
85+
public <#=cstorTypeDeclaration#>()
86+
{
87+
this.ODataType = "<#=entity.FullName#>";
88+
}
89+
<#
90+
}
7991
foreach(var property in entity.Properties)
8092
{
8193
var propertyType = property.IsTypeNullable() || property.IsCollection()

test/Typewriter.Test/Given_a_valid_metadata_file_to_Typewriter.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Typewriter.Test
66
{
77
/// <summary>
88
/// End to end tests that check the results of running Typewriter from the CLI.
9+
/// IMPORTANT: Typewriter MUST be built before as the templates need to be compiled before running the tests.
910
/// </summary>
1011
[TestClass]
1112
public class Given_a_valid_metadata_file_to_Typewriter
@@ -140,5 +141,144 @@ public void It_generates_dotNet_client_with_commented_out_code_comments()
140141
}
141142
Assert.IsTrue(hasTestString, $"The expected test token string, '{testString}', was not set in the generated test file. We are not correctly handling the \r\n coming from the annotations.");
142143
}
144+
145+
[TestMethod]
146+
public void It_generates_dotNet_odatatype_initialization_for_complextypes()
147+
{
148+
const string outputDirectory = "output";
149+
150+
Options options = new Options()
151+
{
152+
Output = outputDirectory,
153+
Language = "CSharp",
154+
GenerationMode = GenerationMode.Files
155+
};
156+
157+
Generator.GenerateFiles(testMetadata, options);
158+
159+
FileInfo fileInfo = new FileInfo(outputDirectory + generatedOutputUrl + @"\Model\Thumbnail.cs");
160+
Assert.IsTrue(fileInfo.Exists, $"Expected: {fileInfo.FullName}. File was not found.");
161+
162+
IEnumerable<string> lines = File.ReadLines(fileInfo.FullName);
163+
bool hasTestString = false;
164+
string testString = "this.ODataType = \"microsoft.graph.thumbnail\";";
165+
bool hasCstorString = false;
166+
string testCstorString = "public Thumbnail()";
167+
foreach (var line in lines)
168+
{
169+
if (line.Contains(testString))
170+
{
171+
hasTestString = true;
172+
}
173+
if (line.Contains(testCstorString))
174+
{
175+
hasCstorString = true;
176+
}
177+
}
178+
179+
Assert.IsTrue(hasTestString, $"The expected test token string, '{testString}', was not set in the generated test file. We didn't properly generate the setter code in the cstor.");
180+
Assert.IsTrue(hasCstorString, $"The expected test token cstor string, '{testCstorString}', was not set in the generated test file. We didn't properly generate the cstor code.");
181+
}
182+
183+
[TestMethod]
184+
public void It_doesnt_generate_odatatype_initialization_for_abstract_complextypes()
185+
{
186+
const string outputDirectory = "output";
187+
188+
Options options = new Options()
189+
{
190+
Output = outputDirectory,
191+
Language = "CSharp",
192+
GenerationMode = GenerationMode.Files
193+
};
194+
195+
Generator.GenerateFiles(testMetadata, options);
196+
197+
FileInfo fileInfo = new FileInfo(outputDirectory + generatedOutputUrl + @"\Model\EmptyComplexType.cs");
198+
Assert.IsTrue(fileInfo.Exists, $"Expected: {fileInfo.FullName}. File was not found.");
199+
200+
IEnumerable<string> lines = File.ReadLines(fileInfo.FullName);
201+
bool hasTestString = false;
202+
string testString = "this.ODataType = \"microsoft.graph.emptyComplexType\";";
203+
foreach (var line in lines)
204+
{
205+
if (line.Contains(testString))
206+
{
207+
hasTestString = true;
208+
break;
209+
}
210+
}
211+
212+
Assert.IsFalse(hasTestString, $"The unexpected test token string, '{testString}', was set in the generated test file. We incorrectly generated the setter code in the cstor.");
213+
}
214+
215+
216+
[TestMethod]
217+
public void It_generates_dotNet_odatatype_initialization_for_entitytypes()
218+
{
219+
const string outputDirectory = "output";
220+
221+
Options options = new Options()
222+
{
223+
Output = outputDirectory,
224+
Language = "CSharp",
225+
GenerationMode = GenerationMode.Files
226+
};
227+
228+
Generator.GenerateFiles(testMetadata, options);
229+
230+
FileInfo fileInfo = new FileInfo(outputDirectory + generatedOutputUrl + @"\Model\TestType.cs");
231+
Assert.IsTrue(fileInfo.Exists, $"Expected: {fileInfo.FullName}. File was not found.");
232+
233+
IEnumerable<string> lines = File.ReadLines(fileInfo.FullName);
234+
bool hasTestString = false;
235+
string testString = "this.ODataType = \"microsoft.graph.testType\";";
236+
foreach (var line in lines)
237+
{
238+
if (line.Contains(testString))
239+
{
240+
hasTestString = true;
241+
break;
242+
}
243+
}
244+
Assert.IsTrue(hasTestString, $"The expected test token string, '{testString}', was not set in the generated test file. We didn't properly generate the cstor code.");
245+
}
246+
247+
[TestMethod]
248+
public void It_doesnt_generate_odatatype_initialization_for_abstract_entitytypes()
249+
{
250+
const string outputDirectory = "output";
251+
252+
Options options = new Options()
253+
{
254+
Output = outputDirectory,
255+
Language = "CSharp",
256+
GenerationMode = GenerationMode.Files
257+
};
258+
259+
Generator.GenerateFiles(testMetadata, options);
260+
261+
FileInfo fileInfo = new FileInfo(outputDirectory + generatedOutputUrl + @"\Model\Entity.cs");
262+
Assert.IsTrue(fileInfo.Exists, $"Expected: {fileInfo.FullName}. File was not found.");
263+
264+
IEnumerable<string> lines = File.ReadLines(fileInfo.FullName);
265+
bool hasTestString = false;
266+
bool hasTestODataInitString = false;
267+
string testString = "protected internal Entity()";
268+
string testODataInitString = "this.ODataType = \"microsoft.graph.entity\"";
269+
foreach (var line in lines)
270+
{
271+
if (line.Contains(testString))
272+
{
273+
hasTestString = true;
274+
}
275+
if (line.Contains(testODataInitString))
276+
{
277+
hasTestODataInitString = true;
278+
}
279+
}
280+
Assert.IsTrue(hasTestString, $"The expected test token string, '{testString}', was not set in the generated test file. We didn't properly generate the cstor code.");
281+
Assert.IsFalse(hasTestODataInitString, $"The unexpected test token string, '{testODataInitString}', was set in the generated test file. We didn't properly generate the cstor code.");
282+
}
143283
}
144284
}

test/Typewriter.Test/Resources/dirtyMetadata.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
33
<edmx:DataServices>
44
<Schema Namespace="microsoft.graph" xmlns="http://docs.oasis-open.org/odata/ns/edm">
5-
<EntityType Name="entity">
5+
<EntityType Name="entity" Abstract="true">
66
<Key>
77
<PropertyRef Name="id"/>
88
</Key>
@@ -29,6 +29,7 @@
2929
<Property Name="url" Type="Edm.String" />
3030
<Property Name="width" Type="Edm.Int32" />
3131
</ComplexType>
32+
<ComplexType Name="emptyComplexType" Abstract="true"/>
3233
<EntityType Name="onenotePage" HasStream="true" BaseType="microsoft.graph.entity">
3334
<Property Name="content" Type="Edm.Stream" />
3435
<NavigationProperty Name="parentNotebook" Type="microsoft.graph.notebook" ContainsTarget="true" />

0 commit comments

Comments
 (0)