Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 1287e4c

Browse files
committed
Fix [] covnert to string errpr
1 parent 3a0c87f commit 1287e4c

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static T CreateCopy<T>(this T from)
9696
if (typeof(T).IsValueType)
9797
return (T)ChangeValueType(from, typeof(T));
9898

99-
if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
99+
if (typeof(IEnumerable).IsAssignableFrom(typeof(T)) && typeof(T) != typeof(string))
100100
{
101101
var listResult = TranslateListWithElements.TryTranslateCollections(from.GetType(), typeof(T), from);
102102
return (T)listResult;
@@ -140,6 +140,9 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
140140
if (from is ReadOnlyMemory<char> rom)
141141
return TypeSerializer.DeserializeFromSpan(toType, rom.Span);
142142

143+
if (toType == typeof(string))
144+
return from.ToJsv();
145+
143146
if (typeof(IEnumerable).IsAssignableFrom(toType))
144147
{
145148
var listResult = TryConvertCollections(fromType, toType, from);
@@ -962,7 +965,7 @@ public static GetMemberDelegate CreateTypeConverter(Type fromType, Type toType)
962965
if (underlyingToType.IsIntegerType())
963966
return fromValue => Convert.ChangeType(fromValue, underlyingToType, null);
964967
}
965-
else if (typeof(IEnumerable).IsAssignableFrom(fromType))
968+
else if (typeof(IEnumerable).IsAssignableFrom(fromType) && underlyingToType != typeof(string))
966969
{
967970
return fromValue => AutoMappingUtils.TryConvertCollections(fromType, underlyingToType, fromValue);
968971
}
@@ -976,6 +979,8 @@ public static GetMemberDelegate CreateTypeConverter(Type fromType, Type toType)
976979
{
977980
if (fromValue == null)
978981
return fromValue;
982+
if (toType == typeof(string))
983+
return fromValue.ToJsv();
979984

980985
var toValue = toType.CreateInstance();
981986
toValue.PopulateWith(fromValue);

src/ServiceStack.Text/TranslateListWithElements.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ private static Dictionary<Type, ConvertInstanceDelegate> TranslateICollectionCac
2727

2828
public static object TranslateToGenericICollectionCache(object from, Type toInstanceOfType, Type elementType)
2929
{
30-
ConvertInstanceDelegate translateToFn;
31-
if (TranslateICollectionCache.TryGetValue(toInstanceOfType, out translateToFn))
30+
if (TranslateICollectionCache.TryGetValue(toInstanceOfType, out var translateToFn))
3231
return translateToFn(from, toInstanceOfType);
3332

3433
var genericType = typeof(TranslateListWithElements<>).MakeGenericType(elementType);
@@ -39,8 +38,9 @@ public static object TranslateToGenericICollectionCache(object from, Type toInst
3938
do
4039
{
4140
snapshot = TranslateICollectionCache;
42-
newCache = new Dictionary<Type, ConvertInstanceDelegate>(TranslateICollectionCache);
43-
newCache[elementType] = translateToFn;
41+
newCache = new Dictionary<Type, ConvertInstanceDelegate>(TranslateICollectionCache) {
42+
[elementType] = translateToFn
43+
};
4444

4545
} while (!ReferenceEquals(
4646
Interlocked.CompareExchange(ref TranslateICollectionCache, newCache, snapshot), snapshot));
@@ -55,8 +55,7 @@ public static object TranslateToConvertibleGenericICollectionCache(
5555
object from, Type toInstanceOfType, Type fromElementType)
5656
{
5757
var typeKey = new ConvertibleTypeKey(toInstanceOfType, fromElementType);
58-
ConvertInstanceDelegate translateToFn;
59-
if (TranslateConvertibleICollectionCache.TryGetValue(typeKey, out translateToFn)) return translateToFn(from, toInstanceOfType);
58+
if (TranslateConvertibleICollectionCache.TryGetValue(typeKey, out var translateToFn)) return translateToFn(from, toInstanceOfType);
6059

6160
var toElementType = toInstanceOfType.FirstGenericType().GetGenericArguments()[0];
6261
var genericType = typeof(TranslateListWithConvertibleElements<,>).MakeGenericType(fromElementType, toElementType);
@@ -67,8 +66,9 @@ public static object TranslateToConvertibleGenericICollectionCache(
6766
do
6867
{
6968
snapshot = TranslateConvertibleICollectionCache;
70-
newCache = new Dictionary<ConvertibleTypeKey, ConvertInstanceDelegate>(TranslateConvertibleICollectionCache);
71-
newCache[typeKey] = translateToFn;
69+
newCache = new Dictionary<ConvertibleTypeKey, ConvertInstanceDelegate>(TranslateConvertibleICollectionCache) {
70+
[typeKey] = translateToFn
71+
};
7272

7373
} while (!ReferenceEquals(
7474
Interlocked.CompareExchange(ref TranslateConvertibleICollectionCache, newCache, snapshot), snapshot));
@@ -109,23 +109,23 @@ public static object TryTranslateCollections(Type fromPropertyType, Type toPrope
109109
public class ConvertibleTypeKey
110110
{
111111
public Type ToInstanceType { get; set; }
112-
public Type FromElemenetType { get; set; }
112+
public Type FromElementType { get; set; }
113113

114114
public ConvertibleTypeKey()
115115
{
116116
}
117117

118-
public ConvertibleTypeKey(Type toInstanceType, Type fromElemenetType)
118+
public ConvertibleTypeKey(Type toInstanceType, Type fromElementType)
119119
{
120120
ToInstanceType = toInstanceType;
121-
FromElemenetType = fromElemenetType;
121+
FromElementType = fromElementType;
122122
}
123123

124124
public bool Equals(ConvertibleTypeKey other)
125125
{
126126
if (ReferenceEquals(null, other)) return false;
127127
if (ReferenceEquals(this, other)) return true;
128-
return Equals(other.ToInstanceType, ToInstanceType) && Equals(other.FromElemenetType, FromElemenetType);
128+
return other.ToInstanceType == ToInstanceType && other.FromElementType == FromElementType;
129129
}
130130

131131
public override bool Equals(object obj)
@@ -141,7 +141,7 @@ public override int GetHashCode()
141141
unchecked
142142
{
143143
return ((ToInstanceType != null ? ToInstanceType.GetHashCode() : 0) * 397)
144-
^ (FromElemenetType != null ? FromElemenetType.GetHashCode() : 0);
144+
^ (FromElementType != null ? FromElementType.GetHashCode() : 0);
145145
}
146146
}
147147
}

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,5 +873,11 @@ public void Translate_Between_Models_of_nullables_and_different_types()
873873
ModelWithFieldsOfDifferentTypesAsNullables.AssertIsEqual(toObj, fromObj);
874874
}
875875

876+
[Test]
877+
public void Can_convert_to_array_to_string()
878+
{
879+
Assert.That(new []{ new ViewModel { Public = "A"} }.ConvertTo<string>(), Is.Not.Empty);
880+
Assert.That(new []{ DayOfWeek.Monday }.ConvertTo<string>(), Is.Not.Empty);
881+
}
876882
}
877883
}

0 commit comments

Comments
 (0)