Skip to content

Commit e1b7f24

Browse files
committed
Eliminate source generator build warnings
1 parent ebe1f70 commit e1b7f24

1 file changed

Lines changed: 90 additions & 53 deletions

File tree

src/SharpYaml.SourceGenerator/YamlSerializerContextGenerator.cs

Lines changed: 90 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ private static string GenerateContextSource(ContextModel model, ImmutableArray<I
574574
{
575575
var builder = new StringBuilder();
576576
var propertyNamingPolicy = ResolveJsonNamingPolicy(model.SourceGenerationOptions.PropertyNamingPolicy);
577+
// Generated code must compile warning-free. Fix the generator logic instead of suppressing warnings in emitted sources.
577578
builder.AppendLine("// <auto-generated />");
578579
builder.AppendLine("#nullable enable annotations");
579580
builder.AppendLine();
@@ -4270,116 +4271,152 @@ private static void EmitReadObjectMemberValueWithObjectCreationHandling(
42704271
string readOnlyFallbackStatement)
42714272
{
42724273
var memberTypeName = member.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
4273-
var effectiveHandlingExpression = member.ObjectCreationHandling is not null
4274-
? "global::System.Text.Json.Serialization.JsonObjectCreationHandling." + member.ObjectCreationHandling
4275-
: "preferredObjectCreationHandling";
42764274
var explicitPopulate = string.Equals(member.ObjectCreationHandling, "Populate", StringComparison.Ordinal);
4275+
var explicitReplace = string.Equals(member.ObjectCreationHandling, "Replace", StringComparison.Ordinal);
42774276
var canAssign = IsWritableMember(member.Symbol);
42784277
var canPopulateValueType = !member.Type.IsValueType || canAssign;
42794278

4280-
builder.Append(" if (").Append(effectiveHandlingExpression)
4281-
.AppendLine(" == global::System.Text.Json.Serialization.JsonObjectCreationHandling.Populate)");
4279+
if (explicitPopulate)
4280+
{
4281+
EmitPopulateObjectMemberValue(builder, member, memberTypeName, declaringTypeName, indexByType, readOnlyFallbackStatement, canAssign, canPopulateValueType, explicitPopulate: true, indent: " ");
4282+
return;
4283+
}
4284+
4285+
if (explicitReplace)
4286+
{
4287+
EmitReplaceObjectMemberValue(builder, member, indexByType, readOnlyFallbackStatement, canAssign, indent: " ");
4288+
return;
4289+
}
4290+
4291+
builder.AppendLine(" if (preferredObjectCreationHandling == global::System.Text.Json.Serialization.JsonObjectCreationHandling.Populate)");
42824292
builder.AppendLine(" {");
4283-
builder.AppendLine(" if (reader.TokenType == global::SharpYaml.Serialization.YamlTokenType.Scalar && global::SharpYaml.Serialization.YamlScalar.IsNull(reader))");
4284-
builder.AppendLine(" {");
4293+
EmitPopulateObjectMemberValue(builder, member, memberTypeName, declaringTypeName, indexByType, readOnlyFallbackStatement, canAssign, canPopulateValueType, explicitPopulate: false, indent: " ");
4294+
builder.AppendLine(" }");
4295+
builder.AppendLine(" else");
4296+
builder.AppendLine(" {");
4297+
EmitReplaceObjectMemberValue(builder, member, indexByType, readOnlyFallbackStatement, canAssign, indent: " ");
4298+
builder.AppendLine(" }");
4299+
}
4300+
4301+
private static void EmitPopulateObjectMemberValue(
4302+
StringBuilder builder,
4303+
MemberModel member,
4304+
string memberTypeName,
4305+
string declaringTypeName,
4306+
Dictionary<ITypeSymbol, int> indexByType,
4307+
string readOnlyFallbackStatement,
4308+
bool canAssign,
4309+
bool canPopulateValueType,
4310+
bool explicitPopulate,
4311+
string indent)
4312+
{
4313+
builder.Append(indent).AppendLine("if (reader.TokenType == global::SharpYaml.Serialization.YamlTokenType.Scalar && global::SharpYaml.Serialization.YamlScalar.IsNull(reader))");
4314+
builder.Append(indent).AppendLine("{");
42854315
if (canAssign)
42864316
{
4287-
builder.Append(" ").Append(member.AssignExpression("default")).AppendLine(";");
4288-
builder.AppendLine(" reader.Read();");
4317+
builder.Append(indent).Append(" ").Append(member.AssignExpression("default")).AppendLine(";");
4318+
builder.Append(indent).AppendLine(" reader.Read();");
42894319
}
42904320
else
42914321
{
4292-
builder.AppendLine(" throw new global::System.InvalidOperationException(\"Unable to assign 'null' to the property or field of type '" + memberTypeName + "'.\");");
4322+
builder.Append(indent).AppendLine(" throw new global::System.InvalidOperationException(\"Unable to assign 'null' to the property or field of type '" + memberTypeName + "'.\");");
42934323
}
42944324

4295-
builder.AppendLine(" }");
4296-
builder.AppendLine(" else");
4297-
builder.AppendLine(" {");
4298-
builder.Append(" var memberConverter = reader.GetConverter(typeof(").Append(memberTypeName).AppendLine("));");
4299-
builder.Append(" var canPopulateMember = memberConverter.CanPopulate(typeof(").Append(memberTypeName).Append("))");
4325+
builder.Append(indent).AppendLine("}");
4326+
builder.Append(indent).AppendLine("else");
4327+
builder.Append(indent).AppendLine("{");
4328+
builder.Append(indent).Append(" var memberConverter = reader.GetConverter(typeof(").Append(memberTypeName).AppendLine("));");
43004329
if (!canPopulateValueType)
43014330
{
4302-
builder.Append(" && false");
4303-
}
4304-
4305-
builder.AppendLine(";");
4306-
builder.AppendLine(" if (!canPopulateMember)");
4307-
builder.AppendLine(" {");
4308-
if (explicitPopulate)
4309-
{
4310-
if (member.Type.IsValueType && !canAssign)
4331+
if (explicitPopulate)
43114332
{
4312-
builder.Append(" throw new global::System.InvalidOperationException(\"Property '").Append(member.Symbol.Name)
4333+
builder.Append(indent).Append(" throw new global::System.InvalidOperationException(\"Property '").Append(member.Symbol.Name)
43134334
.Append("' on type '").Append(declaringTypeName)
43144335
.AppendLine("' is marked with JsonObjectCreationHandling.Populate but is a value type that doesn't have a setter.\");");
43154336
}
43164337
else
43174338
{
4318-
builder.Append(" throw new global::System.InvalidOperationException(\"Property '").Append(member.Symbol.Name)
4319-
.Append("' on type '").Append(declaringTypeName)
4320-
.AppendLine("' is marked with JsonObjectCreationHandling.Populate but it doesn't support populating. This can be either because the property type is immutable or it could use a custom converter.\");");
4339+
builder.Append(indent).Append(" ").Append(readOnlyFallbackStatement).AppendLine();
43214340
}
4341+
4342+
builder.Append(indent).AppendLine("}");
4343+
return;
4344+
}
4345+
4346+
builder.Append(indent).Append(" var canPopulateMember = memberConverter.CanPopulate(typeof(").Append(memberTypeName).AppendLine("));");
4347+
builder.Append(indent).AppendLine(" if (!canPopulateMember)");
4348+
builder.Append(indent).AppendLine(" {");
4349+
if (explicitPopulate)
4350+
{
4351+
builder.Append(indent).Append(" throw new global::System.InvalidOperationException(\"Property '").Append(member.Symbol.Name)
4352+
.Append("' on type '").Append(declaringTypeName)
4353+
.AppendLine("' is marked with JsonObjectCreationHandling.Populate but it doesn't support populating. This can be either because the property type is immutable or it could use a custom converter.\");");
43224354
}
43234355
else if (canAssign)
43244356
{
43254357
EmitReadMemberValueWithCustomConverter(builder, member, indexByType);
43264358
}
43274359
else
43284360
{
4329-
builder.Append(" ").Append(readOnlyFallbackStatement).AppendLine();
4361+
builder.Append(indent).Append(" ").Append(readOnlyFallbackStatement).AppendLine();
43304362
}
43314363

4332-
builder.AppendLine(" }");
4333-
builder.AppendLine(" else");
4334-
builder.AppendLine(" {");
4364+
builder.Append(indent).AppendLine(" }");
4365+
builder.Append(indent).AppendLine(" else");
4366+
builder.Append(indent).AppendLine(" {");
43354367
if (member.Type.IsValueType)
43364368
{
4337-
builder.Append(" var currentValue = instance.").Append(member.Symbol.Name).AppendLine(";");
4338-
builder.Append(" var populatedValue = memberConverter.Populate(reader, typeof(").Append(memberTypeName).AppendLine("), currentValue);");
4369+
builder.Append(indent).Append(" var currentValue = instance.").Append(member.Symbol.Name).AppendLine(";");
4370+
builder.Append(indent).Append(" var populatedValue = memberConverter.Populate(reader, typeof(").Append(memberTypeName).AppendLine("), currentValue);");
43394371
if (canAssign)
43404372
{
4341-
builder.Append(" ").Append(member.AssignExpression("(" + memberTypeName + ")populatedValue!")).AppendLine(";");
4373+
builder.Append(indent).Append(" ").Append(member.AssignExpression("(" + memberTypeName + ")populatedValue!")).AppendLine(";");
43424374
}
43434375
}
43444376
else
43454377
{
4346-
builder.Append(" var currentValue = instance.").Append(member.Symbol.Name).AppendLine(";");
4347-
builder.AppendLine(" if (currentValue is null)");
4348-
builder.AppendLine(" {");
4378+
builder.Append(indent).Append(" var currentValue = instance.").Append(member.Symbol.Name).AppendLine(";");
4379+
builder.Append(indent).AppendLine(" if (currentValue is null)");
4380+
builder.Append(indent).AppendLine(" {");
43494381
if (canAssign)
43504382
{
43514383
EmitReadMemberValueWithCustomConverter(builder, member, indexByType);
43524384
}
43534385
else
43544386
{
4355-
builder.Append(" ").Append(readOnlyFallbackStatement).AppendLine();
4387+
builder.Append(indent).Append(" ").Append(readOnlyFallbackStatement).AppendLine();
43564388
}
4357-
builder.AppendLine(" }");
4358-
builder.AppendLine(" else");
4359-
builder.AppendLine(" {");
4360-
builder.Append(" var populatedValue = memberConverter.Populate(reader, typeof(").Append(memberTypeName).AppendLine("), currentValue);");
4389+
builder.Append(indent).AppendLine(" }");
4390+
builder.Append(indent).AppendLine(" else");
4391+
builder.Append(indent).AppendLine(" {");
4392+
builder.Append(indent).Append(" var populatedValue = memberConverter.Populate(reader, typeof(").Append(memberTypeName).AppendLine("), currentValue);");
43614393
if (canAssign)
43624394
{
4363-
builder.Append(" ").Append(member.AssignExpression("(" + memberTypeName + ")populatedValue!")).AppendLine(";");
4395+
builder.Append(indent).Append(" ").Append(member.AssignExpression("(" + memberTypeName + ")populatedValue!")).AppendLine(";");
43644396
}
4365-
builder.AppendLine(" }");
4397+
builder.Append(indent).AppendLine(" }");
43664398
}
43674399

4368-
builder.AppendLine(" }");
4369-
builder.AppendLine(" }");
4370-
builder.AppendLine(" }");
4371-
builder.AppendLine(" else");
4372-
builder.AppendLine(" {");
4400+
builder.Append(indent).AppendLine(" }");
4401+
builder.Append(indent).AppendLine("}");
4402+
}
4403+
4404+
private static void EmitReplaceObjectMemberValue(
4405+
StringBuilder builder,
4406+
MemberModel member,
4407+
Dictionary<ITypeSymbol, int> indexByType,
4408+
string readOnlyFallbackStatement,
4409+
bool canAssign,
4410+
string indent)
4411+
{
43734412
if (canAssign)
43744413
{
43754414
EmitReadMemberValueWithCustomConverter(builder, member, indexByType);
43764415
}
43774416
else
43784417
{
4379-
builder.Append(" ").Append(readOnlyFallbackStatement).AppendLine();
4418+
builder.Append(indent).Append(readOnlyFallbackStatement).AppendLine();
43804419
}
4381-
4382-
builder.AppendLine(" }");
43834420
}
43844421

43854422
private static void EmitReadMemberValueWithCustomConverter(StringBuilder builder, MemberModel member, Dictionary<ITypeSymbol, int> indexByType)

0 commit comments

Comments
 (0)