-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Support duplicated type names with src gen #58448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steveharter
merged 2 commits into
dotnet:main
from
steveharter:RemoveGetTypeByMetadataName
Sep 7, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
@@ -467,5 +468,68 @@ private void CheckFieldsPropertiesMethods(Type type, string[] expectedFields, st | |
| } | ||
|
|
||
| // TODO: add test guarding against (de)serializing static classes. | ||
|
|
||
| [Fact] | ||
| public void TestMultipleDefinitions() | ||
| { | ||
| // Adding a dependency to an assembly that has internal definitions of public types | ||
| // should not result in a collision and break generation. | ||
| // This verifies the usage of GetBestTypeByMetadataName() instead of GetTypeByMetadataName(). | ||
| var referencedSource = @" | ||
| namespace System.Text.Json.Serialization | ||
| { | ||
| internal class JsonSerializerContext { } | ||
| internal class JsonSerializableAttribute { } | ||
| internal class JsonSourceGenerationOptionsAttribute { } | ||
| }"; | ||
|
|
||
| // Compile the referenced assembly first. | ||
| Compilation referencedCompilation = CompilationHelper.CreateCompilation(referencedSource); | ||
|
|
||
| // Obtain the image of the referenced assembly. | ||
| byte[] referencedImage; | ||
| using (MemoryStream ms = new MemoryStream()) | ||
| { | ||
| var emitResult = referencedCompilation.Emit(ms); | ||
| if (!emitResult.Success) | ||
| { | ||
| throw new InvalidOperationException(); | ||
| } | ||
| referencedImage = ms.ToArray(); | ||
|
Comment on lines
+490
to
+498
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very minor nit: this could have been a helper method.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks; left as-is. Note there are other tests that have the same pattern. |
||
| } | ||
|
|
||
| // Generate the code | ||
| string source = @" | ||
| using System.Text.Json.Serialization; | ||
| namespace HelloWorld | ||
| { | ||
| [JsonSerializable(typeof(HelloWorld.MyType))] | ||
| internal partial class JsonContext : JsonSerializerContext | ||
| { | ||
| } | ||
|
|
||
| public class MyType | ||
| { | ||
| public int MyInt { get; set; } | ||
| } | ||
| }"; | ||
|
|
||
| MetadataReference[] additionalReferences = { MetadataReference.CreateFromImage(referencedImage) }; | ||
| Compilation compilation = CompilationHelper.CreateCompilation(source, additionalReferences); | ||
| JsonSourceGenerator generator = new JsonSourceGenerator(); | ||
|
|
||
| Compilation newCompilation = CompilationHelper.RunGenerators( | ||
| compilation, | ||
| out ImmutableArray<Diagnostic> generatorDiags, generator); | ||
|
|
||
| // Make sure compilation was successful. | ||
| Assert.Empty(generatorDiags.Where(diag => diag.Severity.Equals(DiagnosticSeverity.Error))); | ||
| Assert.Empty(newCompilation.GetDiagnostics().Where(diag => diag.Severity.Equals(DiagnosticSeverity.Error))); | ||
|
|
||
| // Should find the generated type. | ||
| Dictionary<string, Type> types = generator.GetSerializableTypes(); | ||
| Assert.Equal(1, types.Count); | ||
| Assert.Equal("HelloWorld.MyType", types.Keys.First()); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to segregate all the borrowed code into its own file ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I believe the logging generator needs to use these methods as well, so we should move to a common location. cc @maryamariyan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. The second commit does this.