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 https://github.com/apple/container/issues/330
This commit is contained in:
Mark Baseggio
2025-10-29 01:46:16 -07:00
committed by GitHub
parent cc66529b59
commit d7814e4421
+9
View File
@@ -25,6 +25,7 @@ public struct Index: Codable, Sendable {
public let schemaVersion: Int
/// mediaType specifies the type of this document data structure e.g. `application/vnd.oci.image.index.v1+json`
/// This field is optional per the OCI Image Index Specification (omitempty)
public let mediaType: String
/// manifests references platform specific manifests.
@@ -42,4 +43,12 @@ public struct Index: Codable, Sendable {
self.manifests = manifests
self.annotations = annotations
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.schemaVersion = try container.decode(Int.self, forKey: .schemaVersion)
self.mediaType = try container.decodeIfPresent(String.self, forKey: .mediaType) ?? ""
self.manifests = try container.decode([Descriptor].self, forKey: .manifests)
self.annotations = try container.decodeIfPresent([String: String].self, forKey: .annotations)
}
}