Skip to content

Commit 7b170a3

Browse files
committed
Fix StackOverflowException of GenerateSerializerCodes with WithNullable and IsRecursive. Issue #121.
This commit removes unnecessary recursion for nullable type. In addition, this commit fixes enum handling bugs for serializer generation.
1 parent fc147c9 commit 7b170a3

File tree

3 files changed

+108
-74
lines changed

3 files changed

+108
-74
lines changed

src/MsgPack/Serialization/CodeDomSerializers/CodeDomSerializerBuilder`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected override CodeDomConstruct MakeCharLiteral( CodeDomContext context, cha
320320

321321
protected override CodeDomConstruct MakeEnumLiteral( CodeDomContext context, Type type, object constant )
322322
{
323-
return CodeDomConstruct.Expression( type, new CodePrimitiveExpression( constant ) );
323+
return CodeDomConstruct.Expression( type, new CodeFieldReferenceExpression( new CodeTypeReferenceExpression( type ), constant.ToString() ) );
324324
}
325325

326326
protected override CodeDomConstruct MakeDefaultLiteral( CodeDomContext context, Type type )

src/MsgPack/Serialization/SerializerGenerator.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -448,17 +448,15 @@ public IEnumerable<SerializerCodeGenerationResult> Generate( IEnumerable<Type> t
448448
}
449449
else
450450
{
451-
realTargetTypes = targetTypes;
451+
realTargetTypes =
452+
targetTypes
453+
.Where( t => !SerializationTarget.BuiltInSerializerExists( configuration, t, t.GetCollectionTraits() ) );
452454
}
453455

454456
var generationContext = this.CreateGenerationContext( context, configuration );
455457
var generatorFactory = this.CreateGeneratorFactory();
456458

457-
foreach ( var targetType in
458-
realTargetTypes
459-
.Distinct()
460-
.Where( t => !SerializationTarget.BuiltInSerializerExists( configuration, t, t.GetCollectionTraits() ) )
461-
)
459+
foreach ( var targetType in realTargetTypes.Distinct() )
462460
{
463461
var generator = generatorFactory( targetType );
464462

@@ -468,9 +466,6 @@ public IEnumerable<SerializerCodeGenerationResult> Generate( IEnumerable<Type> t
468466
concreteType = context.DefaultCollectionTypes.GetConcreteType( targetType );
469467
}
470468

471-
#if DEBUG
472-
Contract.Assert( !SerializationTarget.BuiltInSerializerExists( configuration, targetType, targetType.GetCollectionTraits() ) );
473-
#endif // DEBUG
474469
generator.BuildSerializerCode( generationContext, concreteType, null );
475470
}
476471

@@ -485,10 +480,17 @@ private static IEnumerable<Type> ExtractElementTypes( SerializationContext conte
485480
{
486481
yield return type;
487482

488-
// Search dependents recursively.
489-
foreach ( var dependentType in SerializationTarget.Prepare( context, type ).Members.SelectMany( m => ExtractElementTypes( context, configuration, m.Member.GetMemberValueType() ) ) )
483+
// Search dependents recursively if the type is NOT enum.
484+
if ( !type.GetIsEnum() )
490485
{
491-
yield return dependentType;
486+
foreach (
487+
var dependentType in
488+
SerializationTarget.Prepare( context, type )
489+
.Members.SelectMany( m => ExtractElementTypes( context, configuration, m.Member.GetMemberValueType() ) )
490+
)
491+
{
492+
yield return dependentType;
493+
}
492494
}
493495
}
494496

@@ -520,15 +522,7 @@ private static IEnumerable<Type> ExtractElementTypes( SerializationContext conte
520522
if ( configuration.WithNullableSerializers && type.GetIsValueType() && Nullable.GetUnderlyingType( type ) == null )
521523
{
522524
// Retrun nullable companion even if they always have built-in serializers.
523-
524-
var nullableType = typeof( Nullable<> ).MakeGenericType( type );
525-
526-
yield return nullableType;
527-
528-
foreach ( var elementType in ExtractElementTypes( context, configuration, nullableType ) )
529-
{
530-
yield return elementType;
531-
}
525+
yield return typeof( Nullable<> ).MakeGenericType( type );
532526
}
533527
}
534528

0 commit comments

Comments
 (0)