Skip to content

Commit

Permalink
Patch Device.Status to contain "PROCESSING" (#137)
Browse files Browse the repository at this point in the history
* Patch Device.Status to contain "PROCESSING"

* Update Device.swift

---------

Co-authored-by: Morten Bjerg Gregersen <[email protected]>
  • Loading branch information
MortenGregersen and MortenGregersen authored Oct 16, 2023
1 parent 4377979 commit 30b59de
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public extension Device.Attributes.Status {
switch self {
case .enabled: return "Enabled"
case .disabled: return "Disabled"
case .processing: return "Processing"
}
}
}
1 change: 1 addition & 0 deletions Sources/Bagbutik-Models/Provisioning/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public struct Device: Codable, Identifiable {
public enum Status: String, ParameterValue, Codable, CaseIterable {
case enabled = "ENABLED"
case disabled = "DISABLED"
case processing = "PROCESSING"
}
}
}
2 changes: 0 additions & 2 deletions Sources/BagbutikSpecDecoder/Schemas/ObjectSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public struct ObjectSchema: Decodable, Equatable {
case properties
case deprecated
case required
case attributes
case relationships
}

private enum PropertyCodingKeys: String, CodingKey {
Expand Down
30 changes: 25 additions & 5 deletions Sources/BagbutikSpecDecoder/Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct Spec: Decodable {
components = try container.decode(Components.self, forKey: .components)
}

internal init(paths: [String: Path], components: Components) throws {
init(paths: [String: Path], components: Components) throws {
self.paths = paths
self.components = components
}
Expand Down Expand Up @@ -91,16 +91,15 @@ public struct Spec: Decodable {
guard case .enumSchema(let mainAttributesPropertySchema) = mainAttributesProperty.type,
mainAttributesPropertySchema.cases == parameterEnumSchema.cases else { return nil }
return (name: propertyName, schema: mainAttributesPropertySchema)
}).first
{
}).first {
newType = "\(path.info.mainType).Attributes.\(propertyInfo.name.capitalizingFirstLetter())"
var enumSchema = propertyInfo.schema
enumSchema.additionalProtocols.insert("ParameterValue")
mainAttributesSchema.properties[propertyInfo.name]?.type = .enumSchema(enumSchema)
mainSchema.properties["attributes"]?.type = .schema(mainAttributesSchema)
components.schemas[path.info.mainType] = .object(mainSchema)
}
if let newType = newType {
if let newType {
operation.parameters?[parameterIndex] = .filter(name: parameterName, type: .simple(type: .init(type: newType)), required: parameterRequired, documentation: parameterDocumentation)
}
}
Expand Down Expand Up @@ -159,7 +158,28 @@ public struct Spec: Decodable {
}
components.schemas["BundleIdPlatform"] = .enum(bundleIdPlatformSchema)
}


// Add the case `PROCESSING` to Device.Status
// Apple's OpenAPI spec doesn't include Processing as status for Device.
if case .object(var deviceSchema) = components.schemas["Device"],
var deviceAttributesSchema: ObjectSchema = deviceSchema.subSchemas.compactMap({
guard case .objectSchema(let subSchema) = $0,
subSchema.name == "Attributes" else {
return nil
}
return subSchema
}).first,
var statusProperty = deviceAttributesSchema.properties["status"],
case .enumSchema(var statusEnum) = statusProperty.type {
var values = statusEnum.cases
values.append(.init(id: "processing", value: "PROCESSING"))
statusEnum.cases = values
statusProperty.type = .enumSchema(statusEnum)
deviceAttributesSchema.properties["status"] = statusProperty
deviceSchema.properties["attributes"]?.type = .schema(deviceAttributesSchema)
components.schemas["Device"] = .object(deviceSchema)
}

// Add the case `VISION_OS` to Platform
// Apple's OpenAPI spec doesn't include visionOS for App Categories. Reported to Apple 28/8/23 as FB13071298.
if case .enum(var platformSchema) = components.schemas["Platform"] {
Expand Down
44 changes: 43 additions & 1 deletion Tests/BagbutikSpecDecoderTests/SpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,32 @@ final class SpecTests: XCTestCase {
"type" : "string",
"enum" : [ "IOS", "MAC_OS" ]
},
"Device" : {
"type" : "object",
"title" : "Device",
"properties" : {
"type" : {
"type" : "string",
"enum" : [ "devices" ]
},
"id" : {
"type" : "string"
},
"attributes" : {
"type" : "object",
"properties" : {
"status" : {
"type" : "string",
"enum" : [ "ENABLED", "DISABLED" ]
},
}
},
"links" : {
"$ref" : "#/components/schemas/ResourceLinks"
}
},
"required" : [ "id", "type" ]
},
"ErrorResponse" : {
"type" : "object",
"properties" : {
Expand Down Expand Up @@ -826,6 +852,22 @@ final class SpecTests: XCTestCase {
XCTAssertTrue(bundleIdPlatformCaseValues.contains("UNIVERSAL"))
XCTAssertTrue(bundleIdPlatformCaseValues.contains("SERVICES"))

guard case .object(let deviceSchema) = spec.components.schemas["Device"],
var deviceAttributesSchema: ObjectSchema = deviceSchema.subSchemas.compactMap({
guard case .objectSchema(let subSchema) = $0,
subSchema.name == "Attributes" else {
return nil
}
return subSchema
}).first,
var statusProperty = deviceAttributesSchema.properties["status"],
case .enumSchema(var deviceStatusSchema) = statusProperty.type else {
XCTFail(); return
}
let deviceStatusCaseValues = deviceStatusSchema.cases.map(\.value)
XCTAssertEqual(deviceStatusCaseValues.count, 3)
XCTAssertTrue(deviceStatusCaseValues.contains("PROCESSING"))

guard case .object(let errorResponse) = spec.components.schemas["ErrorResponse"],
case .arrayOfSubSchema(let errorSchema) = errorResponse.properties["errors"]?.type,
case .oneOf(_, let oneOfSchema) = errorSchema.properties["source"]?.type
Expand All @@ -842,7 +884,7 @@ final class SpecTests: XCTestCase {
XCTFail(); return
}
XCTAssertEqual(errorSchemaRef, "Errors")

guard case .enum(let bundleIdPlatformSchema) = spec.components.schemas["Platform"] else {
XCTFail(); return
}
Expand Down

0 comments on commit 30b59de

Please sign in to comment.