Skip to content

Commit 61ec6b6

Browse files
authored
Updated examples and integration test with the new servers syntax (#688)
### Motivation A drive-by fixup, I noticed the warnings when running the integration test locally. ### Modifications Fixed up the examples and integrate test to use the new syntax. This also meant regenerating the two examples that check in generated code, which took in a few more changes since 1.0. ### Result Syntax matches our current best practices. ### Test Plan Looks good locally.
1 parent 1524989 commit 61ec6b6

File tree

7 files changed

+77
-49
lines changed

7 files changed

+77
-49
lines changed

Examples/manual-generation-generator-cli-example/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# The following values can be changed here, or passed on the command line.
66
SWIFT_OPENAPI_GENERATOR_GIT_URL ?= https://github.com/apple/swift-openapi-generator
7-
SWIFT_OPENAPI_GENERATOR_GIT_TAG ?= 1.0.0
7+
SWIFT_OPENAPI_GENERATOR_GIT_TAG ?= 1.5.0
88
SWIFT_OPENAPI_GENERATOR_CLONE_DIR ?= $(CURRENT_MAKEFILE_DIR)/.swift-openapi-generator
99
SWIFT_OPENAPI_GENERATOR_BUILD_CONFIGURATION ?= debug
1010
OPENAPI_YAML_PATH ?= $(CURRENT_MAKEFILE_DIR)/openapi.yaml

Examples/manual-generation-generator-cli-example/Sources/ManualGeneratorInvocationClient/Generated/Client.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ internal struct Client: APIProtocol {
9393
default:
9494
return .undocumented(
9595
statusCode: response.status.code,
96-
.init()
96+
.init(
97+
headerFields: response.headerFields,
98+
body: responseBody
99+
)
97100
)
98101
}
99102
}

