Skip to content

Commit d7814e4

Browse files
authored
Make Index.mediaType optional to comply with OCI spec (#368)
The `mediaType` field in the `Index` struct was defined as a required field, but according to the [OCI Image Index Specification](https://github.com/opencontainers/image-spec/blob/main/image-index.md), this field is optional. This caused failures when loading OCI archives where the `index.json` omits the top-level `mediaType` field, which is valid per the spec. Tools like skopeo can generate such archives. ## Error before fix ``` keyNotFound(CodingKeys(stringValue: "mediaType", intValue: nil)) ``` ## Changes - Changed `Index.mediaType` from `String` to `String?` - Updated initializer to accept optional `mediaType` parameter - Added comment documenting that field is optional per OCI spec ## Testing Verified that OCI archives without a top-level `mediaType` field in `index.json` now load successfully. Fixes apple/container#330
1 parent cc66529 commit d7814e4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Sources/ContainerizationOCI/Index.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct Index: Codable, Sendable {
2525
public let schemaVersion: Int
2626

2727
/// mediaType specifies the type of this document data structure e.g. `application/vnd.oci.image.index.v1+json`
28+
/// This field is optional per the OCI Image Index Specification (omitempty)
2829
public let mediaType: String
2930

3031
/// manifests references platform specific manifests.
@@ -42,4 +43,12 @@ public struct Index: Codable, Sendable {
4243
self.manifests = manifests
4344
self.annotations = annotations
4445
}
46+
47+
public init(from decoder: Decoder) throws {
48+
let container = try decoder.container(keyedBy: CodingKeys.self)
49+
self.schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)
50+
self.mediaType = try container.decodeIfPresent(String.self, forKey: .mediaType) ?? ""
51+
self.manifests = try container.decode([Descriptor].self, forKey: .manifests)
52+
self.annotations = try container.decodeIfPresent([String: String].self, forKey: .annotations)
53+
}
4554
}

0 commit comments

Comments
 (0)