dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.34k stars 4.74k forks source link

JsonSerializerContext generated source breaks for property named "@event" #109854

Open burtonrodman opened 2 hours ago

burtonrodman commented 2 hours ago

Description

Json source generator generates invalid code when a property name is a keyword, like @event

Reproduction Steps

  1. create a class that has a property call @event
  2. create a JsonSerializerContext class that references above class
  3. build and notice compiler errors in generated code
public class ClickEvent {
  public required string @event { get; set; }
}

[JsonSerializable(typeof(ClickEvent))]
public partial class ClientEventContext : JsonSerializerContext { }

Expected behavior

generated code does not cause compile errors

Actual behavior

        private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::TwentyTenSolutions.ContentManagement.Editing.ViewModels.ClickEvent> Create_ClickEvent(global::System.Text.Json.JsonSerializerOptions options)
        {
            if (!TryGetTypeInfoForRuntimeCustomConverter<global::TwentyTenSolutions.ContentManagement.Editing.ViewModels.ClickEvent>(options, out global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::TwentyTenSolutions.ContentManagement.Editing.ViewModels.ClickEvent> jsonTypeInfo))
            {
                var objectInfo = new global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues<global::TwentyTenSolutions.ContentManagement.Editing.ViewModels.ClickEvent>
                {
                    ObjectCreator = null,
                    ObjectWithParameterizedConstructorCreator = static args => new global::TwentyTenSolutions.ContentManagement.Editing.ViewModels.ClickEvent(){ event = (string)args[0] },
                    PropertyMetadataInitializer = _ => ClickEventPropInit(options),
                    ConstructorParameterMetadataInitializer = ClickEventCtorParamInit,
                    SerializeHandler = ClickEventSerializeHandler
                };

                jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo<global::TwentyTenSolutions.ContentManagement.Editing.ViewModels.ClickEvent>(options, objectInfo);
                jsonTypeInfo.NumberHandling = null;
            }

            jsonTypeInfo.OriginatingResolver = this;
            return jsonTypeInfo;
        }

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

dotnet-policy-service[bot] commented 2 hours ago

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.

