From fc3161388f66d370e5fbb2b6eff6b84269677ea2 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Fri, 23 May 2025 18:43:47 +0530 Subject: [PATCH] Open enums --- Sources/DeveloperAPI/Generated/Types.swift | 309 ++++++++++++++---- Sources/DeveloperAPI/format.js | 25 +- Sources/DeveloperAPI/openapi-overlay.yaml | 19 -- .../DeveloperServicesAddAppOperation.swift | 6 +- .../DeveloperServicesCapability.swift | 4 +- ...perServicesFetchCertificateOperation.swift | 2 +- .../DeveloperServices+OpenAPI.swift | 24 ++ ...eveloperServicesFetchDeviceOperation.swift | 2 +- 8 files changed, 301 insertions(+), 90 deletions(-) delete mode 100644 Sources/DeveloperAPI/openapi-overlay.yaml diff --git a/Sources/DeveloperAPI/Generated/Types.swift b/Sources/DeveloperAPI/Generated/Types.swift index ace7e465..f23522a0 100644 --- a/Sources/DeveloperAPI/Generated/Types.swift +++ b/Sources/DeveloperAPI/Generated/Types.swift @@ -3464,14 +3464,60 @@ public enum Components { /// - Remark: Generated from `#/components/schemas/Device/attributes/udid`. public var udid: Swift.String? /// - Remark: Generated from `#/components/schemas/Device/attributes/deviceClass`. - @frozen public enum DeviceClassPayload: String, Codable, Hashable, Sendable, CaseIterable { - case appleWatch = "APPLE_WATCH" - case ipad = "IPAD" - case iphone = "IPHONE" - case ipod = "IPOD" - case appleTv = "APPLE_TV" - case mac = "MAC" - case appleVisionPro = "APPLE_VISION_PRO" + public struct DeviceClassPayload: Codable, Hashable, Sendable { + /// - Remark: Generated from `#/components/schemas/Device/attributes/deviceClass/value1`. + @frozen public enum Value1Payload: String, Codable, Hashable, Sendable, CaseIterable { + case appleWatch = "APPLE_WATCH" + case ipad = "IPAD" + case iphone = "IPHONE" + case ipod = "IPOD" + case appleTv = "APPLE_TV" + case mac = "MAC" + } + /// - Remark: Generated from `#/components/schemas/Device/attributes/deviceClass/value1`. + public var value1: Components.Schemas.Device.AttributesPayload.DeviceClassPayload.Value1Payload? + /// - Remark: Generated from `#/components/schemas/Device/attributes/deviceClass/value2`. + public var value2: Swift.String? + /// Creates a new `DeviceClassPayload`. + /// + /// - Parameters: + /// - value1: + /// - value2: + public init( + value1: Components.Schemas.Device.AttributesPayload.DeviceClassPayload.Value1Payload? = nil, + value2: Swift.String? = nil + ) { + self.value1 = value1 + self.value2 = value2 + } + public init(from decoder: any Decoder) throws { + var errors: [any Error] = [] + do { + self.value1 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + do { + self.value2 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + try Swift.DecodingError.verifyAtLeastOneSchemaIsNotNil( + [ + self.value1, + self.value2 + ], + type: Self.self, + codingPath: decoder.codingPath, + errors: errors + ) + } + public func encode(to encoder: any Encoder) throws { + try encoder.encodeFirstNonNilValueToSingleValueContainer([ + self.value1, + self.value2 + ]) + } } /// - Remark: Generated from `#/components/schemas/Device/attributes/deviceClass`. public var deviceClass: Components.Schemas.Device.AttributesPayload.DeviceClassPayload? @@ -5015,12 +5061,57 @@ public enum Components { } } /// - Remark: Generated from `#/components/schemas/BundleIdPlatform`. - @frozen public enum BundleIdPlatform: String, Codable, Hashable, Sendable, CaseIterable { - case ios = "IOS" - case macOs = "MAC_OS" - case universal = "UNIVERSAL" - case services = "SERVICES" - case macos = "MACOS" + public struct BundleIdPlatform: Codable, Hashable, Sendable { + /// - Remark: Generated from `#/components/schemas/BundleIdPlatform/value1`. + @frozen public enum Value1Payload: String, Codable, Hashable, Sendable, CaseIterable { + case ios = "IOS" + case macOs = "MAC_OS" + case universal = "UNIVERSAL" + } + /// - Remark: Generated from `#/components/schemas/BundleIdPlatform/value1`. + public var value1: Components.Schemas.BundleIdPlatform.Value1Payload? + /// - Remark: Generated from `#/components/schemas/BundleIdPlatform/value2`. + public var value2: Swift.String? + /// Creates a new `BundleIdPlatform`. + /// + /// - Parameters: + /// - value1: + /// - value2: + public init( + value1: Components.Schemas.BundleIdPlatform.Value1Payload? = nil, + value2: Swift.String? = nil + ) { + self.value1 = value1 + self.value2 = value2 + } + public init(from decoder: any Decoder) throws { + var errors: [any Error] = [] + do { + self.value1 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + do { + self.value2 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + try Swift.DecodingError.verifyAtLeastOneSchemaIsNotNil( + [ + self.value1, + self.value2 + ], + type: Self.self, + codingPath: decoder.codingPath, + errors: errors + ) + } + public func encode(to encoder: any Encoder) throws { + try encoder.encodeFirstNonNilValueToSingleValueContainer([ + self.value1, + self.value2 + ]) + } } /// - Remark: Generated from `#/components/schemas/CapabilityOption`. public struct CapabilityOption: Codable, Hashable, Sendable { @@ -5150,56 +5241,150 @@ public enum Components { } } /// - Remark: Generated from `#/components/schemas/CapabilityType`. - @frozen public enum CapabilityType: String, Codable, Hashable, Sendable, CaseIterable { - case icloud = "ICLOUD" - case inAppPurchase = "IN_APP_PURCHASE" - case gameCenter = "GAME_CENTER" - case pushNotifications = "PUSH_NOTIFICATIONS" - case wallet = "WALLET" - case interAppAudio = "INTER_APP_AUDIO" - case maps = "MAPS" - case associatedDomains = "ASSOCIATED_DOMAINS" - case personalVpn = "PERSONAL_VPN" - case appGroups = "APP_GROUPS" - case healthkit = "HEALTHKIT" - case homekit = "HOMEKIT" - case wirelessAccessoryConfiguration = "WIRELESS_ACCESSORY_CONFIGURATION" - case applePay = "APPLE_PAY" - case dataProtection = "DATA_PROTECTION" - case sirikit = "SIRIKIT" - case networkExtensions = "NETWORK_EXTENSIONS" - case multipath = "MULTIPATH" - case hotSpot = "HOT_SPOT" - case nfcTagReading = "NFC_TAG_READING" - case classkit = "CLASSKIT" - case autofillCredentialProvider = "AUTOFILL_CREDENTIAL_PROVIDER" - case accessWifiInformation = "ACCESS_WIFI_INFORMATION" - case networkCustomProtocol = "NETWORK_CUSTOM_PROTOCOL" - case coremediaHlsLowLatency = "COREMEDIA_HLS_LOW_LATENCY" - case systemExtensionInstall = "SYSTEM_EXTENSION_INSTALL" - case userManagement = "USER_MANAGEMENT" - case appleIdAuth = "APPLE_ID_AUTH" + public struct CapabilityType: Codable, Hashable, Sendable { + /// - Remark: Generated from `#/components/schemas/CapabilityType/value1`. + @frozen public enum Value1Payload: String, Codable, Hashable, Sendable, CaseIterable { + case icloud = "ICLOUD" + case inAppPurchase = "IN_APP_PURCHASE" + case gameCenter = "GAME_CENTER" + case pushNotifications = "PUSH_NOTIFICATIONS" + case wallet = "WALLET" + case interAppAudio = "INTER_APP_AUDIO" + case maps = "MAPS" + case associatedDomains = "ASSOCIATED_DOMAINS" + case personalVpn = "PERSONAL_VPN" + case appGroups = "APP_GROUPS" + case healthkit = "HEALTHKIT" + case homekit = "HOMEKIT" + case wirelessAccessoryConfiguration = "WIRELESS_ACCESSORY_CONFIGURATION" + case applePay = "APPLE_PAY" + case dataProtection = "DATA_PROTECTION" + case sirikit = "SIRIKIT" + case networkExtensions = "NETWORK_EXTENSIONS" + case multipath = "MULTIPATH" + case hotSpot = "HOT_SPOT" + case nfcTagReading = "NFC_TAG_READING" + case classkit = "CLASSKIT" + case autofillCredentialProvider = "AUTOFILL_CREDENTIAL_PROVIDER" + case accessWifiInformation = "ACCESS_WIFI_INFORMATION" + case networkCustomProtocol = "NETWORK_CUSTOM_PROTOCOL" + case coremediaHlsLowLatency = "COREMEDIA_HLS_LOW_LATENCY" + case systemExtensionInstall = "SYSTEM_EXTENSION_INSTALL" + case userManagement = "USER_MANAGEMENT" + case appleIdAuth = "APPLE_ID_AUTH" + } + /// - Remark: Generated from `#/components/schemas/CapabilityType/value1`. + public var value1: Components.Schemas.CapabilityType.Value1Payload? + /// - Remark: Generated from `#/components/schemas/CapabilityType/value2`. + public var value2: Swift.String? + /// Creates a new `CapabilityType`. + /// + /// - Parameters: + /// - value1: + /// - value2: + public init( + value1: Components.Schemas.CapabilityType.Value1Payload? = nil, + value2: Swift.String? = nil + ) { + self.value1 = value1 + self.value2 = value2 + } + public init(from decoder: any Decoder) throws { + var errors: [any Error] = [] + do { + self.value1 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + do { + self.value2 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + try Swift.DecodingError.verifyAtLeastOneSchemaIsNotNil( + [ + self.value1, + self.value2 + ], + type: Self.self, + codingPath: decoder.codingPath, + errors: errors + ) + } + public func encode(to encoder: any Encoder) throws { + try encoder.encodeFirstNonNilValueToSingleValueContainer([ + self.value1, + self.value2 + ]) + } } /// - Remark: Generated from `#/components/schemas/CertificateType`. - @frozen public enum CertificateType: String, Codable, Hashable, Sendable, CaseIterable { - case applePay = "APPLE_PAY" - case applePayMerchantIdentity = "APPLE_PAY_MERCHANT_IDENTITY" - case applePayPspIdentity = "APPLE_PAY_PSP_IDENTITY" - case applePayRsa = "APPLE_PAY_RSA" - case developerIdKext = "DEVELOPER_ID_KEXT" - case developerIdKextG2 = "DEVELOPER_ID_KEXT_G2" - case developerIdApplication = "DEVELOPER_ID_APPLICATION" - case developerIdApplicationG2 = "DEVELOPER_ID_APPLICATION_G2" - case development = "DEVELOPMENT" - case distribution = "DISTRIBUTION" - case identityAccess = "IDENTITY_ACCESS" - case iosDevelopment = "IOS_DEVELOPMENT" - case iosDistribution = "IOS_DISTRIBUTION" - case macAppDistribution = "MAC_APP_DISTRIBUTION" - case macInstallerDistribution = "MAC_INSTALLER_DISTRIBUTION" - case macAppDevelopment = "MAC_APP_DEVELOPMENT" - case passTypeId = "PASS_TYPE_ID" - case passTypeIdWithNfc = "PASS_TYPE_ID_WITH_NFC" + public struct CertificateType: Codable, Hashable, Sendable { + /// - Remark: Generated from `#/components/schemas/CertificateType/value1`. + @frozen public enum Value1Payload: String, Codable, Hashable, Sendable, CaseIterable { + case applePay = "APPLE_PAY" + case applePayMerchantIdentity = "APPLE_PAY_MERCHANT_IDENTITY" + case applePayPspIdentity = "APPLE_PAY_PSP_IDENTITY" + case applePayRsa = "APPLE_PAY_RSA" + case developerIdKext = "DEVELOPER_ID_KEXT" + case developerIdKextG2 = "DEVELOPER_ID_KEXT_G2" + case developerIdApplication = "DEVELOPER_ID_APPLICATION" + case developerIdApplicationG2 = "DEVELOPER_ID_APPLICATION_G2" + case development = "DEVELOPMENT" + case distribution = "DISTRIBUTION" + case identityAccess = "IDENTITY_ACCESS" + case iosDevelopment = "IOS_DEVELOPMENT" + case iosDistribution = "IOS_DISTRIBUTION" + case macAppDistribution = "MAC_APP_DISTRIBUTION" + case macInstallerDistribution = "MAC_INSTALLER_DISTRIBUTION" + case macAppDevelopment = "MAC_APP_DEVELOPMENT" + case passTypeId = "PASS_TYPE_ID" + case passTypeIdWithNfc = "PASS_TYPE_ID_WITH_NFC" + } + /// - Remark: Generated from `#/components/schemas/CertificateType/value1`. + public var value1: Components.Schemas.CertificateType.Value1Payload? + /// - Remark: Generated from `#/components/schemas/CertificateType/value2`. + public var value2: Swift.String? + /// Creates a new `CertificateType`. + /// + /// - Parameters: + /// - value1: + /// - value2: + public init( + value1: Components.Schemas.CertificateType.Value1Payload? = nil, + value2: Swift.String? = nil + ) { + self.value1 = value1 + self.value2 = value2 + } + public init(from decoder: any Decoder) throws { + var errors: [any Error] = [] + do { + self.value1 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + do { + self.value2 = try decoder.decodeFromSingleValueContainer() + } catch { + errors.append(error) + } + try Swift.DecodingError.verifyAtLeastOneSchemaIsNotNil( + [ + self.value1, + self.value2 + ], + type: Self.self, + codingPath: decoder.codingPath, + errors: errors + ) + } + public func encode(to encoder: any Encoder) throws { + try encoder.encodeFirstNonNilValueToSingleValueContainer([ + self.value1, + self.value2 + ]) + } } /// - Remark: Generated from `#/components/schemas/SubscriptionStatusUrlVersion`. @frozen public enum SubscriptionStatusUrlVersion: String, Codable, Hashable, Sendable, CaseIterable { diff --git a/Sources/DeveloperAPI/format.js b/Sources/DeveloperAPI/format.js index f1ace664..064af46e 100644 --- a/Sources/DeveloperAPI/format.js +++ b/Sources/DeveloperAPI/format.js @@ -2,9 +2,22 @@ import fs from 'fs'; +function makeOpen(enumSchema) { + const copy = structuredClone(enumSchema); + Object.keys(enumSchema).forEach(k => delete enumSchema[k]); + Object.assign(enumSchema, { + anyOf: [ + copy, + { type: 'string' }, + ] + }) +} + function format(schema) { + const schemas = schema.components.schemas; + // this field is required when using the private Xcode API - const capabilityCreateRelationships = schema.components.schemas.BundleIdCapabilityCreateRequest.properties.data.properties.relationships; + const capabilityCreateRelationships = schemas.BundleIdCapabilityCreateRequest.properties.data.properties.relationships; capabilityCreateRelationships.properties.capability = { type: 'object', properties: { @@ -26,7 +39,15 @@ function format(schema) { // we don't use this but it triggers a deprecation warning. see: // https://github.com/apple/swift-openapi-generator/issues/715 - schema.components.schemas.App.properties.relationships.properties.inAppPurchases.deprecated = false; + schemas.App.properties.relationships.properties.inAppPurchases.deprecated = false; + + // openapi-generator expects response enums to be exhaustive. Apple's ASC OpenAPI spec + // misses some cases that they do, actually, return. + // https://swiftpackageindex.com/apple/swift-openapi-generator/1.7.2/documentation/swift-openapi-generator/useful-openapi-patterns#Open-enums-and-oneOfs + makeOpen(schemas.BundleIdPlatform); + makeOpen(schemas.CapabilityType); + makeOpen(schemas.CertificateType); + makeOpen(schemas.Device.properties.attributes.properties.deviceClass); return schema; } diff --git a/Sources/DeveloperAPI/openapi-overlay.yaml b/Sources/DeveloperAPI/openapi-overlay.yaml deleted file mode 100644 index 4cec71ac..00000000 --- a/Sources/DeveloperAPI/openapi-overlay.yaml +++ /dev/null @@ -1,19 +0,0 @@ -overlay: 1.0.0 -info: - title: Overlay for App Store Connect API - version: 1.0.0 -actions: - # openapi-generator expects response enums to be exhaustive. Apple's ASC OpenAPI spec - # misses some cases that they do, actually, return. - - target: '$.components.schemas.Device.properties.attributes.properties.deviceClass' - update: - enum: - - APPLE_VISION_PRO - - target: '$.components.schemas.BundleIdPlatform' - update: - enum: - - SERVICES - # the spec already declares MAC_OS but ASC sometimes seems to - # spell this as MACOS instead. see: - # https://github.com/xtool-org/xtool/issues/64 - - MACOS diff --git a/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift b/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift index 90684f8a..0be5c699 100644 --- a/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift +++ b/Sources/XKit/DeveloperServices/App IDs/DeveloperServicesAddAppOperation.swift @@ -57,7 +57,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation { _type: .bundleIds, attributes: .init( name: name, - platform: .ios, + platform: .init(.ios), identifier: newBundleID ) ) @@ -105,8 +105,8 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation { } } else { // DeveloperServices doesn't allow deleting these capabilities - let requiredCapabilities: Set = [.inAppPurchase] - if let capType = cap.attributes?.capabilityType, !requiredCapabilities.contains(capType) { + let requiredCapabilities: Set = [.inAppPurchase] + if let capType = cap.attributes?.capabilityType?.value1, !requiredCapabilities.contains(capType) { _ = try await context.developerAPIClient .bundleIdCapabilitiesDeleteInstance(path: .init(id: cap.id)) .noContent diff --git a/Sources/XKit/DeveloperServices/App IDs/Entitlements/DeveloperServicesCapability.swift b/Sources/XKit/DeveloperServices/App IDs/Entitlements/DeveloperServicesCapability.swift index 6742ea1b..0cc57f3b 100644 --- a/Sources/XKit/DeveloperServices/App IDs/Entitlements/DeveloperServicesCapability.swift +++ b/Sources/XKit/DeveloperServices/App IDs/Entitlements/DeveloperServicesCapability.swift @@ -15,11 +15,11 @@ public struct DeveloperServicesCapability: Sendable, Hashable { public var settings: [Components.Schemas.CapabilitySetting]? public init( - _ capabilityType: Components.Schemas.CapabilityType, + _ capabilityType: Components.Schemas.CapabilityType.Value1Payload, isFree: Bool, settings: [Components.Schemas.CapabilitySetting]? = nil ) { - self.capabilityType = capabilityType + self.capabilityType = .init(capabilityType) self.isFree = isFree self.settings = settings } diff --git a/Sources/XKit/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift b/Sources/XKit/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift index 26ae405a..980cb9c9 100644 --- a/Sources/XKit/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift +++ b/Sources/XKit/DeveloperServices/Certificates/DeveloperServicesFetchCertificateOperation.swift @@ -54,7 +54,7 @@ public struct DeveloperServicesFetchCertificateOperation: DeveloperServicesOpera _type: .certificates, attributes: .init( csrContent: csr.pemString, - certificateType: .development + certificateType: .init(.development) ) ))) ) diff --git a/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift b/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift index 3ba555e2..69da496d 100644 --- a/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift +++ b/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift @@ -256,3 +256,27 @@ struct LoggingMiddleware: ClientMiddleware { return (response, body) } } + +// syntactic sugar to make it nicer to work with `anyOf:` types +public protocol OpenAPIExtensibleEnum { + associatedtype Value1Payload: RawRepresentable, Codable, Hashable, Sendable, CaseIterable + var value1: Value1Payload? { get set } + var value2: String? { get set } + + init(value1: Value1Payload?, value2: String?) +} + +extension OpenAPIExtensibleEnum { + public var rawValue: String { + value2! + } + + public init(_ value: Value1Payload) { + self.init(value1: value, value2: nil) + } +} + +extension Components.Schemas.BundleIdPlatform: OpenAPIExtensibleEnum {} +extension Components.Schemas.CapabilityType: OpenAPIExtensibleEnum {} +extension Components.Schemas.CertificateType: OpenAPIExtensibleEnum {} +extension Components.Schemas.Device.AttributesPayload.DeviceClassPayload: OpenAPIExtensibleEnum {} diff --git a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift index b5cbc8d4..9f5eaf7d 100644 --- a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift +++ b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift @@ -27,7 +27,7 @@ public struct DeveloperServicesFetchDeviceOperation: DeveloperServicesOperation _type: .devices, attributes: .init( name: self.context.deviceName, - platform: .ios, + platform: .init(.ios), udid: self.context.udid ) )))