Skip to content

fix #1031#1033

Merged
EdwardCooke merged 1 commit intoaaubry:masterfrom
dogdie233:fix-1031
Apr 9, 2026
Merged

fix #1031#1033
EdwardCooke merged 1 commit intoaaubry:masterfrom
dogdie233:fix-1031

Conversation

@dogdie233
Copy link
Copy Markdown

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.

@EdwardCooke EdwardCooke merged commit 2867ff5 into aaubry:master Apr 9, 2026
This was referenced Apr 9, 2026
This was referenced Apr 10, 2026
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.

2 participants