Examples/manual-generation-generator-cli-example/Sources/ManualGeneratorInvocationClient/Generated/Types.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ extension APIProtocol {
3434
/// Server URLs defined in the OpenAPI document.
3535
internal enum Servers {
3636
/// Example service deployment.
37+
internal enum Server1 {
38+
/// Example service deployment.
39+
internal static func url() throws -> Foundation.URL {
40+
try Foundation.URL(
41+
validatingOpenAPIServerURL: "https://example.com/api",
42+
variables: []
43+
)
44+
}
45+
}
46+
/// Example service deployment.
47+
@available(*, deprecated, renamed: "Servers.Server1.url")
3748
internal static func server1() throws -> Foundation.URL {
3849
try Foundation.URL(
3950
validatingOpenAPIServerURL: "https://example.com/api",
@@ -123,10 +134,10 @@ internal enum Operations {
123134
self.headers = headers
124135
}
125136
}
126-
@frozen internal enum Output: Sendable, Hashable {
137+
internal enum Output: Sendable, Hashable {
127138
internal struct Ok: Sendable, Hashable {
128139
/// - Remark: Generated from `#/paths/greet/GET/responses/200/content`.
129-
@frozen internal enum Body: Sendable, Hashable {
140+
internal enum Body: Sendable, Hashable {
130141
/// - Remark: Generated from `#/paths/greet/GET/responses/200/content/application\/json`.
131142
case json(Components.Schemas.Greeting)
132143
/// The associated value of the enum case if `self` is `.json`.
@@ -180,7 +191,7 @@ internal enum Operations {
180191
/// A response with a code that is not documented in the OpenAPI document.
181192
case undocumented(statusCode: Swift.Int, OpenAPIRuntime.UndocumentedPayload)
182193
}
183-
@frozen internal enum AcceptableContentType: AcceptableProtocol {
194+
internal enum AcceptableContentType: AcceptableProtocol {
184195
case json
185196
case other(Swift.String)
186197
internal init?(rawValue: Swift.String) {

Examples/manual-generation-package-plugin-example/Sources/CommandPluginInvocationClient/GeneratedSources/Client.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import struct Foundation.Data
1010
import struct Foundation.Date
1111
#endif
1212
import HTTPTypes
13-
package struct Client: APIProtocol {
13+
internal struct Client: APIProtocol {
1414
/// The underlying HTTP client.
1515
private let client: UniversalClient
1616
/// Creates a new client.
@@ -21,7 +21,7 @@ package struct Client: APIProtocol {
2121
/// - configuration: A set of configuration values for the client.
2222
/// - transport: A transport that performs HTTP operations.
2323
/// - middlewares: A list of middlewares to call before the transport.
24-
package init(
24+
internal init(
2525
serverURL: Foundation.URL,
2626
configuration: Configuration = .init(),
2727
transport: any ClientTransport,
@@ -39,7 +39,7 @@ package struct Client: APIProtocol {
3939
}
4040
/// - Remark: HTTP `GET /greet`.
4141
/// - Remark: Generated from `#/paths//greet/get(getGreeting)`.
42-
package func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
42+
internal func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
4343
try await client.send(
4444
input: input,
4545
forOperation: Operations.getGreeting.id,
@@ -93,7 +93,10 @@ package struct Client: APIProtocol {
9393
default:
9494
return .undocumented(
9595
statusCode: response.status.code,
96-
.init()
96+
.init(
97+
headerFields: response.headerFields,
98+
body: responseBody
99+
)
97100
)
98101
}
99102
}

Examples/manual-generation-package-plugin-example/Sources/CommandPluginInvocationClient/GeneratedSources/Types.swift

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import struct Foundation.Data
1010
import struct Foundation.Date
1111
#endif
1212
/// A type that performs HTTP operations defined by the OpenAPI document.
13-
package protocol APIProtocol: Sendable {
13+
internal protocol APIProtocol: Sendable {
1414
/// - Remark: HTTP `GET /greet`.
1515
/// - Remark: Generated from `#/paths//greet/get(getGreeting)`.
1616
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output
@@ -20,7 +20,7 @@ package protocol APIProtocol: Sendable {
2020
extension APIProtocol {
2121
/// - Remark: HTTP `GET /greet`.
2222
/// - Remark: Generated from `#/paths//greet/get(getGreeting)`.
23-
package func getGreeting(
23+
internal func getGreeting(
2424
query: Operations.getGreeting.Input.Query = .init(),
2525
headers: Operations.getGreeting.Input.Headers = .init()
2626
) async throws -> Operations.getGreeting.Output {
@@ -32,9 +32,20 @@ extension APIProtocol {
3232
}
3333

3434
/// Server URLs defined in the OpenAPI document.
35-
package enum Servers {
35+
internal enum Servers {
3636
/// Example service deployment.
37-
package static func server1() throws -> Foundation.URL {
37+
internal enum Server1 {
38+
/// Example service deployment.
39+
internal static func url() throws -> Foundation.URL {
40+
try Foundation.URL(
41+
validatingOpenAPIServerURL: "https://example.com/api",
42+
variables: []
43+
)
44+
}
45+
}
46+
/// Example service deployment.
47+
@available(*, deprecated, renamed: "Servers.Server1.url")
48+
internal static func server1() throws -> Foundation.URL {
3849
try Foundation.URL(
3950
validatingOpenAPIServerURL: "https://example.com/api",
4051
variables: []
@@ -43,97 +54,97 @@ package enum Servers {
4354
}
4455

4556
/// Types generated from the components section of the OpenAPI document.
46-
package enum Components {
57+
internal enum Components {
4758
/// Types generated from the `#/components/schemas` section of the OpenAPI document.
48-
package enum Schemas {
59+
internal enum Schemas {
4960
/// A value with the greeting contents.
5061
///
5162
/// - Remark: Generated from `#/components/schemas/Greeting`.
52-
package struct Greeting: Codable, Hashable, Sendable {
63+
internal struct Greeting: Codable, Hashable, Sendable {
5364
/// The string representation of the greeting.
5465
///
5566
/// - Remark: Generated from `#/components/schemas/Greeting/message`.
56-
package var message: Swift.String
67+
internal var message: Swift.String
5768
/// Creates a new `Greeting`.
5869
///
5970
/// - Parameters:
6071
/// - message: The string representation of the greeting.
61-
package init(message: Swift.String) {
72+
internal init(message: Swift.String) {
6273
self.message = message
6374
}
64-
package enum CodingKeys: String, CodingKey {
75+
internal enum CodingKeys: String, CodingKey {
6576
case message
6677
}
6778
}
6879
}
6980
/// Types generated from the `#/components/parameters` section of the OpenAPI document.
70-
package enum Parameters {}
81+
internal enum Parameters {}
7182
/// Types generated from the `#/components/requestBodies` section of the OpenAPI document.
72-
package enum RequestBodies {}
83+
internal enum RequestBodies {}
7384
/// Types generated from the `#/components/responses` section of the OpenAPI document.
74-
package enum Responses {}
85+
internal enum Responses {}
7586
/// Types generated from the `#/components/headers` section of the OpenAPI document.
76-
package enum Headers {}
87+
internal enum Headers {}
7788
}
7889

7990
/// API operations, with input and output types, generated from `#/paths` in the OpenAPI document.
80-
package enum Operations {
91+
internal enum Operations {
8192
/// - Remark: HTTP `GET /greet`.
8293
/// - Remark: Generated from `#/paths//greet/get(getGreeting)`.
83-
package enum getGreeting {
84-
package static let id: Swift.String = "getGreeting"
85-
package struct Input: Sendable, Hashable {
94+
internal enum getGreeting {
95+
internal static let id: Swift.String = "getGreeting"
96+
internal struct Input: Sendable, Hashable {
8697
/// - Remark: Generated from `#/paths/greet/GET/query`.
87-
package struct Query: Sendable, Hashable {
98+
internal struct Query: Sendable, Hashable {
8899
/// The name used in the returned greeting.
89100
///
90101
/// - Remark: Generated from `#/paths/greet/GET/query/name`.
91-
package var name: Swift.String?
102+
internal var name: Swift.String?
92103
/// Creates a new `Query`.
93104
///
94105
/// - Parameters:
95106
/// - name: The name used in the returned greeting.
96-
package init(name: Swift.String? = nil) {
107+
internal init(name: Swift.String? = nil) {
97108
self.name = name
98109
}
99110
}
100-
package var query: Operations.getGreeting.Input.Query
111+
internal var query: Operations.getGreeting.Input.Query
101112
/// - Remark: Generated from `#/paths/greet/GET/header`.
102-
package struct Headers: Sendable, Hashable {
103-
package var accept: [OpenAPIRuntime.AcceptHeaderContentType<Operations.getGreeting.AcceptableContentType>]
113+
internal struct Headers: Sendable, Hashable {
114+
internal var accept: [OpenAPIRuntime.AcceptHeaderContentType<Operations.getGreeting.AcceptableContentType>]
104115
/// Creates a new `Headers`.
105116
///
106117
/// - Parameters:
107118
/// - accept:
108-
package init(accept: [OpenAPIRuntime.AcceptHeaderContentType<Operations.getGreeting.AcceptableContentType>] = .defaultValues()) {
119+
internal init(accept: [OpenAPIRuntime.AcceptHeaderContentType<Operations.getGreeting.AcceptableContentType>] = .defaultValues()) {
109120
self.accept = accept
110121
}
111122
}
112-
package var headers: Operations.getGreeting.Input.Headers
123+
internal var headers: Operations.getGreeting.Input.Headers
113124
/// Creates a new `Input`.
114125
///
115126
/// - Parameters:
116127
/// - query:
117128
/// - headers:
118-
package init(
129+
internal init(
119130
query: Operations.getGreeting.Input.Query = .init(),
120131
headers: Operations.getGreeting.Input.Headers = .init()
121132
) {
122133
self.query = query
123134
self.headers = headers
124135
}
125136
}
126-
@frozen package enum Output: Sendable, Hashable {
127-
package struct Ok: Sendable, Hashable {
137+
internal enum Output: Sendable, Hashable {
138+
internal struct Ok: Sendable, Hashable {
128139
/// - Remark: Generated from `#/paths/greet/GET/responses/200/content`.
129-
@frozen package enum Body: Sendable, Hashable {
140+
internal enum Body: Sendable, Hashable {
130141
/// - Remark: Generated from `#/paths/greet/GET/responses/200/content/application\/json`.
131142
case json(Components.Schemas.Greeting)
132143
/// The associated value of the enum case if `self` is `.json`.
133144
///
134145
/// - Throws: An error if `self` is not `.json`.
135146
/// - SeeAlso: `.json`.
136-
package var json: Components.Schemas.Greeting {
147+
internal var json: Components.Schemas.Greeting {
137148
get throws {
138149
switch self {
139150
case let .json(body):
@@ -143,12 +154,12 @@ package enum Operations {
143154
}
144155
}
145156
/// Received HTTP response body
146-
package var body: Operations.getGreeting.Output.Ok.Body
157+
internal var body: Operations.getGreeting.Output.Ok.Body
147158
/// Creates a new `Ok`.
148159
///
149160
/// - Parameters:
150161
/// - body: Received HTTP response body
151-
package init(body: Operations.getGreeting.Output.Ok.Body) {
162+
internal init(body: Operations.getGreeting.Output.Ok.Body) {
152163
self.body = body
153164
}
154165
}
@@ -162,7 +173,7 @@ package enum Operations {
162173
///
163174
/// - Throws: An error if `self` is not `.ok`.
164175
/// - SeeAlso: `.ok`.
165-
package var ok: Operations.getGreeting.Output.Ok {
176+
internal var ok: Operations.getGreeting.Output.Ok {
166177
get throws {
167178
switch self {
168179
case let .ok(response):
@@ -180,26 +191,26 @@ package enum Operations {
180191
/// A response with a code that is not documented in the OpenAPI document.
181192
case undocumented(statusCode: Swift.Int, OpenAPIRuntime.UndocumentedPayload)
182193
}
183-
@frozen package enum AcceptableContentType: AcceptableProtocol {
194+
internal enum AcceptableContentType: AcceptableProtocol {
184195
case json
185196
case other(Swift.String)
186-
package init?(rawValue: Swift.String) {
197+
internal init?(rawValue: Swift.String) {
187198
switch rawValue.lowercased() {
188199
case "application/json":
189200
self = .json
190201
default:
191202
self = .other(rawValue)
192203
}
193204
}
194-
package var rawValue: Swift.String {
205+
internal var rawValue: Swift.String {
195206
switch self {
196207
case let .other(string):
197208
return string
198209
case .json:
199210
return "application/json"
200211
}
201212
}
202-
package static var allCases: [Self] {
213+
internal static var allCases: [Self] {
203214
[
204215
.json
205216
]

IntegrationTest/Sources/MockTransportClient/Client.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ struct MockClientTransport: ClientTransport {
2424
}
2525

2626
func run() async throws {
27-
let client = Client(serverURL: try Servers.server1(), transport: MockClientTransport())
27+
let client = Client(serverURL: try Servers.Server1.url(), transport: MockClientTransport())
2828
_ = try await client.getGreeting(.init())
2929
}

IntegrationTest/Sources/MockTransportServer/Server.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ class MockServerTransport: ServerTransport {
3636
func initializeServer() throws {
3737
let handler = SimpleAPIImpl()
3838
let transport = MockServerTransport()
39-
try handler.registerHandlers(on: transport, serverURL: Servers.server1())
39+
try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url())
4040
}

0 commit comments

Comments
 (0)