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

问题描述 投票:2回答:1

我正在使用swagger-codegen来生成一个快速的客户端。我的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
    }
...

}
swift swagger swagger-2.0 swagger-codegen
1个回答
0
投票

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

{
  "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

键“关系”的值是RelationshipCreation对象。 Swagger-codegen似乎解析了这个响应对象,并用一个模型覆盖了预期的Relationship模型,该模型的名称和RelationshipCreation类型的属性。

外卖时,使用与现有型号匹配的键时要小心,在生成代码时可能会覆盖现有的型号。

© www.soinside.com 2019 - 2024. All rights reserved.