Skip to content

Commit 1f3b5b9

Browse files
committed
content model resolver, types, anonymous object bind, complex tests
1 parent 0331fa8 commit 1f3b5b9

24 files changed

+321
-366
lines changed

src/NetCoreStack.Proxy/Binders/HttpGetContentBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class HttpGetContentBinder : ContentModelBinder
1111

1212
public override void BindContent(ContentModelBindingContext bindingContext)
1313
{
14-
ResolvedContentResult result = bindingContext.GetResolvedContentResult(HttpMethod);
14+
ResolvedContentResult result = bindingContext.GetResolvedContentResult();
1515
List<string> keys = result.Dictionary.Keys.ToList();
1616
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.Args, bindingContext.UriDefinition, result.Dictionary, keys);
1717

src/NetCoreStack.Proxy/Binders/HttpPostContentBinder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public override void BindContent(ContentModelBindingContext bindingContext)
1212
var isMultiPartFormData = bindingContext.IsMultiPartFormData;
1313
if (isMultiPartFormData)
1414
{
15-
ResolvedContentResult result = bindingContext.GetResolvedContentResult(HttpMethod);
15+
ResolvedContentResult result = bindingContext.GetResolvedContentResult();
1616
var content = GetMultipartFormDataContent(result);
1717
bindingContext.ContentResult = ContentModelBindingResult.Success(content);
1818
return;
@@ -22,11 +22,9 @@ public override void BindContent(ContentModelBindingContext bindingContext)
2222
{
2323
bindingContext.ContentResult = ContentModelBindingResult.Success(SerializeToString(bindingContext.Args[0]));
2424
return;
25-
// request.Content = SerializeToString(argsDic.First().Value);
2625
}
2726

2827
bindingContext.ContentResult = ContentModelBindingResult.Success(SerializeToString(bindingContext.Args));
29-
// request.Content = SerializeToString(argsDic);
3028
}
3129
}
3230
}

src/NetCoreStack.Proxy/Binders/HttpPutContentBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class HttpPutContentBinder : BodyContentBinder
1212

