我正在尝试使用 SwagGen 从 OpenAPI 3.0 规范生成客户端代码。示例规范是https://github.com/yonaskolb/SwagGen/blob/master/Specs/Petstore/spec.yml(翻译成 json)并且在调用时工作正常:
swaggen generate spec.json
我想用代表 Pet 的外部模式替换组件/模式部分:
"schemas": {
"Pet": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
和
"schemas": {
"Pet": {
"$ref": "pet.json"
},
其中 pet.json 位于同一目录中,如下所示:
{
"$schema": "http://json-schema.org/draft-05/schema#",
"$id": "https://example.com/schema/pet.json",
"title": "Pet",
"description": "Pet",
"type": "object",
"properties": {
"id": {
"description": "id",
"type": "integer",
"format": "int64"
},
"name": {
"description": "name",
"type": "string"
},
"tag": {
"type": "string"
}
},
"required": [
"id",
"name"
]
}
这给了我一个错误Fatal error: Reference ./pet.json is unresolved: file /private/tmp/swaggen-20190424-43085-jvmk5p/SwagGen-4.1.0/Sources/Swagger/Component/Reference.swift, line 10
Illegal instruction: 4
出于某种原因,当我改用 #ref 时:
"schemas": {
"Pet": {
"#ref": "./pet.json"
},
一代成功。但是,生成的 Pet 模型是空的:
// Generated by SwagGen
// https://github.com/yonaskolb/SwagGen
//
import Foundation
public class Pet: APIModel {
public init() {
}
public required init(from decoder: Decoder) throws {
}
public func encode(to encoder: Encoder) throws {
}
public func isEqual(to object: Any?) -> Bool {
guard object is Pet else { return false }
return true
}
public static func == (lhs: Pet, rhs: Pet) -> Bool {
return lhs.isEqual(to: rhs)
}
}
我试图理解为什么 $ref 会导致未解决的问题,为什么 #ref 会起作用,但会导致 Pet 模型不正确。