Skip to content

Question: Override Date Representation #32

@rmecham

Description

@rmecham

I have a Content object that includes a date, that I encode as an ISO 8601 string on output:

struct Info: Content {
    let id: UUID
    let createdDate: Date

    static var openAPISchema: SchemaObject {
        .object(
            properties: [
                "id": .string(format: .uuid, description: "Unique identifier"),
                "createdDate": .string(format: .dateTime, description: "ISO 8601 date-time"),
            ],
            required: ["id", "createdDate"]
        )
    }

    enum CodingKeys: String, CodingKey {
        case id
        case createdDate
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.id, forKey: .id)
        let formatter = ISO8601DateFormatter()
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        let createdDateString = formatter.string(from: self.createdDate)
        try container.encodeIfPresent(createdDateString, forKey: .createdDate)
    }
}

No matter what I do, however, the OpenAPI docs report createdDate as integer($int64) and seems to completely ignore the openAPISchema object I've defined. How can I override the type of createdDate to ensure that OpenAPI reports it as an ISO 8601 formatted string?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions