Generation failure involving JsonProperty and JsonConstructor
See original GitHub issueIssue
I’ve tried to generate the client code for the first time for a new service and I was prompted with this error:
Autorest stacktrace
AutoRest code generation utility [version: 2.0.4283; node: v8.11.1]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4283\autorest-core -> C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4283\node_modules\@microsoft.azure\autorest-core\dist\app.js
C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4283\autorest-language-service -> C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4283\node_modules\@microsoft.azure\autorest-core\dist\language-service\language-service.js
Loading AutoRest core 'C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4283\node_modules\@microsoft.azure\autorest-core\dist' (2.0.4283)
Loading AutoRest extension '@microsoft.azure/autorest.typescript' (2.0.367->2.0.367)
Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.1.23->2.1.23)
FATAL: System.InvalidOperationException: Property 'agent' in Model 'FpsModel' is marked readOnly and is also required. This is not allowed.
at AutoRest.Modeler.SchemaBuilder.BuildServiceType(String serviceTypeName) in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\SchemaBuilder.cs:line 113
at AutoRest.Modeler.ObjectBuilder.BuildServiceType(String serviceTypeName) in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\ObjectBuilder.cs:line 151
at AutoRest.Modeler.SchemaBuilder.ParentBuildServiceType(String serviceTypeName) in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\SchemaBuilder.cs:line 209
at AutoRest.Modeler.SchemaBuilder.BuildServiceType(String serviceTypeName) in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\SchemaBuilder.cs:line 40
at AutoRest.Modeler.SchemaBuilder.BuildServiceType(String serviceTypeName) in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\SchemaBuilder.cs:line 135
at AutoRest.Modeler.SwaggerModeler.BuildCompositeTypes() in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\SwaggerModeler.cs:line 300
at AutoRest.Modeler.SwaggerModeler.Build(ServiceDefinition serviceDefinition) in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\SwaggerModeler.cs:line 69
at AutoRest.Modeler.Program.<ProcessInternal>d__2.MoveNext() in C:\Users\ci\AppData\Local\Temp\PUBLISH948lr\22_20171019T215822\autorest.modeler\src\Program.cs:line 53
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NewPlugin.<Process>d__15.MoveNext()
FATAL: typescript/modeler - FAILED
FATAL: Error: Plugin modeler reported failure.
Process() cancelled due to exception : Plugin modeler reported failure.
Error: Plugin modeler reported failure.
My colleague modeled the following abstraction in C#:
[JsonObject(MemberSerialization.OptIn)]
public sealed class AgentModel
{
#region Properties
[JsonProperty(PropertyName = "agentId", Required = Required.Always)]
public string AgentId { get; }
// Other properties omitted
#endregion
#region Constructors
[JsonConstructor]
public AgentModel(string agentId)
{
AgentId = agentId;
// Other constructor parameters omitted
}
#endregion
}
He was able to generate the C# client code successfully with Autorest, but we didn’t succeed for the TS one. In this context, the error shouldn’t occur since we define a JsonConstructor
that will handle the read-only property (AgentId
). As a workaround, we could remove the Required
property in the JsonProperty
attribute, but we would like to keep it that way, but we might be wrong. If the issue is not related specifically to the TS extension, let me know and I will export the issue in the appropriate repository.
Service’s configuration
typescript: true
use:
- "@microsoft.azure/autorest.typescript@2.0.367"
input-file:
- (external swagger.json)
add-credentials: true
verbose: true
output-folder: './../gateway'
autorest --info
AutoRest code generation utility [version: 2.0.4283; node: v8.11.1]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
Showing All Installed Extensions
Type Extension Name Version Location
core @microsoft.azure/autorest-core 2.0.4280 C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4280
core @microsoft.azure/autorest-core 2.0.4283 C:\Users\fpelletier\.autorest\@microsoft.azure_autorest-core@2.0.4283
extension @microsoft.azure/autorest.modeler 2.1.23 C:\Users\fpelletier\.autorest\@microsoft.azure_autorest.modeler@2.1.23
extension @microsoft.azure/autorest.typescript 2.0.304 C:\Users\fpelletier\.autorest\@microsoft.azure_autorest.typescript@2.0.304
extension @microsoft.azure/autorest.typescript 2.0.330 C:\Users\fpelletier\.autorest\@microsoft.azure_autorest.typescript@2.0.330
extension @microsoft.azure/autorest.typescript 2.0.367 C:\Users\fpelletier\.autorest\@microsoft.azure_autorest.typescript@2.0.367
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Generation failure involving JsonProperty and JsonConstructor
I tried to use Microsoft's Autorest to generate the client code for one of my web services where the Swagger definition is generated...
Read more >How to deserialize a class with overloaded constructors using ...
newOne is the overloaded constructor creator. That means that code like that won't work @JsonCreator public Phone(@JsonProperty( ...
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
The Newtonsoft.Json [JsonConstructor] attribute lets you specify which constructor to call when deserializing to a POCO. System.Text.Json ...
Read more >Painless JSON with Kotlin and jackson - Mario Fernandez
I want to talk about my experience using Kotlin and Jackson for this. I remember that dealing with JSON in Java used to...
Read more >JSON serialization - Immutables
Generation of @JsonCreator , @JsonProperty annotations and a helper class. Delegates everything back to Jackson using its powerful binding engine. Comprehensive ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, I think it would be great to open an issue there and start the conversation on their side.
Thanks for bringing the issue to our attention @felpel. Please reopen if there’s anything actionable in autorest.typescript for this.