1313
public override void BindContent(ContentModelBindingContext bindingContext)
1414
{
15-
ResolvedContentResult result = bindingContext.GetResolvedContentResult(HttpMethod);
15+
ResolvedContentResult result = bindingContext.GetResolvedContentResult();
1616
List<string> keys = result.Dictionary.Keys.ToList();
1717
EnsureTemplate(bindingContext.MethodMarkerTemplate, bindingContext.Args, bindingContext.UriDefinition, result.Dictionary, keys);
1818
if (bindingContext.ArgsLength == 1)

src/NetCoreStack.Proxy/DefaultModelContentResolver.cs

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Net.Http;
4+
using System.Reflection;
55

66
namespace NetCoreStack.Proxy
77
{
@@ -28,14 +28,46 @@ private void SetSimpleEnumerable(string key, Dictionary<string, string> dictiona
2828
}
2929
}
3030

31-
private void TrySetSystemObjectValue()
31+
private void TrySetSystemObjectValue(string key,
32+
PropertyInfo propertyInfo,
33+
string propertyName,
34+
Type containerType,
35+
Dictionary<string, string> dictionary,
36+
object value)
3237
{
38+
var objModelMetadata = new ProxyModelMetadata(propertyInfo,
39+
ProxyModelMetadataIdentity.ForProperty(value.GetType(),
40+
propertyName,
41+
containerType));
42+
43+
44+
if (objModelMetadata.IsSimpleType)
45+
{
46+
dictionary.Add(key, Convert.ToString(value));
47+
return;
48+
}
49+
50+
if (objModelMetadata.IsEnumerableType)
51+
{
52+
if (objModelMetadata.ElementType.IsSimpleType)
53+
{
54+
SetSimpleEnumerable(key, dictionary, value);
55+
}
56+
else
57+
{
58+
TrySetValueInner(key, objModelMetadata, dictionary, value);
59+
}
60+
61+
return;
62+
}
3363

64+
// Anonymous object resolver
65+
ResolveInternal(objModelMetadata, dictionary, value, key);
3466
}
3567

3668
private void TrySetValueInner(string key, ProxyModelMetadata modelMetadata, Dictionary<string, string> dictionary, object value)
3769
{
38-
var count = modelMetadata.ElementType.Properties.Count;
70+
var count = modelMetadata.ElementType.PropertiesCount;
3971
var elementProperties = modelMetadata.ElementType.Properties;
4072
for (int i = 0; i < count; i++)
4173
{
@@ -63,6 +95,7 @@ private void TrySetValue(string prefix, ProxyModelMetadata modelMetadata, Dictio
6395

6496
if (modelMetadata.IsNullableValueType)
6597
{
98+
// recall with prefix
6699
TrySetValue(prefix, modelMetadata.ElementType, dictionary, value);
67100
return;
68101
}
@@ -73,6 +106,11 @@ private void TrySetValue(string prefix, ProxyModelMetadata modelMetadata, Dictio
73106
return;
74107
}
75108

109+
if (modelMetadata.IsFormFile)
110+
{
111+
// TODO Gencebay
112+
}
113+
76114
if (modelMetadata.IsEnumerableType)
77115
{
78116
if (modelMetadata.ElementType.IsSimpleType)
@@ -81,7 +119,11 @@ private void TrySetValue(string prefix, ProxyModelMetadata modelMetadata, Dictio
81119
}
82120
else if(modelMetadata.ElementType.IsSystemObject)
83121
{
84-
122+
TrySetSystemObjectValue(key,
123+
modelMetadata.ElementType.PropertyInfo,
124+
modelMetadata.ElementType.PropertyName,
125+
modelMetadata.ContainerType,
126+
dictionary, value);
85127
}
86128
else
87129
{
@@ -94,52 +136,29 @@ private void TrySetValue(string prefix, ProxyModelMetadata modelMetadata, Dictio
94136
// If typeof(object)
95137
if (modelMetadata.IsSystemObject)
96138
{
97-
// TODO
98-
// TrySetSystemObjectValue
99-
100-
var objModelMetadata = new ProxyModelMetadata(modelMetadata.PropertyInfo,
101-
ProxyModelMetadataIdentity.ForProperty(value.GetType(),
102-
modelMetadata.PropertyName,
103-
modelMetadata.ContainerType));
104-
105-
if (objModelMetadata.IsSimpleType)
106-
{
107-
dictionary.Add(key, Convert.ToString(value));
108-
return;
109-
}
110-
111-
if (objModelMetadata.IsEnumerableType)
112-
{
113-
if (objModelMetadata.ElementType.IsSimpleType)
114-
{
115-
SetSimpleEnumerable(key, dictionary, value);
116-
}
117-
else
118-
{
119-
TrySetValueInner(key, objModelMetadata, dictionary, value);
120-
}
121-
}
139+
TrySetSystemObjectValue(key, modelMetadata.PropertyInfo, modelMetadata.PropertyName, modelMetadata.ContainerType, dictionary, value);
122140
}
123141
}
124142

125143
private void ResolveInternal(ProxyModelMetadata modelMetadata, Dictionary<string, string> dictionary, object value, string prefix = "")
126144
{
127-
var count = modelMetadata.Properties.Count;
145+
var count = modelMetadata.PropertiesCount;
128146
if (count == 0)
129147
{
130148
TrySetValue(prefix, modelMetadata, dictionary, value);
131149
return;
132150
}
133151

134-
for (int i = 0; i < modelMetadata.Properties.Count; i++)
152+
for (int i = 0; i < modelMetadata.PropertiesCount; i++)
135153
{
136154
var metadata = modelMetadata.Properties[i];
137155
if (metadata.ContainerType != null)
138156
{
139157
var parent = prefix;
140158
if (!metadata.IsSimpleType &&
141159
!metadata.IsEnumerableType &&
142-
!metadata.IsNullableValueType)
160+
!metadata.IsNullableValueType &&
161+
!metadata.IsSystemObject)
143162
{
144163
parent = !string.IsNullOrEmpty(prefix) ? $"{prefix}.{metadata.PropertyName}" : metadata.PropertyName;
145164
}
@@ -161,10 +180,7 @@ private void ResolveInternal(ProxyModelMetadata modelMetadata, Dictionary<string
161180
}
162181

163182
// Per request parameter context resolver
164-
public ResolvedContentResult Resolve(HttpMethod httpMethod,
165-
List<ProxyModelMetadata> parameters,
166-
bool isMultiPartFormData,
167-
object[] args)
183+
public ResolvedContentResult Resolve(List<ProxyModelMetadata> parameters, object[] args)
168184
{
169185
var dictionary = new Dictionary<string, string>(StringComparer.Ordinal);
170186
var count = parameters.Count;

src/NetCoreStack.Proxy/DefaultProxyContentStreamProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using NetCoreStack.Proxy.Extensions;
2-
using System;
32
using System.Net.Http;
43
using System.Threading.Tasks;
54

src/NetCoreStack.Proxy/Extensions/ContentModelBindingContextExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace NetCoreStack.Proxy.Extensions
66
{
77
public static class ContentModelBindingContextExtensions
88
{
9-
public static ResolvedContentResult GetResolvedContentResult(this ContentModelBindingContext bindingContext, HttpMethod httpMethod)
9+
public static ResolvedContentResult GetResolvedContentResult(this ContentModelBindingContext bindingContext)
1010
{
11-
return bindingContext.ModelContentResolver.Resolve(httpMethod, bindingContext.Parameters, bindingContext.IsMultiPartFormData, bindingContext.Args);
11+
return bindingContext.ModelContentResolver.Resolve(bindingContext.Parameters, bindingContext.Args);
1212
}
1313

1414
public static void TrySetContent(this ContentModelBindingContext bindingContext, HttpRequestMessage httpRequest)

src/NetCoreStack.Proxy/Extensions/DictionaryExtensions.cs

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ namespace NetCoreStack.Proxy.Extensions
77
{
88
internal static class DictionaryExtensions
99
{
10-
//private static string TypeParser(KeyValuePair<string, object> selector)
11-
//{
12-
// if (selector.Value == null)
13-
// return null;
14-
15-
// if (selector.Value != null)
16-
// {
17-
// if (selector.Value is DateTime)
18-
// {
19-
// return ((DateTime)selector.Value).ToString(CultureInfo.InvariantCulture);
20-
// }
21-
// }
22-
23-
// return selector.Value.ToString();
24-
//}
25-
2610
// Copyright (c) .NET Foundation. All rights reserved.
2711
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
2812
private static string AddQueryString(string uri, IEnumerable<KeyValuePair<string, string>> queryString)
@@ -82,88 +66,6 @@ private static string AddQueryString(string uri, IDictionary<string, string> que
8266
return AddQueryString(uri, (IEnumerable<KeyValuePair<string, string>>)queryString);
8367
}
8468

85-
//private static void AddToDictionary(IDictionary<string, string> source, ProxyModelMetadata metadata, object instance)
86-
//{
87-
// do
88-
// {
89-
// foreach (var modelMetadata in metadata.Properties)
90-
// {
91-
// var prefix = modelMetadata.ContainerType?.Name;
92-
// var propertyName = modelMetadata.PropertyName;
93-
// var key = !string.IsNullOrEmpty(prefix) ? $"{prefix}.{propertyName}" : propertyName;
94-
95-
// if (modelMetadata.IsSimpleType)
96-
// {
97-
// var value = Convert.ToString(instance, CultureInfo.InvariantCulture);
98-
// if (!string.IsNullOrEmpty(value))
99-
// {
100-
// source.Add(key, value);
101-
// }
102-
// }
103-
// }
104-
// }
105-
// while (metadata.Properties.Any());
106-
//}
107-
108-
//internal static void Merge(this IDictionary<string, object> instance, IDictionary<string, object> from, bool replaceExisting)
109-
//{
110-
// foreach (KeyValuePair<string, object> entry in from)
111-
// {
112-
// if (replaceExisting || !instance.ContainsKey(entry.Key))
113-
// {
114-
// instance[entry.Key] = entry.Value;
115-
// }
116-
// }
117-
//}
118-
119-
//internal static void Merge(this IDictionary<string, PropertyContext> instance, IDictionary<string, PropertyContext> from, bool replaceExisting)
120-
//{
121-
// foreach (KeyValuePair<string, PropertyContext> entry in from)
122-
// {
123-
// if (replaceExisting || !instance.ContainsKey(entry.Key))
124-
// {
125-
// instance[entry.Key] = entry.Value;
126-
// }
127-
// }
128-
//}
129-
130-
//internal static void MergeArgs(this IDictionary<string, PropertyContext> dictionary,
131-
// object[] args,
132-
// List<ProxyModelMetadata> parameters,
133-
// bool isMultiPartFormData)
134-
//{
135-
// if (args.Length == 0)
136-
// return;
137-
138-
// if (!isMultiPartFormData)
139-
// {
140-
// for (int i = 0; i < parameters.Count; i++)
141-
// {
142-
// var modelMetadata = parameters[i];
143-
// dictionary.Add(modelMetadata.PropertyName, new PropertyContext { ModelMetadata = modelMetadata, Value = args[i] });
144-
// }
145-
146-
// return;
147-
// }
148-
149-
// // Multipart form data context
150-
// for (int i = 0; i < parameters.Count; i++)
151-
// {
152-
// var modelMetadata = parameters[i];
153-
// foreach (var prop in parameters[i].Properties)
154-
// {
155-
// var parameterContext = new PropertyContext
156-
// {
157-
// ModelMetadata = prop,
158-
// // TODO Gencebay
159-
// // Value = contentTypeInfo.PropertyInfo.GetValue(args[i]),
160-
// };
161-
// // TODO Gencebay
162-
// // dictionary.Add(name, parameterContext);
163-
// }
164-
// }
165-
//}
166-
16769
internal static string ToQueryString(this Uri baseUrl, IDictionary<string, string> dictionary)
16870
{
16971
if (dictionary == null || dictionary.Count == 0)
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using System.Collections.Generic;
2-
using System.Net.Http;
32

43
namespace NetCoreStack.Proxy
54
{
65
public interface IModelContentResolver
76
{
8-
ResolvedContentResult Resolve(HttpMethod httpMethod,
9-
List<ProxyModelMetadata> parameters,
10-
bool isMultiPartFormData,
11-
object[] args);
7+
ResolvedContentResult Resolve(List<ProxyModelMetadata> parameters, object[] args);
128
}
139
}

src/NetCoreStack.Proxy/NetCoreStack.Proxy.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.0.1" />
1212
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
14-
<PackageReference Include="NetCoreStack.Contracts" Version="2.0.2" />
14+
<PackageReference Include="NetCoreStack.Contracts" Version="2.0.3" />
1515
<PackageReference Include="NetCoreStack.DispatchProxyAsync" Version="2.0.2" />
1616
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1717
</ItemGroup>

src/NetCoreStack.Proxy/PropertyContentType.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)