Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
143 stars 166 forks source link

"System.InvalidOperationException: Unhandled BinaryData example value type" when value is an array of objects #5171

Open brrusino opened 1 day ago

brrusino commented 1 day ago

Seeing the following stack trace:

Unhandled exception. System.InvalidOperationException: Unhandled BinaryData example value type
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendFrameworkTypeValue(CodeWriter writer, CSharpType type, InputExampleValue exampleValue, Boolean includeCollectionInitialization) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 117
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendExampleValue(CodeWriter writer, InputExampleValue exampleValue, CSharpType type, Boolean includeCollectionInitialization) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 46
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendDictionaryValue(CodeWriter writer, CSharpType type, InputExampleObjectValue exampleValue, Boolean includeInitialization) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 209
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendFrameworkTypeValue(CodeWriter writer, CSharpType type, InputExampleValue exampleValue, Boolean includeCollectionInitialization) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 106
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendExampleValue(CodeWriter writer, InputExampleValue exampleValue, CSharpType type, Boolean includeCollectionInitialization) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 46
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendObjectTypeValue(CodeWriter writer, ObjectType objectType, IReadOnlyDictionary`2 valueDict) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 483
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendTypeProviderValue(CodeWriter writer, CSharpType type, InputExampleValue exampleValue) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 404
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendExampleValue(CodeWriter writer, InputExampleValue exampleValue, CSharpType type, Boolean includeCollectionInitialization) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 46
   at AutoRest.CSharp.MgmtTest.Extensions.CodeWriterExtensions.AppendExampleParameterValue(CodeWriter writer, ExampleParameterValue exampleParameterValue) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Extensions\CodeWriterExtensions.cs:line 95
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteOperationInvocationParameters(Sample sample) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 480
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteSampleLroOperation(CodeWriterDeclaration instanceVar, Sample sample) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 357
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteSampleOperation(CodeWriterVariableDeclaration collectionResult, Sample sample) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 342
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteSampleOperationForResource(CodeWriterVariableDeclaration clientVar, Resource resource, Sample sample) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 219
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteSampleSteps(Sample sample) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 94
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteSample(Sample sample) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 75
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.WriteImplementation() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 46
   at AutoRest.CSharp.MgmtTest.Generation.Samples.MgmtSampleWriter.Write() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\Generation\Samples\MgmtSampleWriter.cs:line 37
   at AutoRest.CSharp.AutoRest.Plugins.MgmtTestTarget.WriteSamples(GeneratedCodeWorkspace project, MgmtTestOutputLibrary library) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\AutoRest\MgmtTestTarget.cs:line 74
   at AutoRest.CSharp.AutoRest.Plugins.MgmtTestTarget.ExecuteAsync(GeneratedCodeWorkspace project, InputNamespace inputNamespace, SourceInputModel sourceInputModel) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\MgmtTest\AutoRest\MgmtTestTarget.cs:line 45
   at AutoRest.CSharp.AutoRest.Plugins.CSharpGen.ExecuteAsync(InputNamespace rootNamespace) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Common\AutoRest\Plugins\CSharpGen.cs:line 92
   at AutoRest.CSharp.AutoRest.Communication.StandaloneGeneratorRunner.RunAsync(CommandLineOptions options) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Common\AutoRest\Communication\StandaloneGeneratorRunner.cs:line 62
   at AutoRest.CSharp.Program.Run(CommandLineOptions options) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Program.cs:line 47
   at AutoRest.CSharp.Program.<>c.<<Main>b__2_0>d.MoveNext() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Program.cs:line 29
--- End of stack trace from previous location ---
   at AutoRest.CSharp.Program.Main(String[] args) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Program.cs:line 26
   at AutoRest.CSharp.Program.<Main>(String[] args)

This seems to be when trying to process an example with object[] value when Record is the Proxy Resource's property model (legacy resource for our RP).

PR link to model definition in TypeSpec: https://github.com/Azure/azure-rest-api-specs/blob/792b94bd3d1e499882a4294fd345b2965684eed8/specification/chaos/Chaos.Management/Target.tsp#L20C1-L20C71

PR link to example property that seems to be causing failure: https://github.com/Azure/azure-rest-api-specs/blob/792b94bd3d1e499882a4294fd345b2965684eed8/specification/chaos/Chaos.Management/examples/2025-01-01/Targets_Get.json#L19

I would expect this model to be supported in the example, or otherwise the model definition for Target should be modified to another implementation that does support object[] as value.

ArcturusZhang commented 11 hours ago

Hi @brrusino thank you for opening this issue I am working on a refactor of sample related code: https://github.com/Azure/autorest.csharp/pull/5107 This is very likely to fix this issue - because the code in the stack trace is being refactored in above PR.

Could you also share where you saw this issue happens?