1- using System . Collections ;
1+ using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . ComponentModel ;
45using System . Linq ;
1011using Newtonsoft . Json ;
1112using Newtonsoft . Json . Serialization ;
1213using System . Reflection ;
14+ using JsonApiDotNetCore . Routing ;
1315
1416namespace JsonApiDotNetCore . Services
1517{
1618 public class JsonApiSerializer
1719 {
1820 private readonly JsonApiContext _context ;
1921 private readonly JsonApiModelConfiguration _jsonApiModelConfiguration ;
22+ private string _entityName ;
23+ private Type _entityType ;
2024
2125 public JsonApiSerializer ( JsonApiContext jsonApiContext , JsonApiModelConfiguration configuration )
2226 {
2327 _context = jsonApiContext ;
2428 _jsonApiModelConfiguration = configuration ;
29+ _entityName = GetEntityName ( ) ;
30+ _entityType = GetEntityType ( ) ;
31+ }
32+
33+ private string GetEntityName ( ) {
34+ return ( ! ( _context . Route is RelationalRoute ) ? _context . Route . BaseRouteDefinition . ContextPropertyName
35+ : _jsonApiModelConfiguration . Routes . Single ( r=> r . ModelType == ( ( RelationalRoute ) _context . Route ) . RelationalType ) . ContextPropertyName ) . ToCamelCase ( ) ;
36+ }
37+
38+ private Type GetEntityType ( ) {
39+ return ! ( _context . Route is RelationalRoute ) ? _context . Route . BaseRouteDefinition . ModelType
40+ : ( ( RelationalRoute ) _context . Route ) . RelationalType ;
2541 }
2642
2743 public string ToJsonApiDocument ( object resultValue )
@@ -66,7 +82,7 @@ private JsonApiDatum ResourceToJsonApiDatum(JsonApiContext context, IJsonApiReso
6682 {
6783 return new JsonApiDatum
6884 {
69- Type = context . Route . BaseRouteDefinition . ContextPropertyName . ToCamelCase ( ) ,
85+ Type = _entityName ,
7086 Id = resource . Id ,
7187 Attributes = GetAttributesFromResource ( resource ) ,
7288 Links = GetJsonApiDatumLinks ( context , resource ) ,
@@ -88,21 +104,24 @@ private Dictionary<string, string> GetJsonApiDocumentLinks(JsonApiContext jsonAp
88104 var request = jsonApiContext . HttpContext . Request ;
89105 var route = jsonApiContext . Route ;
90106
91- return DocumentBuilder . BuildSelfLink ( request . Scheme , request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
92- route . BaseRouteDefinition . ContextPropertyName . ToCamelCase ( ) , route . ResourceId ) ;
107+ return new Dictionary < string , string > {
108+ {
109+ "self" , $ "{ request . Scheme } ://{ request . Host } { request . Path } "
110+ }
111+ } ;
93112 }
94113
95114 private Dictionary < string , string > GetJsonApiDatumLinks ( JsonApiContext jsonApiContext , IJsonApiResource resource )
96115 {
97116 return DocumentBuilder . BuildSelfLink ( jsonApiContext . HttpContext . Request . Scheme ,
98117 jsonApiContext . HttpContext . Request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
99- jsonApiContext . Route . BaseRouteDefinition . ContextPropertyName . ToCamelCase ( ) , resource . Id ) ;
118+ _entityName , resource . Id ) ;
100119 }
101120
102121 private Dictionary < string , object > BuildRelationshipsObject ( JsonApiContext jsonApiContext , IJsonApiResource resource )
103122 {
104123 var relationships = new Dictionary < string , object > ( ) ;
105- jsonApiContext . Route . BaseModelType . GetProperties ( ) . Where ( propertyInfo => propertyInfo . GetMethod . IsVirtual ) . ToList ( ) . ForEach (
124+ _entityType . GetProperties ( ) . Where ( propertyInfo => propertyInfo . GetMethod . IsVirtual ) . ToList ( ) . ForEach (
106125 virtualProperty =>
107126 {
108127 relationships . Add ( virtualProperty . Name , GetRelationshipLinks ( jsonApiContext , resource , virtualProperty . Name . ToCamelCase ( ) ) ) ;
@@ -114,7 +133,7 @@ private Dictionary<string, string> GetRelationshipLinks(JsonApiContext jsonApiCo
114133 {
115134 return DocumentBuilder . BuildRelationshipLinks ( jsonApiContext . HttpContext . Request . Scheme ,
116135 jsonApiContext . HttpContext . Request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
117- jsonApiContext . Route . BaseRouteDefinition . ContextPropertyName . ToCamelCase ( ) , resource . Id , relationshipName ) ;
136+ _entityName , resource . Id , relationshipName ) ;
118137 }
119138 }
120139}
0 commit comments