Open jrodenburg opened 4 years ago
@bradygaster is this something you're handline these days?
@mkArtakMSFT yes I think I would be involved, though it will require some attention on the part of @sayedihashimi as well given his old role is now his new role. :) @sayedihashimi - let's triage this together. Would either of you propose a method of being able to triage/tag all the Swagger/OAS-related issues? I'd like to start being on the hook for those.
@jrodenburg would you be willing/able to share the OAS doc?
Including the generated swagger.json for the service. swagger1.txt
Did you expect it would be an Open API 3.0 doc? I used the swagger validator to validate it - looks okay from a document validity point of view. Would you mind trying it converted to a 2.0 doc? Curious if you'd omit the errors then. Also cc @ryanbrandenburg
This appears to be a problem with code-generation itself as opposed to the tool (which merely adds `
Did you expect it would be an Open API 3.0 doc? I used the swagger validator to validate it - looks okay from a document validity point of view. Would you mind trying it converted to a 2.0 doc? Curious if you'd omit the errors then. Also cc @ryanbrandenburg
yes we made this intentionally as OA3. Our codebase has morphed so getting it back to 2.0 will require hand-manipulation or a tool.
This appears to be a problem with code-generation itself as opposed to the tool (which merely adds
<ItemGroup> <OpenApiReference Include="C:\Users\rybrande\Downloads\swagger1.json" /> </ItemGroup>
to the csproj). When I ran the steps above locally I got errors very similar to the ones reported above.When I inspect
obj\swagger1Client.cs
I can see that multiple partials ofswagger1Client
have been generated, each containing definitions of _baseUrl, _httpClient,_settings, etc, as well as all having theSystem.CodeDom.Compiler.GeneratedCode
attribute applied to them. swagger1Client.txt
That's exactly the output I had. Multiple partial class definitions that duplicate definitions (hence the build errors.) Issue title is likely misleading.
@jrodenburg would you be open to another approach? I am getting up-to-speed with help from @ryanbrandenburg on how this set of the tools work so i'm not entirely familiar with what could be happening here. I have a bit of familiarity with an approach that uses AutoRest, however, and if you're open to that as an alternative for now until we can resolve this I'd be willing to set up some time with you to walk through it or write up a doc on how to set it up. AutoRest would have the single dependency of needing NPM on your dev machine as that's how AutoRest is acquired and how it acquires dependencies it uses.
@bradygaster yes, i've actually used AutoRest previously (w/ dotnetcore v2.) Due to the node/npm dependency, can we bake this into a docker image, ala https://hub.docker.com/r/azuresdk/autorest?
@jrodenburg sorry for the late reply, i've actually been tinkering around with this a bunch. I presume you could do that, but wouldn't the Docker requirement be heavier than the NPM requirement?
wouldn't the Docker requirement be heavier than the NPM requirement?
Docker is my preference. Doing so would make implementation in our build pipeline far easier and manageable.
Wouldn't any modification just be an update to https://github.com/Azure/autorest/blob/master/Dockerfile? Seems easy enough.....
@jrodenburg checking in on this - did you give the dockerized autorest a shot?
I did, still no success. Here are the steps followed:
Received the following output:
[ $ ] docker run -it --rm -v $(pwd):/clients autorest autorest --base-folder=/clients --input-file=swagger.json --output-folder=csharp --csharp --namespace=random.namespace --v3
AutoRest code generation utility [cli version: 3.0.6247; node: v10.22.1, max-memory: 1280 gb]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
Loading AutoRest core '/root/.autorest/@autorest_core@3.0.6320/node_modules/@autorest/core/dist' (3.0.6320)
Installing AutoRest extension '@microsoft.azure/autorest.csharp' (~2.3.79)
Installed AutoRest extension '@microsoft.azure/autorest.csharp' (~2.3.79->2.3.90)
Installing AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.55)
Installed AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.55->2.3.55)
TypeError: Cannot delete property '17' of [object String]
I've just ran into this bug myself. I found that removing "operationId" from the swagger file causes the problem to go away.
Though it might help with debugging and may help @jrodenburg move forward
I encountered this issue today using the Swagger doc provided by Thycotic's Secret Server Cloud found here.
I used NSwagStudio 13.10.8.0 to generate the client against the same Swagger doc, and it generates the same bad output.
Is anyone working with Rico Suter on this issue?
adding @RicoSuter here - I'd like to get his input on this. It would be great if this could be mitigated, but the reality is that no one generation author can support all the nuances of use-cases for folks building OpenAPI specs. Also curious if @darrelmiller has thoughts on this.
I was able to resolve this by specifying the class name inside of the OpenApiReference
element:
<ClassName>{controller}Client</ClassName>
Using {controller}
prevents the same class name from being generated multiple times.
Is this resolved with @excelkobayashi fix?
@excelkobayashi answer worked for me. However, I blame the API I'm working against, not following standards. In this case it was Swagger 2.0, and the json wasn't 100% valid.
Thanks all, closing this out as there doesn't appear to be any direct action items remaining.
I'm not sure this should have been closed. We have a work around, that doesn't fix the fact that clients that don't compile are being generated if you specify an operationId in the swagger definition.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
I found that the issue is caused by operationId
where duplicated ID is used. The example of the following will cause this issue,
...
"operationId": "getUser",
...
"operationId": "getUser_1",
The [underscore] + [numeric] in the operationId
seems to be truncated, thus causing the duplicate.
Setting the option /OperationGenerationMode:SingleClientFromOperationId
fixed it for me:
e.g.
<OpenApiReference Include="connectv2.swagger.json">
<ClassName>ConnectV2Client</ClassName>
<Options>/OperationGenerationMode:SingleClientFromOperationId</Options>
</OpenApiReference>
There's a good write-up with all the options here: https://stevetalkscode.co.uk/openapireference-commands
Setting the option
/OperationGenerationMode:SingleClientFromOperationId
fixed it for me:e.g.
<OpenApiReference Include="connectv2.swagger.json"> <ClassName>ConnectV2Client</ClassName> <Options>/OperationGenerationMode:SingleClientFromOperationId</Options> </OpenApiReference>
There's a good write-up with all the options here: https://stevetalkscode.co.uk/openapireference-commands
Worked for me, but had to delete the obj folder first
I am using it for Apple Connect API (specs, grabbed from the doc), I still have this issue. I added <Options>/OperationGenerationMode:SingleClientFromOperationId</Options>
but I received a new error from this faulty generated code:
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "14.0.7.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum SubscriptionStatusUrlVersion
{
[System.Runtime.Serialization.EnumMember(Value = @"V1")]
V1 = 0,
[System.Runtime.Serialization.EnumMember(Value = @"V2")]
V2 = 1,
[System.Runtime.Serialization.EnumMember(Value = @"v1")]
V1 = 2,
[System.Runtime.Serialization.EnumMember(Value = @"v2")]
V2 = 3,
}
The type 'SubscriptionStatusUrlVersion' already contains a definition for 'V1'
// And more
Describe the bug
Using the dotnet-openapi tool, a broken client (uncompilable) is generated from a swagger definition.
To Reproduce
Further technical details
[ $ ~ ] dotnet --info .NET Core SDK (reflecting any global.json): Version: 3.1.200 Commit: c5123d973b
Runtime Environment: OS Name: Mac OS X OS Version: 10.14 OS Platform: Darwin RID: osx.10.14-x64 Base Path: /usr/local/share/dotnet/sdk/3.1.200/
Host (useful for support): Version: 3.1.3 Commit: 4a9f85e9f8
.NET Core SDKs installed: 2.2.402 [/usr/local/share/dotnet/sdk] 3.0.100 [/usr/local/share/dotnet/sdk] 3.1.100 [/usr/local/share/dotnet/sdk] 3.1.103 [/usr/local/share/dotnet/sdk] 3.1.200 [/usr/local/share/dotnet/sdk]
.NET Core runtimes installed: Microsoft.AspNetCore.All 2.2.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.2.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.16 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download