Skip to content

Improve ergonomics when switching over Outputs #104

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

Closed
clarkkent-superman opened this issue Jul 7, 2023 · 4 comments · Fixed by #308
Closed

Improve ergonomics when switching over Outputs #104

clarkkent-superman opened this issue Jul 7, 2023 · 4 comments · Fixed by #308
Labels
area/generator Affects: plugin, CLI, config file. kind/usability Usability of generated code, ergonomics. status/needs-design Needs further discussion and a concrete proposal.
Milestone

Comments

@clarkkent-superman
Copy link

Getting down to the actual response model object requires drilling down through few layers of structs and enums:

switch result {
case let .ok(reponse):
    case let .json(payload):
        print(payload.body, payload.headers)
    @unknown default:
        break
    }
@unknown default:
    break
} 

I understand there is a requirement to handle multiple response types. If the payload could be flattened into just a body and headers, instead of a struct containing both of these, it would be possible to use the following syntax which would still support multiple response types.

switch result {
case let .ok(.json(body, headers):
  print(body, headers)
@unknown default:
    break
} 
@simonjbeaumont
Copy link
Collaborator

@clarkkent-superman thanks for filing this issue.

As you noted, the API of the generated code is designed to be as expressive as the OpenAPI specification allows. This includes multiple response bodies.

The spec for the response object specifies a response have headers and then a content map for various media types—i.e. the headers are shared across all possible content types for that response.

You're correct: it does result in a verbose API for simple operations that have a single content type. However, our goal was to generate code that would evolve in a sensible way as the API evolves.

For example, when an operation that has a single content type grows the ability to provide an additional body content type. The experience today is that clients will have another enum case to handle, which feels like an idiomatic mapping to Swift.

While we want to retain the expressive API layer, we are definitely interested in adding some conveniences/sugar APIs on top if/when they make sense, and we'd definitely welcome a contribution in this area. However, we'd need to have a well-thought-out design first. Issue #22 has some ideas, which might overlap with this issue.

Could you elaborate on the flattening you have in mind? Would we then need to duplicate the headers object into each of the response body type enum cases?

//cc @czechboy0

@czechboy0
Copy link
Contributor

Just to add to @simonjbeaumont - I think the generated base layer will continue to map the structure of OpenAPI (i.e., we likely won't duplicate the headers in every response enum case just to make the switching easier).

However, we're very open to adding sugar on top of the generated base layer. We'd like to see examples of what folks think it should look like, and we can take it from there.

Seeing specific ideas also helps us decide whether it should be part of the generated code, or if it should be a separate, e.g. macro package, that folks can explicitly opt-into to get the extra sugar, without growing the generated base layer.

A lot of questions, so we need to see some ideas 🙂

@czechboy0 czechboy0 added area/generator Affects: plugin, CLI, config file. status/needs-design Needs further discussion and a concrete proposal. kind/enhancement Improvements to existing feature. labels Jul 11, 2023
@czechboy0
Copy link
Contributor

Tagged as needing design – but feel free to add ideas in this issue until someone puts together a concrete proposal.

@PadraigK
Copy link

PadraigK commented Oct 3, 2023

❤️ Nice!

simonjbeaumont added a commit that referenced this issue Oct 3, 2023
### Motivation

We'd like to run a proposal for generating additional API to simplify
providing operation inputs and handling
operation outputs for simple API calls with just one successful outcome.

### Modifications

- Add SOAR-0007: Shorthand APIs for operation inputs and outputs

(See the proposal itself for details)[^1]

### Result

n/a

### Test Plan

n/a

### Related Issues

#22, #104, #105, #145

[^1]: https://github.com/apple/swift-openapi-generator/pull/291/files

---------

Signed-off-by: Si Beaumont <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/generator Affects: plugin, CLI, config file. kind/usability Usability of generated code, ergonomics. status/needs-design Needs further discussion and a concrete proposal.
Projects
None yet
4 participants