Skip to content

Conversation

@dogdie233
Copy link

What cause the issue

In line 94

var namespaceName = syntaxReceiver.YamlStaticContextType?.ContainingNamespace.ContainingNamespace;
write($"namespace {syntaxReceiver.YamlStaticContextType?.GetNamespace() ?? "YamlDotNet.Static"}", true);
write("{", true); indent();
new StaticContextFile(write, indent, unindent, _context).Write(syntaxReceiver);
new StaticObjectFactoryFile(write, indent, unindent, _context).Write(syntaxReceiver);

When the StaticContext to be generated is in the global namespace, syntaxReceiver.YamlStaticContextType?.GetNamespace() returns an empty string instead of null. As a result, the ?? operator does not take effect.

So, the generated code in YamlDotNetAutoGraph.g.cs ends up as follows:

...
namespace 
{
    ...

Since there is no identifier following namespace, the compilation fails.

About unit test case

Due to the stringent conditions required to trigger this issue, I am unsure how to write a unit test. Should I create a new console project to reproduce the issue?

A potential problem

When the auto-generated StaticContext is in the global namespace, other classes, such as StaticObjectFactory, will also be created in the global namespace. This could potentially conflict with other classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant