Swaggen外部参考因未解析的参考而失败

时间:2019-06-04 22:05:40

标签: json swagger openapi openapi-generator swaggen

我正在尝试使用SwagGen从OpenAPI 3.0规范生成客户端代码。样本规范为https://github.com/yonaskolb/SwagGen/blob/master/Specs/Petstore/spec.yml(转换为json),当通过以下方式调用时可以正常工作:

swaggen generate spec.json

我想用代表Pet的外部模式替换components / schemas部分:

      "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模型。

0 个答案:

没有答案
相关问题