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

Return type conversion #663

Closed alteredtech closed 3 weeks ago

alteredtech commented 3 weeks ago

Question

I am create a client for some things at work and personal. Something I have not been able to find in any documentation is if we are meant to create our own return type models.

I was reading this and its what I am doing.

How I thought it was meant to be, just return the result.body.json type. However, when testing, while this can sometimes work. It will often result in a <<error type>> in xcode.

Is there a piece I am missing or are we meant to convert the generated type to our own for a curated client library?

For either case, if it is not too much work for myself after I understand the process, I could add it as an example to the repo for others.

czechboy0 commented 3 weeks ago

Hi @alteredtech,

I'm not sure that I understand the question, but check out the many examples in https://github.com/apple/swift-openapi-generator/blob/main/Examples

Browse and play with them, and if they still don't unblock you, please try to add more details to your question by describing what you're missing on one of those sample projects. Thanks!

alteredtech commented 3 weeks ago

Not sure how I over looked it but the curated example already there, it was close but was only returning a type String. Which is leading me to an answer if I am correct. Essentially I have a openapi generated client that I am curating. It has an endpoint of getting a device from an id. That has this openapi type and response. Components.Schemas.Device

{
"deviceId": 123-asd-234,
"ipAddress": 10.0.0.1,
"macAddress": 12:23:34:ab:cd,
"hostname": example
}

This return would be however the generator would format Components.Schemas.Devices from this example function in the curated client.

public func getDevice(id: String) async throws -> Components.Schemas.Device {
... code here ...
return try result.body.json
}

Some times when I then try to use this function, the function says it has a <<error type>> even when I build the project, clear cache, quit xcode, restart my computer.

Is it proper practice to reconvert the json response to a model like so?

struct Device {
    let deviceId: String
    let ipAddress: String
    let macAddress: String
    let hostname: String
}

This doesnt feel like the right way but I am unsure how to prevent the issue with return type being an error.

alteredtech commented 3 weeks ago
IMG_2913

This is what I see in my editor for this curated client.

czechboy0 commented 3 weeks ago

In the curated example, you're not meant to return any generated types. The idea is that the generated code is used as an implementation detail only, and that any types you want to use as input/output of the public API, you hand write. And then you'd manually map the values between your hand-written types and the generated types.

That's also why the default accessModifier is internal, to avoid accidentally leaking the generated types.

Regarding the autocompletion, that's not something that this package has control over, please file any Xcode issues on http://feedbackassistant.apple.com, thanks!

alteredtech commented 3 weeks ago

Perfect. That is exactly what I was thinking it would have to be after some more testing. Glad to have it confirmed.

The autocompletion, I dont believe is an error with xcode since I am returning the generated types. That would be a me issue.

I will add to my todo list to see about slightly expanding the curated example so it shows it with a specified return model if that is alright?

czechboy0 commented 3 weeks ago

I will add to my todo list to see about slightly expanding the curated example so it shows it with a specified return model if that is alright?

That sounds great, thank you @alteredtech!