apple / apple/swift-openapi-generator

Omit `throws` on `json` computed var when possible

Open
#710 2 comments 0 reactions 0 assignees View on GitHub
kind/feature status/triage
Dominant language
Swift
Stars
2k
Forks
182
Avg merge
13h 28m
Merged PRs (30d)
5

Description

### Motivation

Currently, a computed var `json` is generated for every REST response body. Unfortunately, this var is marked as `throws` even if `application/json` is the only content type defined for that response body. This is the case for 100% of our REST API methods, and I imagine is the most common case across all users of this library.

```swift
/// The associated value of the enum case if `self` is `.json`.
///
/// - Throws: An error if `self` is not `.json`.
/// - SeeAlso: `.json`.
public var json: Operations.postLinkedAccounts.Output.Conflict.Body.jsonPayload {
get throws {
switch self {
case let .json(body):
return body
}
}
}
```

This means that at the call site, I have to use `try!` to do something that is in reality perfectly safe:

```swift
case .conflict(let error): throw .conflict(try! error.body.json)
```

### Proposed solution

The generator should only add the `throws` annotation to the computed var `json` if in fact there are multiple cases/content types for that response body.

### Alternatives considered

_No response_

### Additional information

_No response_

Contributor guide

Open the contributing guide

Research direction

Search the generator for the computed `json` property and its `get throws` emission, then inspect tests covering response-body accessors. Reproduce the single-content-type case and compare it with a response having multiple content types; done means only the former omits `throws` and the existing generation tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, swift
Domain
api, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.