[swift] Add Swift Vapor client library#8515
[swift] Add Swift Vapor client library#8515aymanbagabas wants to merge 6 commits intoOpenAPITools:masterfrom
Conversation
|
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
ad966c3 to
d19159b
Compare
|
Hi @aymanbagabas, Since Vapor is a server side framework, and this introduces a lot of changes, I think it makes more sence to separate this into a new generator. What do you think about this? I will comment on some changed on the code, to try to better understand some of the changes. |
4brunu
left a comment
There was a problem hiding this comment.
Hey, I just left a few comments, to try to understand the changes 🙂
There was a problem hiding this comment.
Hi, could you elabore on what's the use case for this default response?
There was a problem hiding this comment.
I needed a way to determine if operation responses contain a default response. The way I'm handling each operation return value is to return an enum based on the response status code.
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
List is not a Swift type. I had a problem while testing DocuSign specs where they define a type List and that got mapped to the wrong value Array which is a primitive type in swift.
There was a problem hiding this comment.
Can you elaborate more on this issue please? Perhaps show an example of the issue?
There was a problem hiding this comment.
I noticed that List is not a Swift nor OAS data type. I was working with DS specs and one of their models is named List, defined as below. I was facing a problem where the generator was naming this model Array instead of List.
...
"list": {
"type": "object",
"properties": {
"anchorAllowWhiteSpaceInCharacters": {
"description": "",
"type": "string"
},
"anchorAllowWhiteSpaceInCharactersMetadata": {
"$ref": "#/definitions/propertyMetadata",
"description": ""
},
"anchorCaseSensitive": {
...
There was a problem hiding this comment.
You are right, thanks for this fix and for the extra info 👍
There was a problem hiding this comment.
Could you explain this change please?
There was a problem hiding this comment.
Swift uses backticks to escape reserved words. Refer to the note section here
There was a problem hiding this comment.
This is a breaking change, that I suggested to open a new PR with, against the 6.0.x branch
There was a problem hiding this comment.
I noticed that this breaks objCompatible files, so I changed it to escape objCompatible reserved words with a _ and back-ticks otherwise.
There was a problem hiding this comment.
Could you explain this change please?
There was a problem hiding this comment.
Character uses quotes when initialized same as strings it needs to be surrounded by quotes " when defined as an enum.
There was a problem hiding this comment.
So this is a bug fix? It's not working in the current release?
There was a problem hiding this comment.
Commit dropped. I was trying something out and forgot to remove this line.
modules/openapi-generator/src/main/resources/swift5/APIs.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/swift5/Package.swift.mustache
Outdated
Show resolved
Hide resolved
|
Is this already working already? 🙂 |
There was a problem hiding this comment.
I needed a way to determine if operation responses contain a default response. The way I'm handling each operation return value is to return an enum based on the response status code.
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
List is not a Swift type. I had a problem while testing DocuSign specs where they define a type List and that got mapped to the wrong value Array which is a primitive type in swift.
There was a problem hiding this comment.
Swift uses backticks to escape reserved words. Refer to the note section here
There was a problem hiding this comment.
Character uses quotes when initialized same as strings it needs to be surrounded by quotes " when defined as an enum.
modules/openapi-generator/src/main/resources/swift5/APIs.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/swift5/Package.swift.mustache
Outdated
Show resolved
Hide resolved
While it will be cleaner, I don't see that as necessary since all the libraries (including this one) are client-side generators. And the language mapping is the same, the only difference I can see is that Vapor already implements all the boiler plate code needed for generating a client API, i.e. encoding, decoding, and other helpers are all built-in Vapor. What I would do is separate Vapor templates from other libraries' templates, I think that would be a good middle ground. What do you think? |
b9137f7 to
673a0fe
Compare
1886af8 to
2ae9e2b
Compare
From what I understand, this will be used to generate a client project, that will be used on a Vapor server, so that the Vapor server can invoke other API servers, is this correct or did I understand it wrong? The main blocker that I see in this PR, is that it has a lot of breaking changes. |
4brunu
left a comment
There was a problem hiding this comment.
I think I commented all the breaking changes
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Can you elaborate more on this issue please? Perhaps show an example of the issue?
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
So this is a bug fix? It's not working in the current release?
modules/openapi-generator/src/main/resources/swift5/APIs.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/swift5/Package.swift.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/swift5/Configuration.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/swift5/api.mustache
Outdated
Show resolved
Hide resolved
2d56a1a to
5ab962c
Compare
There was a problem hiding this comment.
@aymanbagabas I think the change of AnyType and object to JSON, could be applied to all libraries.
What do you think?
There was a problem hiding this comment.
I think that would be a great idea since JSON is Codable which can be decoded/encoded easily.
Thanks, I already reviewed it. |
There was a problem hiding this comment.
@aymanbagabas this should only be done when using classes, not structs.
There was a problem hiding this comment.
Codable can be applied to both AFAICT. I added encode(encoder:) and init(decoder:) explicitly to decode null values since you need to use encoderIfPresent() and decoderIfPresent() to capture nulls instead of ignoring the optionals all together.
There was a problem hiding this comment.
I know we can use explicitly encode(encoder:) to structs, but I think we should rely on the implicit implementation, because it's less code to maintain and one less source of bugs.
There was a problem hiding this comment.
As far as I now, the implicit implementation of encode(encoder:) already deals with the nulls, or not?
There was a problem hiding this comment.
I believe you're right. I meant to encode nullable variables. To do so, you gotta use encode(encoder:) instead of encodeIfPresent(encoder:) for optional values so that it encodes nulls. We really don't need init(decoder:) so I'm just gonna drop that one. Here's a good example of what I'm talking about https://stackoverflow.com/a/47268112/10913628.
These are the possible four cases of encoding. I'm using String as an example.
| isNullable | isRequired | type | func |
|---|---|---|---|
| true | true | String? | encode() |
| true | false | String? | encodeIfPresent() |
| false | true | String | encode() |
| false | false | String? | encodeIfPresent() |
My question is, does the generator always has isNullable and isRequired available? Because I know that nullable is an OAI 3.0 feature and it doesn't exist in either 2.0 nor 3.1.
There was a problem hiding this comment.
Since that's a question related to OAI, I don't know the answer.
But I think everything comes down to String? or String.
Looking at the first case, isNullable true, and isRequired true, I'm not sure if the func should be encode() or encodeIfPresent().
There was a problem hiding this comment.
If we want to encode null in the generated JSON, we have to use encode() instead of encodeIfPresent(). Note that encodeIfPresent() is the one used for optionals and that's the default implementation of func encode(to:).
There was a problem hiding this comment.
@4brunu I will make this change to encode nulls in the encoded JSONs. I'm also gonna start splitting this PR into smaller ones.
There was a problem hiding this comment.
Thanks for your effort in splitting this big PR into small ones, that will make it a lot easier to review each PR.
Some good candidates to a small PR are this one, Any to JSON, reserved word _ or `.
Ideally this PR end's up just having the Vapor changes.
72ed696 to
a1fd855
Compare
a1fd855 to
eebbaf9
Compare
82cd240 to
e7da117
Compare
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java
Outdated
Show resolved
Hide resolved
c5769d9 to
997268f
Compare
e0d09a2 to
8753fec
Compare
8753fec to
eb1cb63
Compare
eb1cb63 to
af4c108
Compare
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.For Windows users, please run the script in Git BASH.
master,5.1.x,6.0.x