OpenAPI Swift Generator

Hello, I'm using the generator to create a client from this API:

https://github.com/griptape-ai/griptape-cloud-control-plane/blob/main/models/Griptape.openapi.json

Everything seems fine until I go to call a method. I get an error on a DateTime format mismatch in the response.

Client error - cause description: 'Unknown', underlying error: DecodingError: dataCorrupted - at : Expected date string to be ISO8601-formatted. (underlying error: <nil>),

Is there a decoder option or something I can attach to the generated client code to adjust for this?

Answered by kyrodrax in 776264022

Was able to fix w/ a transcoder: Added this to configuration of the Client.

struct ComplainLessTranscoder: DateTranscoder {

        /// Creates and returns an ISO 8601 formatted string representation of the specified date.
        public func encode(_ date: Date) throws -> String { ISO8601DateFormatter().string(from: date) }

        /// Creates and returns a date object from the specified ISO 8601 formatted string representation.
        public func decode(_ dateString: String) throws -> Date {
            let formatter = ISO8601DateFormatter()
            formatter.formatOptions = [.withFractionalSeconds]
            guard let date = formatter.date(from: dateString) else {
                throw DecodingError.dataCorrupted(
                    .init(codingPath: [], debugDescription: "Expected date string to be ISO8601-formatted.")
                )
            }
            return date
        }
    }

    let dateTrans = ComplainLessTranscoder()
Accepted Answer

Was able to fix w/ a transcoder: Added this to configuration of the Client.

struct ComplainLessTranscoder: DateTranscoder {

        /// Creates and returns an ISO 8601 formatted string representation of the specified date.
        public func encode(_ date: Date) throws -> String { ISO8601DateFormatter().string(from: date) }

        /// Creates and returns a date object from the specified ISO 8601 formatted string representation.
        public func decode(_ dateString: String) throws -> Date {
            let formatter = ISO8601DateFormatter()
            formatter.formatOptions = [.withFractionalSeconds]
            guard let date = formatter.date(from: dateString) else {
                throw DecodingError.dataCorrupted(
                    .init(codingPath: [], debugDescription: "Expected date string to be ISO8601-formatted.")
                )
            }
            return date
        }
    }

    let dateTrans = ComplainLessTranscoder()

You can also use the built-in fractional seconds transcoder: https://swiftpackageindex.com/apple/swift-openapi-runtime/1.4.0/documentation/openapiruntime/datetranscoder/iso8601withfractionalseconds

You provide it in the initializer to the Client's configuration:

let client(..., configuration: .init(dateTranscoder: .iso8601WithFractionalSeconds))
OpenAPI Swift Generator
 
 
Q