Swagger-codegen 创建了一个模型,其中参数已被对另一个模型的引用覆盖

Swagger-codegen is creates a model where parameters have been overwritten by a reference to another model

我正在使用 swagger-codegen 生成 swift 客户端。我的 swagger.yaml 文件包含以下模型定义:

RelationshipCollection:
    type: object
    description: a collection of relationships
    required:
      - pagination
      - relationships
    properties:
      pagination:
        $ref: '#/definitions/PaginationData'
      relationships:
        type: array
        items:
          $ref: '#/definitions/Relationship'
  Relationship:
    type: object
    description: Indicates the relationship between a parent and a student.
    properties:
      relationship_id:
        type: integer
        format: int32
      parent:
        $ref: '#/definitions/SwaggerUser'
      student:
        $ref: '#/definitions/SwaggerUser'
  RelationshipCreate:
    name: RelationshipCreate
    type: object
    description: What a student must send to the system to form a `Relationship` with their parent. Cannot be created without an `Invitation`.
    required:
      - token
      - security_answer
    properties:
      token:
        type: string
        example: jRMcN645BQyDr67yHR3qjsJF
        description: The token from the `Invitation` used to create this relationship
      security_answer:
        type: string
        example: Some kind of answer to a security question
        description: The answer to the security question asked in the `Invitation`

当我使用 swagger-codegen 生成代码时,我得到以下关系模型。

open class Relationship: Codable {

    public var relationshipCreate: RelationshipCreate



    public init(relationshipCreate: RelationshipCreate) {
        self.relationshipCreate = relationshipCreate
    }


    // Encodable protocol methods

    public func encode(to encoder: Encoder) throws {

        var container = encoder.container(keyedBy: String.self)

        try container.encode(relationshipCreate, forKey: "relationshipCreate")
    }

    // Decodable protocol methods

    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)

        relationshipCreate = try container.decode(RelationshipCreate.self, forKey: "relationshipCreate")
    }
}

我期待以下内容:

open class Relationship: Codable {

    public var relationshipId: Int?
    public var parent: SwaggerUser?
    public var student: SwaggerUser?


    public init(relationshipID: Int?, parent: SwaggerUser?, student: SwaggerUser?) {
        self.relationshipID = relationshipID
        self.parent = parent
        self.student = student
    }
...

}

我偶然发现了这个问题的解决方案。在我们的 API 中,我们有一个 post 请求 returns 以下内容。

{
  "relationship": {
    "token": "jRMcN645BQyDr67yHR3qjsJF",
    "security_answer": "Some kind of answer to a security question"
  }
}

这是相关的 swagger 代码:

post:
  summary: Create a relationship
  description: Create a relationship between a parent and a student. The student accepts the parent's `Invitation` by providing it's `token` and the correct answer to their `security_question`. Also marks the invitation as accepted so it cannot be used again.
  tags:
    - Relationships
  parameters:
    - $ref: '#/parameters/user_id'
    - name: Accept-Language
      description: 'see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language'
      in: header
      type: string
      default: en
    - in: body
      name: relationship
      schema:
        type: object
        required:
          - relationship
        properties:
          relationship:
            $ref: '#/definitions/RelationshipCreate'
  responses:
    '201':
      description: ''
      schema:
        $ref: '#/definitions/Relationship'
    '400':
      description: Bad Request

键 "relationship" 的值是一个 RelationshipCreation 对象。 Swagger-codegen 似乎解析了这个响应对象,并用一个模型覆盖了预期的关系模型,这个模型以这个键作为它的名称和一个 属性 类型的 RelationshipCreation。

要注意的是,使用与现有模型匹配的密钥时要小心,生成代码时可能会覆盖现有模型。