Skip to content

Commit f048771

Browse files
Generate @Frozen enums for operation outputs and request/response bodies (#109)
### Motivation The generated code contains several enum types. A lot of these are just namespaces (i.e. the enum has no cases), but some are "proper" enums that we expect adopters to switch over. For example the operation output, and the request and response bodies. To make using these types more ergonomic, we can annotate them as `@frozen`. This is something we already do for other generated enum types (e.g. OneOf and enum types declared in the OpenAPI document). ### Modifications - Add desired `@frozen` attributes to reference code - Generate `@frozen` enum for request bodies - Generate `@frozen` enum for response bodies - Generate `@frozen` enum for operation outputs ### Result Switching over operation outputs and request/response bodies is simpler. ### Resolves - Resolves #105. ### Test Plan Reference test updated. --------- Signed-off-by: Si Beaumont <[email protected]>
1 parent 23b982d commit f048771

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

Sources/_OpenAPIGeneratorCore/Translator/RequestBody/translateRequestBody.swift

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ extension TypesFileTranslator {
150150
members: [Declaration]
151151
) -> Declaration {
152152
let bodyEnumDecl: Declaration = .enum(
153+
isFrozen: true,
153154
accessModifier: config.access,
154155
name: typeName.shortSwiftName,
155156
conformances: Constants.Operation.Output.conformances,

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponse.swift

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ extension TypesFileTranslator {
8383
}
8484
let hasNoContent: Bool = bodyCases.isEmpty
8585
let contentEnumDecl: Declaration = .enum(
86+
isFrozen: true,
8687
accessModifier: config.access,
8788
name: bodyTypeName.shortSwiftName,
8889
conformances: Constants.Operation.Body.conformances,

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateOperations.swift

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ extension TypesFileTranslator {
161161
}
162162

163163
let enumDecl: Declaration = .enum(
164+
isFrozen: true,
164165
accessModifier: config.access,
165166
name: outputTypeName.shortSwiftName,
166167
conformances: Constants.Operation.Output.conformances,

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift

+22-20
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ public enum Components {
616616
}
617617
/// Types generated from the `#/components/requestBodies` section of the OpenAPI document.
618618
public enum RequestBodies {
619-
public enum UpdatePetRequest: Sendable, Equatable, Hashable {
619+
@frozen public enum UpdatePetRequest: Sendable, Equatable, Hashable {
620620
/// - Remark: Generated from `#/components/requestBodies/UpdatePetRequest/json`.
621621
public struct jsonPayload: Codable, Equatable, Hashable, Sendable {
622622
/// - Remark: Generated from `#/components/requestBodies/UpdatePetRequest/json/name`.
@@ -662,7 +662,7 @@ public enum Components {
662662
}
663663
/// Received HTTP response headers
664664
public var headers: Components.Responses.ErrorBadRequest.Headers
665-
public enum Body: Sendable, Equatable, Hashable {
665+
@frozen public enum Body: Sendable, Equatable, Hashable {
666666
/// - Remark: Generated from `#/components/responses/ErrorBadRequest/json`.
667667
public struct jsonPayload: Codable, Equatable, Hashable, Sendable {
668668
/// - Remark: Generated from `#/components/responses/ErrorBadRequest/json/code`.
@@ -819,7 +819,7 @@ public enum Operations {
819819
public init() {}
820820
}
821821
public var cookies: Operations.listPets.Input.Cookies
822-
public enum Body: Sendable, Equatable, Hashable {}
822+
@frozen public enum Body: Sendable, Equatable, Hashable {}
823823
public var body: Operations.listPets.Input.Body?
824824
/// Creates a new `Input`.
825825
///
@@ -843,7 +843,7 @@ public enum Operations {
843843
self.body = body
844844
}
845845
}
846-
public enum Output: Sendable, Equatable, Hashable {
846+
@frozen public enum Output: Sendable, Equatable, Hashable {
847847
public struct Ok: Sendable, Equatable, Hashable {
848848
public struct Headers: Sendable, Equatable, Hashable {
849849
public var My_Response_UUID: Swift.String
@@ -863,7 +863,7 @@ public enum Operations {
863863
}
864864
/// Received HTTP response headers
865865
public var headers: Operations.listPets.Output.Ok.Headers
866-
public enum Body: Sendable, Equatable, Hashable {
866+
@frozen public enum Body: Sendable, Equatable, Hashable {
867867
case json(Components.Schemas.Pets)
868868
}
869869
/// Received HTTP response body
@@ -894,7 +894,7 @@ public enum Operations {
894894
}
895895
/// Received HTTP response headers
896896
public var headers: Operations.listPets.Output.Default.Headers
897-
public enum Body: Sendable, Equatable, Hashable {
897+
@frozen public enum Body: Sendable, Equatable, Hashable {
898898
case json(Components.Schemas._Error)
899899
}
900900
/// Received HTTP response body
@@ -953,7 +953,7 @@ public enum Operations {
953953
public init() {}
954954
}
955955
public var cookies: Operations.createPet.Input.Cookies
956-
public enum Body: Sendable, Equatable, Hashable {
956+
@frozen public enum Body: Sendable, Equatable, Hashable {
957957
case json(Components.Schemas.CreatePetRequest)
958958
}
959959
public var body: Operations.createPet.Input.Body
@@ -979,7 +979,7 @@ public enum Operations {
979979
self.body = body
980980
}
981981
}
982-
public enum Output: Sendable, Equatable, Hashable {
982+
@frozen public enum Output: Sendable, Equatable, Hashable {
983983
public struct Created: Sendable, Equatable, Hashable {
984984
public struct Headers: Sendable, Equatable, Hashable {
985985
public var X_Extra_Arguments: Components.Schemas.CodeError?
@@ -993,7 +993,7 @@ public enum Operations {
993993
}
994994
/// Received HTTP response headers
995995
public var headers: Operations.createPet.Output.Created.Headers
996-
public enum Body: Sendable, Equatable, Hashable {
996+
@frozen public enum Body: Sendable, Equatable, Hashable {
997997
case json(Components.Schemas.Pet)
998998
}
999999
/// Received HTTP response body
@@ -1054,7 +1054,7 @@ public enum Operations {
10541054
public init() {}
10551055
}
10561056
public var cookies: Operations.probe.Input.Cookies
1057-
public enum Body: Sendable, Equatable, Hashable {}
1057+
@frozen public enum Body: Sendable, Equatable, Hashable {}
10581058
public var body: Operations.probe.Input.Body?
10591059
/// Creates a new `Input`.
10601060
///
@@ -1078,15 +1078,15 @@ public enum Operations {
10781078
self.body = body
10791079
}
10801080
}
1081-
public enum Output: Sendable, Equatable, Hashable {
1081+
@frozen public enum Output: Sendable, Equatable, Hashable {
10821082
public struct NoContent: Sendable, Equatable, Hashable {
10831083
public struct Headers: Sendable, Equatable, Hashable {
10841084
/// Creates a new `Headers`.
10851085
public init() {}
10861086
}
10871087
/// Received HTTP response headers
10881088
public var headers: Operations.probe.Output.NoContent.Headers
1089-
public enum Body: Sendable, Equatable, Hashable {}
1089+
@frozen public enum Body: Sendable, Equatable, Hashable {}
10901090
/// Received HTTP response body
10911091
public var body: Operations.probe.Output.NoContent.Body?
10921092
/// Creates a new `NoContent`.
@@ -1168,15 +1168,15 @@ public enum Operations {
11681168
self.body = body
11691169
}
11701170
}
1171-
public enum Output: Sendable, Equatable, Hashable {
1171+
@frozen public enum Output: Sendable, Equatable, Hashable {
11721172
public struct NoContent: Sendable, Equatable, Hashable {
11731173
public struct Headers: Sendable, Equatable, Hashable {
11741174
/// Creates a new `Headers`.
11751175
public init() {}
11761176
}
11771177
/// Received HTTP response headers
11781178
public var headers: Operations.updatePet.Output.NoContent.Headers
1179-
public enum Body: Sendable, Equatable, Hashable {}
1179+
@frozen public enum Body: Sendable, Equatable, Hashable {}
11801180
/// Received HTTP response body
11811181
public var body: Operations.updatePet.Output.NoContent.Body?
11821182
/// Creates a new `NoContent`.
@@ -1205,7 +1205,7 @@ public enum Operations {
12051205
}
12061206
/// Received HTTP response headers
12071207
public var headers: Operations.updatePet.Output.BadRequest.Headers
1208-
public enum Body: Sendable, Equatable, Hashable {
1208+
@frozen public enum Body: Sendable, Equatable, Hashable {
12091209
/// - Remark: Generated from `#/paths/pets/{petId}/PATCH/json`.
12101210
public struct jsonPayload: Codable, Equatable, Hashable, Sendable {
12111211
/// - Remark: Generated from `#/paths/pets/{petId}/PATCH/json/message`.
@@ -1277,7 +1277,7 @@ public enum Operations {
12771277
public init() {}
12781278
}
12791279
public var cookies: Operations.uploadAvatarForPet.Input.Cookies
1280-
public enum Body: Sendable, Equatable, Hashable { case binary(Foundation.Data) }
1280+
@frozen public enum Body: Sendable, Equatable, Hashable { case binary(Foundation.Data) }
12811281
public var body: Operations.uploadAvatarForPet.Input.Body
12821282
/// Creates a new `Input`.
12831283
///
@@ -1301,15 +1301,17 @@ public enum Operations {
13011301
self.body = body
13021302
}
13031303
}
1304-
public enum Output: Sendable, Equatable, Hashable {
1304+
@frozen public enum Output: Sendable, Equatable, Hashable {
13051305
public struct Ok: Sendable, Equatable, Hashable {
13061306
public struct Headers: Sendable, Equatable, Hashable {
13071307
/// Creates a new `Headers`.
13081308
public init() {}
13091309
}
13101310
/// Received HTTP response headers
13111311
public var headers: Operations.uploadAvatarForPet.Output.Ok.Headers
1312-
public enum Body: Sendable, Equatable, Hashable { case binary(Foundation.Data) }
1312+
@frozen public enum Body: Sendable, Equatable, Hashable {
1313+
case binary(Foundation.Data)
1314+
}
13131315
/// Received HTTP response body
13141316
public var body: Operations.uploadAvatarForPet.Output.Ok.Body
13151317
/// Creates a new `Ok`.
@@ -1338,7 +1340,7 @@ public enum Operations {
13381340
}
13391341
/// Received HTTP response headers
13401342
public var headers: Operations.uploadAvatarForPet.Output.PreconditionFailed.Headers
1341-
public enum Body: Sendable, Equatable, Hashable { case json(Swift.String) }
1343+
@frozen public enum Body: Sendable, Equatable, Hashable { case json(Swift.String) }
13421344
/// Received HTTP response body
13431345
public var body: Operations.uploadAvatarForPet.Output.PreconditionFailed.Body
13441346
/// Creates a new `PreconditionFailed`.
@@ -1368,7 +1370,7 @@ public enum Operations {
13681370
}
13691371
/// Received HTTP response headers
13701372
public var headers: Operations.uploadAvatarForPet.Output.InternalServerError.Headers
1371-
public enum Body: Sendable, Equatable, Hashable { case text(Swift.String) }
1373+
@frozen public enum Body: Sendable, Equatable, Hashable { case text(Swift.String) }
13721374
/// Received HTTP response body
13731375
public var body: Operations.uploadAvatarForPet.Output.InternalServerError.Body
13741376
/// Creates a new `InternalServerError`.

0 commit comments

Comments
 (0)