简体   繁体   English

NSwag:生成 C# 客户端导致代码无效

[英]NSwag: Generation of a C# client results in invalid code

I am trying to generate a C# client for the Trello API.我正在尝试为 Trello API 生成C#客户端。 Therefore I have downloaded the Open API specification from https://developer.atlassian.com/cloud/trello/swagger.v3.json and run the following nswag command from the bash. Therefore I have downloaded the Open API specification from https://developer.atlassian.com/cloud/trello/swagger.v3.json and run the following nswag command from the bash.

nswag openapi2csclient /input:swagger.v3.json \
    /classname:TrelloClient \
    /namespace:Integrations.Trello \
    /output:TrelloClient.cs \
    /GenerateClientInterfaces:True \
    /GenerateExceptionClasses:True \
    /GenerateClientClasses:True \
    /DisposeHttpClient:False \
    /OperationGenerationMode:SingleClientFromOperationId

The code generation completes without errors, but the generated code does not compile because it contains many errors.代码生成完成且没有错误,但生成的代码由于包含许多错误而无法编译。 For example, some of the generated method names contain invalid expressions like =idAsync , or method signatures have ambiguous parameters (for instance, multiple key and token parameters of type string ).例如,某些生成的方法名称包含无效的表达式,例如=idAsync ,或者方法签名具有不明确的参数(例如, string类型的多个keytoken参数)。 The following method declaration has been generated for the GetMembers method, which is obviously the wrong syntax.GetMembers方法生成了以下方法声明,这显然是错误的语法。

System.Threading.Tasks.Task<Member> GetMembers=idAsync(
    string key, string token, string id, string actions, string boards, BoardBackgrounds? boardBackgrounds, 
    BoardsInvited? boardsInvited, BoardFields? boardsInvited_fields, bool? boardStars, string cards, 
    CustomBoardBackgrounds? customBoardBackgrounds, CustomEmoji2? customEmoji, CustomStickers? customStickers, 
    MemberFields? fields, string notifications, Organizations? organizations, 
    OrganizationFields? organization_fields, bool? organization_paid_account, 
    OrganizationsInvited? organizationsInvited, OrganizationFields? organizationsInvited_fields, 
    bool? paid_account, bool? savedSearches, Tokens? tokens);

Are there any special options that need to be set when processing an Open API specification document of version 3?在处理版本 3 的 Open API 规范文档时,是否需要设置任何特殊选项?

I was able to solve syntax issues regarding the get-members method in the generated code by changing the OperationGenerationMode from SingleClientFromOperationId to SingleClientFromPathSegments .通过将OperationGenerationModeSingleClientFromOperationId更改为SingleClientFromPathSegments ,我能够解决生成代码中有关get-members方法的语法问题。 This works as a workaround because the =id term only appears in the operationId .这是一种解决方法,因为=id术语仅出现在operationId中。

I inspected Trello´s Open API specification document and found out that the definition of get-members contained the =id term in the operationId ;我检查了 Trello 的 Open API 规范文档,发现get-members的定义在operationId中包含=id术语; not sure if this is wrong in the spec, or the generator is not able to handle that case correctly.不确定这是否在规范中出错,或者生成器无法正确处理这种情况。 The definition of the get-members method looks like that (foreshortened): get-members方法的定义如下所示(缩短):

... "/members/{id}":
{
   "get":
   {
       "tags": [],
       "operationId":"get-members=id",
       "parameters" ...
   }
}

As a sidenote, the reason why I use the SingleClient... operation modes is that Nswag also adds GeneratedCode attributes to any partial class and interface which also results in invalid code (this attribute can only be applied once).作为旁注,我使用SingleClient...操作模式的原因是Nswag还将GeneratedCode属性添加到任何部分 class 和接口,这也会导致无效代码(此属性只能应用一次)。 The SingleClient... operation mode solves that. SingleClient...操作模式解决了这个问题。

What remains is the problem that NSwag generates methods with duplicated parameters, which has been reported as an issue, but is not solved yet;剩下的是NSwag生成具有重复参数的方法的问题,该问题已被报告为问题,但尚未解决; see https://github.com/RicoSuter/NSwag/issues/2560 for further information.有关详细信息,请参阅https://github.com/RicoSuter/NSwag/issues/2560 Last but not least, I tried to remove the =id from the operationId and switched back to the SingleClientFromOperationId operation mode to check whether this does the trick, which is not the case.最后但并非最不重要的一点是,我尝试从operationId中删除=id并切换回SingleClientFromOperationId操作模式以检查这是否有效,但事实并非如此。

I assume that NSwag is used by a broad audience which makes me think that this must be more related to the processed spec than Nswag .我假设NSwag被广泛的受众使用,这让我认为这一定比Nswag与处理后的规范更相关。 Thus, I looked up the spec for methods that have been reported by the C# compiler to have duplicated parameters.因此,我查找了 C# 编译器报告的具有重复参数的方法的规范。 Here is an example.这是一个例子。

...
{
    "name": "token",
    "in": "query",
    "description": "The API token to use",
    "required": true,
    "schema": {
        "$ref": "#/components/schemas/APIToken"
    }
},
{
    "name": "token",
    "in": "path",
    "description": "",
    "required": true,
    "schema": {
        "type": "string"
    }
}
...

So it seems that a method can have parameters of the same name, but they can appear in different places such a the query-string, or be part of the URL path.所以看起来一个方法可以有同名的参数,但它们可以出现在不同的地方,比如查询字符串,或者是 URL 路径的一部分。 Then, it might be Nswag that produces the wrong output;那么,可能是Nswag产生了错误的 output; if it is intended that both parameters can be sent to the API, then the generator should just prefix the names to avoid ambiguities, for instance, queryToken and pathToken , leaving it up to the developer to decide what parameter to use or to give a hint to the expected value (in case both parameters are required and expect different values).如果打算将两个参数都发送到 API,那么生成器应该只在名称前加上前缀以避免歧义,例如queryTokenpathToken ,由开发人员决定使用什么参数或给出提示到期望值(如果两个参数都需要并且期望不同的值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM