apple / swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
https://swiftpackageindex.com/apple/swift-openapi-generator/documentation
Apache License 2.0
1.45k stars 120 forks source link

Generate a static property to create a responses without any parameters #656

Closed arthurcro closed 1 month ago

arthurcro commented 1 month ago

Motivation

Closes https://github.com/apple/swift-openapi-generator/issues/651. Today, some responses have no headers nor body (for example, 204 No Content).

In generated code it looks like this:

internal enum Output: Sendable, Hashable {
    internal struct NoContent: Sendable, Hashable {
        /// Creates a new `NoContent`.
        internal init() {}
    }
    case noContent(Operations.addPollAnswer.Output.NoContent)
}

And when writing a server handler, adopters have to spell it as:

return .noContent(.init())

The (.init()) bit is unnecessary, and we should make this common case prettier.

Modifications

Adds a static property to the generated Output enum for any response which does not have a header or a body and does not require a status code.

Result

The following is code is generated:

internal enum Output: Sendable, Hashable {
    internal struct NoContent: Sendable, Hashable {
        /// Creates a new `NoContent`.
        internal init() {}
    }
    case noContent(Operations.addPollAnswer.Output.NoContent)
    internal static var noContent: Self {
        .noContent(.init())
    }
}

Test Plan

Update FileBasedReferenceTests.swift to reflect the changes in the generated Types.swift.