swagger-codegen-cli generates invalid C# from openapi.json
I downloaded openapi.json from YouTrack Server Build 2023.3.23390 Wednesday, January 17, 2024.
I am trying to build a C# client library using the current build of https://github.com/swagger-api/swagger-codegen
swagger-codegen runs without issue and creates a C# solution, which I then try to build. After a little fiddling with NuGet packages, I can get it to attempt the build, but at that point there are errors that are due to the definitions in openapi.json.
Specifically, many of the models (classes) are derived from other models in openapi.json using the keyword “allOf”. However, some of those derived models re-define some members, resulting in two members with the same name. swagger-codegen implements those members in both models, so there are warnings about an unintended override. That may or may not be a problem.
However, when swagger-codegen creates the constructor for the derived class, the arguments to that constructor include both definitions of the same named members, resulting in an error. Below is the list of errors that I receive, each indicating a shadowed members in a derived class. It seems to me that swagger-codegen is doing a rational thing, since the constructor should include arguments for members of the base class.
I cannot find anything in the OpenAPI documentation that indicates that this is legal. Is this a bug in the openapi.json from YouTrack, or a bug in swagger-codegen?
Severity Code Description Project File
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\GitLabChangesProcessor.cs
Error CS0100 The parameter name 'content' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Article.cs
Error CS0100 The parameter name 'visibility' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Article.cs
Error CS0100 The parameter name 'comments' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Article.cs
Error CS0100 The parameter name 'updatedBy' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Article.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\BitBucketChangesProcessor.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\BitbucketStandaloneChangesProcessor.cs
Error CS0100 The parameter name 'target' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\CustomFieldActivityItem.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\GiteaChangesProcessor.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\GitHubChangesProcessor.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\GogsChangesProcessor.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\JenkinsChangesProcessor.cs
Error CS0100 The parameter name 'url' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\JenkinsServer.cs
Error CS0100 The parameter name 'value' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\PeriodIssueCustomField.cs
Error CS0100 The parameter name 'name' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Project.cs
Error CS0100 The parameter name 'updateSharingSettings' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\SavedQuery.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\SpaceChangesProcessor.cs
Error CS0100 The parameter name 'target' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\SprintActivityItem.cs
Error CS0100 The parameter name 'updateSharingSettings' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Tag.cs
Error CS0100 The parameter name 'tagSharingSettings' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\Tag.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\TeamcityChangesProcessor.cs
Error CS0100 The parameter name 'url' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\TeamcityServer.cs
Error CS0100 The parameter name 'target' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\TextCustomFieldActivityItem.cs
Error CS0100 The parameter name 'value' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\TextIssueCustomField.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\UpsourceChangesProcessor.cs
Error CS0100 The parameter name 'url' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\UpsourceServer.cs
Error CS0100 The parameter name 'server' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\VcsHostingChangesProcessor.cs
Error CS0100 The parameter name 'url' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\VcsHostingServer.cs
Error CS0100 The parameter name 'added' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\WorkItemAuthorActivityItem.cs
Error CS0100 The parameter name 'target' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\WorkItemAuthorActivityItem.cs
Error CS0100 The parameter name 'added' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\WorkItemDurationActivityItem.cs
Error CS0100 The parameter name 'target' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\WorkItemDurationActivityItem.cs
Error CS0100 The parameter name 'target' is a duplicate YouTrack.Api C:\Projects\YouTrack\CSharpApi\api\src\YouTrack.Api\Model\WorkItemTypeActivityItem.cs
Please sign in to leave a comment.
Hi! This is Julia from YouTrack Support.
It looks like overriding derived properties is indeed not covered by the OpenAPI specification. Let me quote a response one of the OpenAPI v4 proposal contributors posted in this GitHub issue:
Despite being a questionable pattern, overriding inherited properties is quite a common use case (example) that is usually accepted by OpenAPI tools - for example, Swagger Parser deems the YouTrack schema valid. However, Swagger Codegen does seem to have some kind of a bug. For example, this specification produces the following code:
public Article(string content = default(string)) : base(content)
The derived
Article
class constructor correctly calls the base constructor with its owncontent
property. Now, here is what's generated for this specification:public Article(List<Article> childArticles = default(List<Article>), string content = default(string), string content = default(string), List<Article> childArticles = default(List<Article>)) : base(id, content, type)
As you can see, the properties in the
Article
class constructor are duplicated, and theid
property is missing altogether. Given that thechildArticles
property is not even present in theBaseArticle
schema, it looks like the issue might be caused by something other than overriding properties.I'd suggest creating an issue in the Swagger Codegen project on GitHub since the behavior can be reproduced with the current stable version (3.0.52). With that said, it's not entirely clear why
allOf
is used in that way in the YouTrack OpenAPI specification. So, I registered this issue in our tracker: https://youtrack.jetbrains.com/issue/JT-79930/OpenAPI-spec-allOf-might-not-be-justified-potentially-illegal-discriminator.Thank you for looking into this, Julia.
Just to be clear, are you asking me to report this to the Swagger Codegen project, or will you do it as part of your investigation? I'm not convinced there is any point in reporting an issue to Swagger Codegen when there is a question of whether the YouTrack spec is even compliant.
asthomas, I'd ask you to report the issue to Swagger Codegen for further investigation - they should be able to determine what exactly prompts the property duplication. Currently, despite certain design specifics, it doesn't seem like the YouTrack specification is inherently incorrect. As I mentioned, even Swagger's own OpenAPI schema validator finds the specification valid; besides, Swagger Codegen also duplicates properties that are only defined once in the specification.
Feel free to share the link to the issue here!
I have posted a bug report to the swagger-codegen project on GitHub. Looking at other people's bug reports, I don't hold out much hope for a quick response. You can see it here: https://github.com/swagger-api/swagger-codegen/issues/12356
I have also tested with openapi-generator, with different but similar results. I have reported the bug here: https://github.com/OpenAPITools/openapi-generator/issues/18163
What tools are you using to generate client APIs from your OpenAPI spec? swagger-codegen and openapi-generator are not the only tools for the purpose. Have you had better luck with something else?
Again, what tools are you using to generate APIs from the OpenAPI spec? I don't need to use any particular toolset, I just need a functioning C# API.
Hi,
Sorry for keeping you waiting!
We don't generally use any specific tools for generating APIs, so we won't be able to provide any recommendations. The most reliable approach seems to be correcting the generating code where it's needed.
I tried correcting the generated code, but the errors are pervasive. I tried removing some of the more broken classes, but they are prerequisites for other classes that I need.
Do you use this OpenAPI definition file to generate client APIs in another language (like JavaScript), or is it just a reference document? Is the problem here that the C# code generator is just broken for this spec? I haven't had any luck getting an answer from either the Swagger or OpenAPI projects, unfortunately.
The specification was created when the API was already in place, so our team doesn't generally use it for generating the API definitions. From what we can tell, the issue is indeed with the generators, so, unfortunately, not much can be done from our side.