gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:27 26 4
gpt4 key购买 nike

我正在使用 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 中,我们有一个返回以下内容的发布请求。

{
"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 类型的属性。

要点是,在使用与现有模型匹配的 key 时要小心,生成代码时可能会覆盖现有模型。

关于swift - Swagger-codegen 创建了一个模型,其中参数已被对另一个模型的引用覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54205813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com