[Swift3] Array Model Error in Generated Code (v2.3.0)
还没有人认领这个 Issue。
评估
调研方向
从 modules/swagger-codegen/src/main/resources/swift3/Models.mustache 开始,检查由 isArrayModel 控制的解码器部分。从包含数组模型的 API 生成 Swift 3 代码,然后在 Xcode 中编译生成的模型。完成标准是:生成的数组模型文件能够编译,不出现提前返回错误,并且普通模型生成仍然保持正常。
由索引模型根据 Issue 内容生成。
描述
Description
There seems to be an error in generated Swift 3 code being created in version 2.3.0 due to the placement of a return statement at the beginning of the "isArrayModel" section in the default Swift 3 Models.mustache file.
The effect of this is that generated Swift 3 model code does not correctly compile when an array model is being used. I noticed this while generating Swift code for an in-house API using the version 2.3.0 branch. Our API does use an array model, hence the triggering of this error.
Swagger-codegen version
2.3.0
Steps to reproduce
To reproduce this, run Swagger Codegen V2.3.0 against an API which uses an array model. The resulting Swift model file will not compile in Xcode due to early return statements.
Suggest a Fix
It looks like the fix for this should be fairly simple and to edit the following section of the default Swift 3 Models.mustache file to edit the following section:
{{^isArrayModel}}
// Decoder for [{{{classname}}}]
return Decoders.decode(clazz: [{{{classname}}}].self, source: source, instance: instance)
}
// Decoder for {{{classname}}}
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<{{{classname}}}> in
{{#isEnum}}
...
To add the decoder declaration for an array type as follows (giving the return statement a closure to return from):
{{^isArrayModel}}
// Decoder for [{{{classname}}}]
Decoders.addDecoder(clazz: [{{{classname}}}].self) { (source: AnyObject, instance: AnyObject?) -> Decoded<[{{{classname}}}]> in
return Decoders.decode(clazz: [{{{classname}}}].self, source: source, instance: instance)
}
// Decoder for {{{classname}}}
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<{{{classname}}}> in
{{#isEnum}}
...
I have trialled this within the app I am working on and this seems to fix the issue. I can raise a PR for this if it would be useful. It would be useful to know if this change has a wider impact, or if this is simply a bug in the template. The same issue does not appear in the current master version of swagger-codegen, but it does seem as if there have been some changes made to the Swift template for the 2.3.0 version.
I am using the 2.3.0 version as it supports the ability to retrieve data payloads from API errors via the ErrorResponse enum, and therefore fixing this issue is pretty important for our project (we are currently using a branched version with the fix outlined above in place.
- 主要语言
- Mustache
- 星标
- 17.8k
- 派生
- 6k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
swagger-api/swagger-codegen 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
swagger-api/swagger-codegen#12755 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
swagger-api/swagger-codegen#12445 ·
-
security vulnerability
难度 1/5 1 小时以内 新手友好度 62/100
swagger-api/swagger-codegen#11942 ·
-
[JAVA] Non compilable enums generated with property errorOnUnknownEnum=true可能已有人在做 @HiagoW 于 1464 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
swagger-api/swagger-codegen#11812 · 1 个 reaction ·
-
security vulnerability
难度 2/5 1-3 小时 新手友好度 65/100
swagger-api/swagger-codegen#11594 ·
查看 swagger-api/swagger-codegen 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
apache/rocketmq-dashboard#7302 ·
维护者通常 3 天内回复
-
bug enhancement python
难度 2/5 1-3 小时 新手友好度 85/100
DewaldOosthuizen/tech-cheat-sheets-and-notes#392 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 67/100
mobile-next/mobile-mcp#526 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
aws-cloudformation/cfn-lint#4804 ·
维护者通常 7 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复