@@ -13,7 +13,7 @@ public class Response(string component, InertiaProps props, string? version, Ine
13
13
: IActionResult
14
14
{
15
15
private IDictionary < string , object > ? _viewData ;
16
-
16
+
17
17
public async Task ExecuteResultAsync ( ActionContext context )
18
18
{
19
19
var page = new InertiaPage
@@ -23,8 +23,9 @@ public async Task ExecuteResultAsync(ActionContext context)
23
23
Url = context . HttpContext . RequestedUri ( ) ,
24
24
Props = await GetFinalProps ( context ) ,
25
25
DeferredProps = GetDeferredProps ( context ) ,
26
+ MergeProps = GetMergeProps ( context )
26
27
} ;
27
-
28
+
28
29
if ( ! context . HttpContext . IsInertiaRequest ( ) )
29
30
{
30
31
var viewData = new ViewDataDictionary ( new EmptyModelMetadataProvider ( ) , context . ModelState )
@@ -45,33 +46,36 @@ public async Task ExecuteResultAsync(ActionContext context)
45
46
context . HttpContext . Response . Headers . Append ( "X-Inertia" , "true" ) ;
46
47
context . HttpContext . Response . Headers . Append ( "Vary" , "Accept" ) ;
47
48
context . HttpContext . Response . StatusCode = 200 ;
48
-
49
- var jsonResult = new JsonResult ( page , jsonSerializerOptions ) ;
49
+
50
+ var jsonResult = new JsonResult ( page , options . JsonSerializerOptions ) ;
50
51
await jsonResult . ExecuteResultAsync ( context ) ;
51
52
}
52
53
}
53
-
54
+
54
55
private async Task < InertiaProps > GetFinalProps ( ActionContext context )
55
56
{
56
- var partials = context . IsInertiaPartialComponent ( component ) ? context . GetPartialData ( ) : null ;
57
+ var isPartial = context . IsInertiaPartialComponent ( component ) ;
58
+ var partials = isPartial ? context . GetPartialData ( ) : [ ] ;
59
+ var excepts = isPartial ? context . GetInertiaExcepts ( ) : [ ] ;
60
+
57
61
var shared = context . HttpContext . Features . Get < InertiaSharedProps > ( ) ;
58
- var flash = context . HttpContext . Features . Get < InertiaFlashMessages > ( )
62
+ var flash = context . HttpContext . Features . Get < InertiaFlashMessages > ( )
59
63
?? InertiaFlashMessages . FromSession ( context . HttpContext ) ;
60
64
var errors = GetErrors ( context ) ;
61
-
62
- var finalProps = await props . ToProcessedProps ( partials ) ;
63
-
65
+
66
+ var finalProps = await props . ToProcessedProps ( isPartial , partials , excepts ) ;
67
+
64
68
finalProps = finalProps
65
69
. Merge ( shared ? . GetData ( ) )
66
70
. AddTimeStamp ( )
67
71
. AddFlash ( flash . GetData ( ) )
68
72
. AddErrors ( errors ) ;
69
-
73
+
70
74
flash . Clear ( false ) ;
71
-
75
+
72
76
return finalProps ;
73
77
}
74
-
78
+
75
79
private Dictionary < string , List < string > > GetDeferredProps ( ActionContext context )
76
80
{
77
81
if ( context . IsInertiaPartialComponent ( component ) )
@@ -95,31 +99,50 @@ private Dictionary<string, List<string>> GetDeferredProps(ActionContext context)
95
99
g => g . Select ( x => x . Key ) . ToList ( )
96
100
) ;
97
101
}
98
-
102
+
103
+ private List < string > GetMergeProps ( ActionContext context )
104
+ {
105
+ var resetData = context . GetInertiaResetData ( ) ;
106
+
107
+ var tmp = new Dictionary < string , string > ( ) ;
108
+
109
+ foreach ( var ( key , value ) in props )
110
+ {
111
+ if ( value is IMergeableProp { Merge : true } && ! resetData . Contains ( key ) )
112
+ tmp [ key ] = key ;
113
+ }
114
+
115
+ // apply json serialization options to dictionary keys before grouping them
116
+ var jsonOptions = options . JsonSerializerOptions as JsonSerializerOptions ;
117
+ tmp = JsonSerializer . Deserialize < Dictionary < string , string > > ( JsonSerializer . Serialize ( tmp , jsonOptions ) , jsonOptions ) ;
118
+
119
+ return tmp ! . Select ( prop => prop . Key ) . ToList ( ) ;
120
+ }
121
+
99
122
private static Dictionary < string , string > GetErrors ( ActionContext context )
100
123
{
101
124
var sessionErrors = context . HttpContext . Session . GetString ( "errors" ) ;
102
125
if ( sessionErrors is not null )
103
126
{
104
127
var errors = JsonSerializer . Deserialize < Dictionary < string , string > > ( sessionErrors ) ;
105
128
context . HttpContext . Session . Remove ( "errors" ) ;
106
-
107
- if ( errors is not null )
129
+
130
+ if ( errors is not null )
108
131
return errors ;
109
132
}
110
-
111
- if ( context . ModelState . IsValid )
133
+
134
+ if ( context . ModelState . IsValid )
112
135
return [ ] ;
113
-
136
+
114
137
return context . ModelState . ToDictionary (
115
138
kv => kv . Key ,
116
139
kv => kv . Value ? . Errors . FirstOrDefault ( ) ? . ErrorMessage ?? ""
117
- ) ;
140
+ ) ;
118
141
}
119
142
120
143
public Response WithViewData ( IDictionary < string , object > viewData )
121
144
{
122
145
_viewData = viewData ;
123
146
return this ;
124
147
}
125
- }
148
+ }
0 commit comments