@@ -37,6 +37,13 @@ public void SerializeError(object error, Stream writeStream, JsonWriter writer,
3737 }
3838 }
3939
40+ private class NonStandardIdThing
41+ {
42+ [ JSONAPI . Attributes . UseAsId ]
43+ public Guid Uuid { get ; set ; }
44+ public string Data { get ; set ; }
45+ }
46+
4047 [ TestInitialize ]
4148 public void SetupModels ( )
4249 {
@@ -291,5 +298,86 @@ public void DeserializeExtraRelationshipTest()
291298 // Assert
292299 Assert . AreEqual ( "Jason Hater" , a . Name ) ; // Completed without exceptions and didn't timeout!
293300 }
301+
302+ [ TestMethod ]
303+ [ DeploymentItem ( @"Data\NonStandardIdTest.json" ) ]
304+ public void SerializeNonStandardIdTest ( )
305+ {
306+ var formatter = new JSONAPI . Json . JsonApiFormatter ( new PluralizationService ( ) ) ;
307+ var stream = new MemoryStream ( ) ;
308+ var payload = new List < NonStandardIdThing > {
309+ new NonStandardIdThing { Uuid = new Guid ( "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" ) , Data = "Swap" }
310+ } ;
311+
312+ // Act
313+ formatter . WriteToStreamAsync ( typeof ( List < NonStandardIdThing > ) , payload , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . TransportContext ) null ) ;
314+
315+ // Assert
316+ var expectedJson = File . ReadAllText ( "NonStandardIdTest.json" ) ;
317+ var minifiedExpectedJson = JsonHelpers . MinifyJson ( expectedJson ) ;
318+ var output = System . Text . Encoding . ASCII . GetString ( stream . ToArray ( ) ) ;
319+ output . Should ( ) . Be ( minifiedExpectedJson ) ;
320+ }
321+
322+ #region Non-standard Id attribute tests
323+
324+ [ TestMethod ]
325+ [ DeploymentItem ( @"Data\NonStandardIdTest.json" ) ]
326+ public void DeserializeNonStandardIdTest ( )
327+ {
328+ var formatter = new JSONAPI . Json . JsonApiFormatter ( new PluralizationService ( ) ) ;
329+ var stream = new FileStream ( "NonStandardIdTest.json" , FileMode . Open ) ;
330+
331+ // Act
332+ IList < NonStandardIdThing > things ;
333+ things = ( IList < NonStandardIdThing > ) formatter . ReadFromStreamAsync ( typeof ( NonStandardIdThing ) , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . Http . Formatting . IFormatterLogger ) null ) . Result ;
334+ stream . Close ( ) ;
335+
336+ // Assert
337+ things . Count . Should ( ) . Be ( 1 ) ;
338+ things . First ( ) . Uuid . Should ( ) . Be ( new Guid ( "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" ) ) ;
339+ }
340+
341+ [ TestMethod ]
342+ [ DeploymentItem ( @"Data\NonStandardIdTest.json" ) ]
343+ public void DeserializeNonStandardIdWithIdOnly ( )
344+ {
345+ var formatter = new JSONAPI . Json . JsonApiFormatter ( new PluralizationService ( ) ) ;
346+ string json = File . ReadAllText ( "NonStandardIdTest.json" ) ;
347+ json = Regex . Replace ( json , @"""uuid"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*," , "" ) ; // remove the uuid attribute
348+ var stream = new MemoryStream ( System . Text . Encoding . ASCII . GetBytes ( json ) ) ;
349+
350+ // Act
351+ IList < NonStandardIdThing > things ;
352+ things = ( IList < NonStandardIdThing > ) formatter . ReadFromStreamAsync ( typeof ( NonStandardIdThing ) , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . Http . Formatting . IFormatterLogger ) null ) . Result ;
353+
354+ // Assert
355+ json . Should ( ) . NotContain ( "uuid" , "The \" uuid\" attribute was supposed to be removed, test methodology problem!" ) ;
356+ things . Count . Should ( ) . Be ( 1 ) ;
357+ things . First ( ) . Uuid . Should ( ) . Be ( new Guid ( "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" ) ) ;
358+ }
359+
360+ [ TestMethod ]
361+ [ DeploymentItem ( @"Data\NonStandardIdTest.json" ) ]
362+ public void DeserializeNonStandardIdWithoutId ( )
363+ {
364+ var formatter = new JSONAPI . Json . JsonApiFormatter ( new PluralizationService ( ) ) ;
365+ string json = File . ReadAllText ( "NonStandardIdTest.json" ) ;
366+ json = Regex . Replace ( json , @"""id"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*," , "" ) ; // remove the uuid attribute
367+ var stream = new MemoryStream ( System . Text . Encoding . ASCII . GetBytes ( json ) ) ;
368+
369+ // Act
370+ IList < NonStandardIdThing > things ;
371+ things = ( IList < NonStandardIdThing > ) formatter . ReadFromStreamAsync ( typeof ( NonStandardIdThing ) , stream , ( System . Net . Http . HttpContent ) null , ( System . Net . Http . Formatting . IFormatterLogger ) null ) . Result ;
372+
373+ // Assert
374+ json . Should ( ) . NotContain ( "\" id\" " , "The \" id\" attribute was supposed to be removed, test methodology problem!" ) ;
375+ things . Count . Should ( ) . Be ( 1 ) ;
376+ things . First ( ) . Uuid . Should ( ) . Be ( new Guid ( "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" ) ) ;
377+
378+ }
379+
380+ #endregion
381+
294382 }
295383}
0 commit comments