Skip to content

Commit 8226ced

Browse files
PARAIPAN9czechboy0
andauthored
Add CaseIterable conformance for string, int enums (#559)
### Motivation - Fixes - #548. ### Modifications - Add another value in `conformances` array. ### Result - `String` and `Int` generated enums will conform by default to `CaseIterable` protocol. ### Test Plan - Adjust `OpenAPIGeneratorReferenceTests`. Co-authored-by: Honza Dvorsky <[email protected]>
1 parent 4f43141 commit 8226ced

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/Constants.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ enum Constants {
146146
static let baseConformanceInteger: String = "Int"
147147

148148
/// The types that every enum conforms to.
149-
static let conformances: [String] = ["Codable", "Hashable", "Sendable"]
149+
static let conformances: [String] = ["Codable", "Hashable", "Sendable", "CaseIterable"]
150150
}
151151

152152
/// Constants related to generated oneOf enums.

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public enum Components {
405405
/// Kind of pet
406406
///
407407
/// - Remark: Generated from `#/components/schemas/PetKind`.
408-
@frozen public enum PetKind: String, Codable, Hashable, Sendable {
408+
@frozen public enum PetKind: String, Codable, Hashable, Sendable, CaseIterable {
409409
case cat = "cat"
410410
case dog = "dog"
411411
case ELEPHANT = "ELEPHANT"
@@ -492,7 +492,7 @@ public enum Components {
492492
/// - Remark: Generated from `#/components/schemas/PetFeeding`.
493493
public struct PetFeeding: Codable, Hashable, Sendable {
494494
/// - Remark: Generated from `#/components/schemas/PetFeeding/schedule`.
495-
@frozen public enum schedulePayload: String, Codable, Hashable, Sendable {
495+
@frozen public enum schedulePayload: String, Codable, Hashable, Sendable, CaseIterable {
496496
case hourly = "hourly"
497497
case daily = "daily"
498498
case weekly = "weekly"
@@ -1491,7 +1491,7 @@ public enum Components {
14911491
/// - Remark: Generated from `#/components/requestBodies/MultipartUploadTypedRequest/multipartForm/log/headers`.
14921492
public struct Headers: Sendable, Hashable {
14931493
/// - Remark: Generated from `#/components/requestBodies/MultipartUploadTypedRequest/multipartForm/log/headers/x-log-type`.
1494-
@frozen public enum x_hyphen_log_hyphen_typePayload: String, Codable, Hashable, Sendable {
1494+
@frozen public enum x_hyphen_log_hyphen_typePayload: String, Codable, Hashable, Sendable, CaseIterable {
14951495
case structured = "structured"
14961496
case unstructured = "unstructured"
14971497
}
@@ -1645,7 +1645,7 @@ public enum Components {
16451645
/// - Remark: Generated from `#/components/responses/MultipartDownloadTypedResponse/content/multipartForm/log/headers`.
16461646
public struct Headers: Sendable, Hashable {
16471647
/// - Remark: Generated from `#/components/responses/MultipartDownloadTypedResponse/content/multipartForm/log/headers/x-log-type`.
1648-
@frozen public enum x_hyphen_log_hyphen_typePayload: String, Codable, Hashable, Sendable {
1648+
@frozen public enum x_hyphen_log_hyphen_typePayload: String, Codable, Hashable, Sendable, CaseIterable {
16491649
case structured = "structured"
16501650
case unstructured = "unstructured"
16511651
}
@@ -1773,7 +1773,7 @@ public enum Operations {
17731773
/// - Remark: Generated from `#/paths/pets/GET/query/limit`.
17741774
public var limit: Swift.Int32?
17751775
/// - Remark: Generated from `#/paths/pets/GET/query/habitat`.
1776-
@frozen public enum habitatPayload: String, Codable, Hashable, Sendable {
1776+
@frozen public enum habitatPayload: String, Codable, Hashable, Sendable, CaseIterable {
17771777
case water = "water"
17781778
case land = "land"
17791779
case air = "air"
@@ -1782,7 +1782,7 @@ public enum Operations {
17821782
/// - Remark: Generated from `#/paths/pets/GET/query/habitat`.
17831783
public var habitat: Operations.listPets.Input.Query.habitatPayload?
17841784
/// - Remark: Generated from `#/paths/pets/GET/query/feedsPayload`.
1785-
@frozen public enum feedsPayloadPayload: String, Codable, Hashable, Sendable {
1785+
@frozen public enum feedsPayloadPayload: String, Codable, Hashable, Sendable, CaseIterable {
17861786
case omnivore = "omnivore"
17871787
case carnivore = "carnivore"
17881788
case herbivore = "herbivore"

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
11891189
""",
11901190
"""
11911191
public enum Schemas {
1192-
@frozen public enum MyEnum: String, Codable, Hashable, Sendable {
1192+
@frozen public enum MyEnum: String, Codable, Hashable, Sendable, CaseIterable {
11931193
case one = "one"
11941194
case _empty = ""
11951195
case _dollar_tart = "$tart"
@@ -1213,7 +1213,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
12131213
""",
12141214
"""
12151215
public enum Schemas {
1216-
@frozen public enum MyEnum: Int, Codable, Hashable, Sendable {
1216+
@frozen public enum MyEnum: Int, Codable, Hashable, Sendable, CaseIterable {
12171217
case _0 = 0
12181218
case _10 = 10
12191219
case _20 = 20
@@ -1238,7 +1238,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
12381238
"""
12391239
public enum Schemas {
12401240
public struct MyOpenEnum: Codable, Hashable, Sendable {
1241-
@frozen public enum Value1Payload: String, Codable, Hashable, Sendable {
1241+
@frozen public enum Value1Payload: String, Codable, Hashable, Sendable, CaseIterable {
12421242
case one = "one"
12431243
case two = "two"
12441244
}
@@ -1928,7 +1928,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
19281928
public enum Responses {
19291929
public struct BadRequest: Sendable, Hashable {
19301930
public struct Headers: Sendable, Hashable {
1931-
@frozen public enum X_hyphen_ReasonPayload: String, Codable, Hashable, Sendable {
1931+
@frozen public enum X_hyphen_ReasonPayload: String, Codable, Hashable, Sendable, CaseIterable {
19321932
case badLuck = "badLuck"
19331933
}
19341934
public var X_hyphen_Reason: Components.Responses.BadRequest.Headers.X_hyphen_ReasonPayload?
@@ -2118,7 +2118,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
21182118
@frozen public enum multipartFormPayload: Sendable, Hashable {
21192119
public struct logPayload: Sendable, Hashable {
21202120
public struct Headers: Sendable, Hashable {
2121-
@frozen public enum x_hyphen_log_hyphen_typePayload: String, Codable, Hashable, Sendable {
2121+
@frozen public enum x_hyphen_log_hyphen_typePayload: String, Codable, Hashable, Sendable, CaseIterable {
21222122
case structured = "structured"
21232123
case unstructured = "unstructured"
21242124
}

0 commit comments

Comments
 (0)