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 shorthand throwing APIs for providing inputs and handling outputs #308

Merged
merged 17 commits into from
Oct 3, 2023

Conversation

simonjbeaumont
Copy link
Collaborator

@simonjbeaumont simonjbeaumont commented Oct 2, 2023

Motivation

The review period for SOAR-0007 (Shorthand APIs for inputs and outputs) has now concluded. This pull request adds the required SPIs to the runtime library to throw a runtime error when the response and/or body does not match that of the shorthand API being used.

For further context, please review the proposal itself.1

Modifications

  • Add support for computed properties with effects (e.g. throwing getters).
  • Generate a protocol extension to APIProtocol with an overload for each operation that lifts each of the parameters of Input.init as function parameters.
  • Generate a throwing computed property for each enum case related to a documented outcome, which will return the associated value for the expected case, or throw a runtime error if the value is a different enum case.

Result

Code that used to be written like this

// before
switch try await client.getGreeting(.init()) {
case .ok(let response):
    switch response.body {
    case .json(let body):
        print(body.message)
    }
case .undocumented(statusCode: _, _):
    throw UnexpectedResponseError()
}

// after
print(try await client.getGreeting().ok.body.json.message)
//                     ^          ^  ^       ^
//                     |          |  |       `- (New) Throws if body did not conform to documented JSON.
//                     |          |  |
//                     |          |  `- (New) Throws if HTTP response is not 200 (OK).
//                     |          |
//                     |          `- (New) No need to wrap parameters in input value.
//                     |
//                     `- (Existing) Throws if there is an error making the API call.

Test Plan

This PR includes updates to the various tests:

  • SnippetBasedReferenceTests
  • FileBasedReferenceTests
  • PetstoreConsumerTests

Related Issues

Footnotes

  1. https://github.com/apple/swift-openapi-generator/pull/291

@simonjbeaumont simonjbeaumont force-pushed the sb/shorthand-apis-rebase branch from e83e207 to 51caeb0 Compare October 2, 2023 18:48
@simonjbeaumont simonjbeaumont changed the title Sb/shorthand apis rebase Generate shorthand throwing APIs for providing inputs and handling outputs Oct 3, 2023
@simonjbeaumont simonjbeaumont force-pushed the sb/shorthand-apis-rebase branch from 51caeb0 to 8cb80bb Compare October 3, 2023 10:25
@simonjbeaumont simonjbeaumont marked this pull request as ready for review October 3, 2023 10:28
Copy link
Contributor

@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.

A few suggestions, but no blockers so I'll let you make the final call on these.

@simonjbeaumont
Copy link
Collaborator Author

@swift-server-bot test this please

@simonjbeaumont simonjbeaumont enabled auto-merge (squash) October 3, 2023 13:56
@simonjbeaumont
Copy link
Collaborator Author

@swift-server-bot test this please

3 similar comments
@simonjbeaumont
Copy link
Collaborator Author

@swift-server-bot test this please

@simonjbeaumont
Copy link
Collaborator Author

@swift-server-bot test this please

@simonjbeaumont
Copy link
Collaborator Author

@swift-server-bot test this please

@simonjbeaumont simonjbeaumont merged commit a243417 into apple:main Oct 3, 2023
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
2 participants