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

时间:2019-01-15 19:43:36

标签: swift swagger swagger-2.0 swagger-codegen

我正在使用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生成代码时,将获得以下用于Relationship的模型。

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
    }
...

}

1 个答案:

答案 0 :(得分:0)

我偶然发现了解决方案。在我们的API中,我们有一个发布请求,该请求返回以下内容。

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

以下是相关的摇摇欲坠的代码:

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的属性的模型覆盖预期的Relationship模型。

总结,使用与现有模型匹配的键时要小心,生成代码时可能会覆盖现有模型。