burtonrodman commented 2 hours ago
 *  Executing task in folder MyFirm.ContentManagement: C:\Program Files\dotnet\dotnet.exe build D:\source\MyFirm\Internal\MyFirm.ContentManagement/MyFirm.ContentManagement.sln /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary;ForceNoAlign 

  Determining projects to restore...
  All projects are up-to-date for restore.
  MyFirm.ContentManagement.Editing.Domain -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing.Domain\bin\Debug\net8.0\MyFirm.ContentManagement.Editing.Domain.dll
  MyFirm.ContentManagement.Data -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\data\MyFirm.ContentManagement.Data\bin\Debug\net8.0\MyFirm.ContentManagement.Data.dll
  MyFirm.ContentManagement.Editing.Api -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing.Api\bin\Debug\net8.0\MyFirm.ContentManagement.Editing.Api.dll
  MyFirm.ContentManagement.Rendering -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\rendering\MyFirm.ContentManagement.Rendering\bin\Debug\net8.0\MyFirm.ContentManagement.Rendering.dll
  MyFirm.ContentManagement.Rendering.Tests -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\rendering\MyFirm.ContentManagement.Rendering.Tests\bin\Debug\net8.0\MyFirm.ContentManagement.Rendering.Tests.dll
  sampleSite -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\samples\site\sampleSite\bin\Debug\net8.0\sampleSite.dll
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\PickImageEventContext.PickImageEvent.g.cs(36,17): error CS8802: Only one compilation unit can have top-level statements. [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotEditorsInitEventContext.SlotEditorsInitEvent.g.cs(36,17): error CS8802: Only one compilation unit can have top-level statements. [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotInsertBlockEventContext.SlotInsertBlockEvent.g.cs(36,17): error CS8802: Only one compilation unit can have top-level statements. [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockActionMenuClickEventContext.BlockActionMenuClickEvent.g.cs(36,17): error CS8802: Only one compilation unit can have top-level statements. [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\ClientEventContext.ClickEvent.g.cs(36,17): error CS8802: Only one compilation unit can have top-level statements. [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\ClientEventContext.ClickEvent.g.cs(30,168): error CS0065: 'ClientEventContext.': event property must have both add and remove accessors [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockEditableInputEventContext.BlockEditableInputEvent.g.cs(30,214): error CS0065: 'BlockEditableInputEventContext.': event property must have both add and remove accessors [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\PickImageEventContext.PickImageEvent.g.cs(30,172): error CS0065: 'PickImageEventContext.': event property must have both add and remove accessors [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotEditorsInitEventContext.SlotEditorsInitEvent.g.cs(30,178): error CS0065: 'SlotEditorsInitEventContext.': event property must have both add and remove accessors [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]  
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockEditableInputEventContext.BlockEditableInputEvent.g.cs(31,56): error CS1520: Method must have a return type [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockEditableInputEventContext.BlockEditableInputEvent.g.cs(31,56): error CS0501: '<invalid-global-code>.<invalid-global-code>(options)' must declare a body because it is not marked abstract, extern, or partial [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\PickImageEventContext.PickImageEvent.g.cs(31,56): error CS1520: Method must have a return type [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\PickImageEventContext.PickImageEvent.g.cs(31,56): error CS0501: '<invalid-global-code>.<invalid-global-code>(options)' must declare a body because it is not marked abstract, extern, or partial [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotEditorsInitEventContext.SlotEditorsInitEvent.g.cs(31,56): error CS1520: Method must have a return type [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotEditorsInitEventContext.SlotEditorsInitEvent.g.cs(31,56): error CS0501: '<invalid-global-code>.<invalid-global-code>(options)' must declare a body because it is not marked abstract, extern, or partial [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotInsertBlockEventContext.SlotInsertBlockEvent.g.cs(31,56): error CS1520: Method must have a return type [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotInsertBlockEventContext.SlotInsertBlockEvent.g.cs(31,56): error CS0501: '<invalid-global-code>.<invalid-global-code>(options)' must declare a body because it is not marked abstract, extern, or partial [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockActionMenuClickEventContext.BlockActionMenuClickEvent.g.cs(31,56): error CS1520: Method must have a return type [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockActionMenuClickEventContext.BlockActionMenuClickEvent.g.cs(31,56): error CS0501: '<invalid-global-code>.<invalid-global-code>(options)' must declare a body because it is not marked abstract, extern, or partial [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\ClientEventContext.ClickEvent.g.cs(31,56): error CS1520: Method must have a return type [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\ClientEventContext.ClickEvent.g.cs(31,56): error CS0501: '<invalid-global-code>.<invalid-global-code>(options)' must declare a body because it is not marked abstract, extern, or partial [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockActionMenuClickEventContext.BlockActionMenuClickEvent.g.cs(30,209): error CS0065: 'BlockActionMenuClickEventContext.': event property must have both add and remove accessors [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotInsertBlockEventContext.SlotInsertBlockEvent.g.cs(30,206): error CS0065: 'SlotInsertBlockEventContext.': event property must have both add and remove accessors [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]  
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockEditableInputEventContext.BlockEditableInputEvent.g.cs(31,88): error CS0246: The type or namespace name 'options' could not be found (are you missing a using directive or an assembly reference?) [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\PickImageEventContext.PickImageEvent.g.cs(31,79): error CS0246: The type or namespace name 'options' could not be found (are you missing a using directive or an assembly reference?) [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotEditorsInitEventContext.SlotEditorsInitEvent.g.cs(31,85): error CS0246: The type or namespace name 'options' could not be found (are you missing a using directive or an assembly reference?) [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SlotInsertBlockEventContext.SlotInsertBlockEvent.g.cs(31,85): error CS0246: The type or namespace name 'options' could not be found (are you missing a using directive or an assembly reference?) [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\BlockActionMenuClickEventContext.BlockActionMenuClickEvent.g.cs(31,90): error CS0246: The type or namespace name 'options' could not be found (are you missing a using directive or an assembly reference?) [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\obj\Debug\net8.0\generated\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\ClientEventContext.ClickEvent.g.cs(31,75): error CS0246: The type or namespace name 'options' could not be found (are you missing a using directive or an assembly reference?) [D:\source\MyFirm\Internal\MyFirm.ContentManagement\editing\MyFirm.ContentManagement.Editing\MyFirm.ContentManagement.Editing.csproj]
  sample.Client -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\samples\sample\sample.Client\bin\Debug\net8.0\sample.Client.dll
  sample.Client (Blazor output) -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\samples\sample\sample.Client\bin\Debug\net8.0\wwwroot
  sample -> D:\source\MyFirm\Internal\MyFirm.ContentManagement\samples\sample\sample\bin\Debug\net8.0\sample.dll

Workload updates are available. Run `dotnet workload list` for more information.

 *  The terminal process "C:\Program Files\dotnet\dotnet.exe 'build', 'D:\source\MyFirm\Internal\MyFirm.ContentManagement/MyFirm.ContentManagement.sln', '/property:GenerateFullPaths=true', '/consoleloggerparameters:NoSummary;ForceNoAlign'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it.