Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 21, 2024

Conversation

arthurcro
Copy link
Contributor

@arthurcro arthurcro commented Oct 20, 2024

Motivation

Closes #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.

Copy link
Collaborator

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small suggestion, otherwise looks great!

Copy link
Collaborator

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@czechboy0 czechboy0 enabled auto-merge (squash) October 21, 2024 17:21
@czechboy0 czechboy0 merged commit 9727261 into apple:main Oct 21, 2024
26 checks passed
@czechboy0 czechboy0 added the semver/minor Adds new public API. label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate a convenience method to create a response case without any parameters
2 participants