mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-18 22:57:07 +00:00
[swift6] promote to beta and improve documentation (#19856)
* [swift6] promote to beta * [swift6] format code * [swift] authentication docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs * [swift] update docs
This commit is contained in:
@@ -35,7 +35,6 @@ open class AnotherFakeAPI {
|
||||
- PATCH /another-fake/dummy
|
||||
- To test special tags and operation ID starting with number
|
||||
- parameter body: (body) client model
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Client>
|
||||
*/
|
||||
|
||||
@@ -33,7 +33,6 @@ open class FakeAPI {
|
||||
- POST /fake/outer/boolean
|
||||
- Test serialization of outer boolean types
|
||||
- parameter body: (body) Input boolean as post body (optional)
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Bool>
|
||||
*/
|
||||
@@ -78,7 +77,6 @@ open class FakeAPI {
|
||||
- POST /fake/outer/composite
|
||||
- Test serialization of object with outer number type
|
||||
- parameter body: (body) Input composite as post body (optional)
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<OuterComposite>
|
||||
*/
|
||||
@@ -123,7 +121,6 @@ open class FakeAPI {
|
||||
- POST /fake/outer/number
|
||||
- Test serialization of outer number types
|
||||
- parameter body: (body) Input number as post body (optional)
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Double>
|
||||
*/
|
||||
@@ -168,7 +165,6 @@ open class FakeAPI {
|
||||
- POST /fake/outer/string
|
||||
- Test serialization of outer string types
|
||||
- parameter body: (body) Input string as post body (optional)
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<String>
|
||||
*/
|
||||
@@ -213,7 +209,6 @@ open class FakeAPI {
|
||||
- PUT /fake/body-with-file-schema
|
||||
- For this test, the body for this request much reference a schema named `File`.
|
||||
- parameter body: (body)
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -258,8 +253,7 @@ open class FakeAPI {
|
||||
/**
|
||||
- PUT /fake/body-with-query-params
|
||||
- parameter query: (query)
|
||||
- parameter body: (body)
|
||||
|
||||
- parameter body: (body)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -309,7 +303,6 @@ open class FakeAPI {
|
||||
- PATCH /fake
|
||||
- To test \"client\" model
|
||||
- parameter body: (body) client model
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Client>
|
||||
*/
|
||||
@@ -372,20 +365,19 @@ open class FakeAPI {
|
||||
- type: http
|
||||
- name: http_basic_test
|
||||
- parameter number: (form) None
|
||||
- parameter double: (form) None
|
||||
- parameter patternWithoutDelimiter: (form) None
|
||||
- parameter byte: (form) None
|
||||
- parameter integer: (form) None (optional)
|
||||
- parameter int32: (form) None (optional)
|
||||
- parameter int64: (form) None (optional)
|
||||
- parameter float: (form) None (optional)
|
||||
- parameter string: (form) None (optional)
|
||||
- parameter binary: (form) None (optional)
|
||||
- parameter date: (form) None (optional)
|
||||
- parameter dateTime: (form) None (optional)
|
||||
- parameter password: (form) None (optional)
|
||||
- parameter callback: (form) None (optional)
|
||||
|
||||
- parameter double: (form) None
|
||||
- parameter patternWithoutDelimiter: (form) None
|
||||
- parameter byte: (form) None
|
||||
- parameter integer: (form) None (optional)
|
||||
- parameter int32: (form) None (optional)
|
||||
- parameter int64: (form) None (optional)
|
||||
- parameter float: (form) None (optional)
|
||||
- parameter string: (form) None (optional)
|
||||
- parameter binary: (form) None (optional)
|
||||
- parameter date: (form) None (optional)
|
||||
- parameter dateTime: (form) None (optional)
|
||||
- parameter password: (form) None (optional)
|
||||
- parameter callback: (form) None (optional)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -524,14 +516,13 @@ open class FakeAPI {
|
||||
- GET /fake
|
||||
- To test enum parameters
|
||||
- parameter enumHeaderStringArray: (header) Header parameter enum test (string array) (optional)
|
||||
- parameter enumHeaderString: (header) Header parameter enum test (string) (optional, default to .efg)
|
||||
- parameter enumQueryStringArray: (query) Query parameter enum test (string array) (optional)
|
||||
- parameter enumQueryString: (query) Query parameter enum test (string) (optional, default to .efg)
|
||||
- parameter enumQueryInteger: (query) Query parameter enum test (double) (optional)
|
||||
- parameter enumQueryDouble: (query) Query parameter enum test (double) (optional)
|
||||
- parameter enumFormStringArray: (form) Form parameter enum test (string array) (optional, default to .dollar)
|
||||
- parameter enumFormString: (form) Form parameter enum test (string) (optional, default to .efg)
|
||||
|
||||
- parameter enumHeaderString: (header) Header parameter enum test (string) (optional, default to .efg)
|
||||
- parameter enumQueryStringArray: (query) Query parameter enum test (string array) (optional)
|
||||
- parameter enumQueryString: (query) Query parameter enum test (string) (optional, default to .efg)
|
||||
- parameter enumQueryInteger: (query) Query parameter enum test (double) (optional)
|
||||
- parameter enumQueryDouble: (query) Query parameter enum test (double) (optional)
|
||||
- parameter enumFormStringArray: (form) Form parameter enum test (string array) (optional, default to .dollar)
|
||||
- parameter enumFormString: (form) Form parameter enum test (string) (optional, default to .efg)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -597,12 +588,11 @@ open class FakeAPI {
|
||||
- DELETE /fake
|
||||
- Fake endpoint to test group parameters (optional)
|
||||
- parameter requiredStringGroup: (query) Required String in group parameters
|
||||
- parameter requiredBooleanGroup: (header) Required Boolean in group parameters
|
||||
- parameter requiredInt64Group: (query) Required Integer in group parameters
|
||||
- parameter stringGroup: (query) String in group parameters (optional)
|
||||
- parameter booleanGroup: (header) Boolean in group parameters (optional)
|
||||
- parameter int64Group: (query) Integer in group parameters (optional)
|
||||
|
||||
- parameter requiredBooleanGroup: (header) Required Boolean in group parameters
|
||||
- parameter requiredInt64Group: (query) Required Integer in group parameters
|
||||
- parameter stringGroup: (query) String in group parameters (optional)
|
||||
- parameter booleanGroup: (header) Boolean in group parameters (optional)
|
||||
- parameter int64Group: (query) Integer in group parameters (optional)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -655,7 +645,6 @@ open class FakeAPI {
|
||||
test inline additionalProperties
|
||||
- POST /fake/inline-additionalProperties
|
||||
- parameter param: (body) request body
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -702,8 +691,7 @@ open class FakeAPI {
|
||||
test json serialization of form data
|
||||
- GET /fake/jsonFormData
|
||||
- parameter param: (form) field1
|
||||
- parameter param2: (form) field2
|
||||
|
||||
- parameter param2: (form) field2
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
|
||||
@@ -38,7 +38,6 @@ open class FakeClassnameTags123API {
|
||||
- type: apiKey api_key_query (QUERY)
|
||||
- name: api_key_query
|
||||
- parameter body: (body) client model
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Client>
|
||||
*/
|
||||
|
||||
@@ -40,7 +40,6 @@ open class PetAPI {
|
||||
- type: apiKey api_key_query (QUERY)
|
||||
- name: api_key_query
|
||||
- parameter body: (body) Pet object that needs to be added to the store
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -90,8 +89,7 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter petId: (path) Pet id to delete
|
||||
- parameter apiKey: (header) (optional)
|
||||
|
||||
- parameter apiKey: (header) (optional)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -153,7 +151,6 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter status: (query) Status values that need to be considered for filter
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<[Pet]>
|
||||
*/
|
||||
@@ -207,7 +204,6 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter tags: (query) Tags to filter by
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<[Pet]>
|
||||
*/
|
||||
@@ -261,7 +257,6 @@ open class PetAPI {
|
||||
- type: apiKey api_key (HEADER)
|
||||
- name: api_key
|
||||
- parameter petId: (path) ID of pet to return
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Pet>
|
||||
*/
|
||||
@@ -313,7 +308,6 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter body: (body) Pet object that needs to be added to the store
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -364,9 +358,8 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter petId: (path) ID of pet that needs to be updated
|
||||
- parameter name: (form) Updated name of the pet (optional)
|
||||
- parameter status: (form) Updated status of the pet (optional)
|
||||
|
||||
- parameter name: (form) Updated name of the pet (optional)
|
||||
- parameter status: (form) Updated status of the pet (optional)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -426,9 +419,8 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter petId: (path) ID of pet to update
|
||||
- parameter additionalMetadata: (form) Additional data to pass to server (optional)
|
||||
- parameter file: (form) file to upload (optional)
|
||||
|
||||
- parameter additionalMetadata: (form) Additional data to pass to server (optional)
|
||||
- parameter file: (form) file to upload (optional)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<ApiResponse>
|
||||
*/
|
||||
@@ -488,9 +480,8 @@ open class PetAPI {
|
||||
- type: oauth2
|
||||
- name: petstore_auth
|
||||
- parameter petId: (path) ID of pet to update
|
||||
- parameter requiredFile: (form) file to upload
|
||||
- parameter additionalMetadata: (form) Additional data to pass to server (optional)
|
||||
|
||||
- parameter requiredFile: (form) file to upload
|
||||
- parameter additionalMetadata: (form) Additional data to pass to server (optional)
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<ApiResponse>
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,6 @@ open class StoreAPI {
|
||||
- DELETE /store/order/{order_id}
|
||||
- For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||
- parameter orderId: (path) ID of the order that needs to be deleted
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -86,7 +85,6 @@ open class StoreAPI {
|
||||
- API Key:
|
||||
- type: apiKey api_key (HEADER)
|
||||
- name: api_key
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<[String: Int]>
|
||||
*/
|
||||
@@ -133,7 +131,6 @@ open class StoreAPI {
|
||||
- GET /store/order/{order_id}
|
||||
- For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions
|
||||
- parameter orderId: (path) ID of pet that needs to be fetched
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Order>
|
||||
*/
|
||||
@@ -182,7 +179,6 @@ open class StoreAPI {
|
||||
Place an order for a pet
|
||||
- POST /store/order
|
||||
- parameter body: (body) order placed for purchasing the pet
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Order>
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,6 @@ open class UserAPI {
|
||||
- POST /user
|
||||
- This can only be done by the logged in user.
|
||||
- parameter body: (body) Created user object
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -81,7 +80,6 @@ open class UserAPI {
|
||||
Creates list of users with given input array
|
||||
- POST /user/createWithArray
|
||||
- parameter body: (body) List of user object
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -127,7 +125,6 @@ open class UserAPI {
|
||||
Creates list of users with given input array
|
||||
- POST /user/createWithList
|
||||
- parameter body: (body) List of user object
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -174,7 +171,6 @@ open class UserAPI {
|
||||
- DELETE /user/{username}
|
||||
- This can only be done by the logged in user.
|
||||
- parameter username: (path) The name that needs to be deleted
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -223,7 +219,6 @@ open class UserAPI {
|
||||
Get user by user name
|
||||
- GET /user/{username}
|
||||
- parameter username: (path) The name that needs to be fetched. Use user1 for testing.
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<User>
|
||||
*/
|
||||
@@ -274,8 +269,7 @@ open class UserAPI {
|
||||
- GET /user/login
|
||||
- responseHeaders: [X-Rate-Limit(Int), X-Expires-After(Date)]
|
||||
- parameter username: (query) The user name for login
|
||||
- parameter password: (query) The password for login in clear text
|
||||
|
||||
- parameter password: (query) The password for login in clear text
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<String>
|
||||
*/
|
||||
@@ -323,7 +317,6 @@ open class UserAPI {
|
||||
/**
|
||||
Logs out current logged in user session
|
||||
- GET /user/logout
|
||||
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
@@ -371,8 +364,7 @@ open class UserAPI {
|
||||
- PUT /user/{username}
|
||||
- This can only be done by the logged in user.
|
||||
- parameter username: (path) name that need to be deleted
|
||||
- parameter body: (body) Updated user object
|
||||
|
||||
- parameter body: (body) Updated user object
|
||||
- parameter openAPIClient: The OpenAPIClient that contains the configuration for the http request.
|
||||
- returns: RequestBuilder<Void>
|
||||
*/
|
||||
|
||||
@@ -140,6 +140,89 @@ Authentication schemes defined for the API:
|
||||
- **Type**: HTTP basic authentication
|
||||
|
||||
|
||||
### How do I implement bearer token authentication with URLSession on the Swift 6 API client?
|
||||
|
||||
First you implement the `OpenAPIInterceptor` protocol.
|
||||
```
|
||||
public class BearerOpenAPIInterceptor: OpenAPIInterceptor {
|
||||
public init() {}
|
||||
|
||||
public func intercept(urlRequest: URLRequest, urlSession: URLSessionProtocol, openAPIClient: OpenAPIClient, completion: @escaping (Result<URLRequest, any Error>) -> Void) {
|
||||
refreshTokenIfDoesntExist { token in
|
||||
|
||||
// Change the current url request
|
||||
var newUrlRequest = urlRequest
|
||||
newUrlRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||
|
||||
// Change the global headers
|
||||
openAPIClient.customHeaders["Authorization"] = "Bearer \(token)"
|
||||
|
||||
completion(.success(newUrlRequest))
|
||||
}
|
||||
}
|
||||
|
||||
public func retry(urlRequest: URLRequest, urlSession: URLSessionProtocol, openAPIClient: OpenAPIClient, data: Data?, response: URLResponse, error: Error, completion: @escaping (OpenAPIInterceptorRetry) -> Void) {
|
||||
// We will analyse the response to see if it's a 401, and if it's a 401, we will refresh the token and retry the request
|
||||
refreshTokenIfUnauthorizedRequestResponse(
|
||||
data: data,
|
||||
response: response,
|
||||
error: error
|
||||
) { (wasTokenRefreshed, newToken) in
|
||||
|
||||
if wasTokenRefreshed, let newToken = newToken {
|
||||
|
||||
// Change the global headers
|
||||
openAPIClient.customHeaders["Authorization"] = "Bearer \(newToken)"
|
||||
|
||||
completion(.retry)
|
||||
} else {
|
||||
// If the token was not refreshed, it's because it was not a 401 error, so we send the response to the completion block
|
||||
completion(.dontRetry)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var bearerToken: String? = nil
|
||||
|
||||
func refreshTokenIfDoesntExist(completionHandler: @escaping (String) -> Void) {
|
||||
if let bearerToken = bearerToken {
|
||||
completionHandler(bearerToken)
|
||||
} else {
|
||||
startRefreshingToken { token in
|
||||
completionHandler(token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func refreshTokenIfUnauthorizedRequestResponse(data: Data?, response: URLResponse, error: Error, completionHandler: @escaping (Bool, String?) -> Void) {
|
||||
if let response = response as? HTTPURLResponse, response.statusCode == 401 {
|
||||
startRefreshingToken { token in
|
||||
completionHandler(true, token)
|
||||
}
|
||||
} else {
|
||||
completionHandler(false, nil)
|
||||
}
|
||||
}
|
||||
|
||||
private func startRefreshingToken(completionHandler: @escaping (String) -> Void) {
|
||||
// Get a bearer token
|
||||
let dummyBearerToken = "..."
|
||||
|
||||
bearerToken = dummyBearerToken
|
||||
|
||||
completionHandler(dummyBearerToken)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then you assign the `BearerOpenAPIInterceptor` to the property `OpenAPIClient.shared.interceptor`.
|
||||
|
||||
`OpenAPIClient.shared.interceptor = BearerOpenAPIInterceptor()`
|
||||
|
||||
Here is a working sample that put's together all of this.
|
||||
[AppDelegate.swift](https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/swift6/urlsessionLibrary/SwaggerClientTests/SwaggerClient/AppDelegate.swift)
|
||||
[BearerTokenHandler.swift](https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/swift5/urlsessionLibrary/SwaggerClientTests/SwaggerClient/BearerDecodableRequestBuilder.swift)
|
||||
|
||||
## Author
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user