From ed731ca89a35f4b62b67d94f37402e5628b78935 Mon Sep 17 00:00:00 2001 From: Valentin Delaye Date: Tue, 24 Feb 2026 07:57:21 +0100 Subject: [PATCH] Better support for Index artifact type Signed-off-by: Valentin Delaye --- src/main/java/land/oras/Index.java | 63 ++++++++++++++++++++++++-- src/test/java/land/oras/IndexTest.java | 8 ++-- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/main/java/land/oras/Index.java b/src/main/java/land/oras/Index.java index d467080..67bed2c 100644 --- a/src/main/java/land/oras/Index.java +++ b/src/main/java/land/oras/Index.java @@ -21,6 +21,7 @@ package land.oras; import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @@ -32,6 +33,7 @@ import java.util.Objects; import land.oras.utils.Const; import land.oras.utils.JsonUtils; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; /** @@ -70,7 +72,42 @@ private Index( @JsonProperty(Const.JSON_PROPERTY_MANIFESTS) List manifests, @JsonProperty(Const.JSON_PROPERTY_ANNOTATIONS) Map annotations, @JsonProperty(Const.JSON_PROPERTY_SUBJECT) Subject subject) { - this(schemaVersion, mediaType, artifactType, manifests, annotations, subject, null, null, null); + super( + null, + null, + mediaType, + annotations != null && !annotations.isEmpty() ? Map.copyOf(annotations) : null, + artifactType, + null, + null); + this.schemaVersion = schemaVersion; + this.manifests = manifests; + this.descriptor = null; + this.subject = subject; + } + + private Index( + int schemaVersion, + String mediaType, + ArtifactType artifactType, + List manifests, + Map annotations, + Subject subject, + ManifestDescriptor descriptor, + String registry, + String json) { + super( + null, + null, + mediaType, + annotations, + artifactType != null ? artifactType.getMediaType() : null, + registry, + json); + this.schemaVersion = schemaVersion; + this.descriptor = descriptor; + this.subject = subject; + this.manifests = manifests; } private Index( @@ -139,6 +176,15 @@ public List unspecifiedPlatforms() { .orElse(null); } + @Override + @JsonIgnore + public @NonNull ArtifactType getArtifactType() { + if (artifactType != null) { + return ArtifactType.from(artifactType); + } + return ArtifactType.unknown(); + } + /** * Get the artifact type as string for JSON serialization * @return The artifact type as string @@ -185,7 +231,15 @@ public Index withNewManifests(ManifestDescriptor manifest) { } newManifests.add(manifest); return new Index( - schemaVersion, mediaType, artifactType, newManifests, annotations, subject, descriptor, registry, json); + schemaVersion, + mediaType, + ArtifactType.from(artifactType), + newManifests, + annotations, + subject, + descriptor, + registry, + json); } @Override @@ -210,7 +264,7 @@ public ManifestDescriptor getDescriptor() { * @param artifactType The artifact type * @return The index */ - public Index withArtifactType(String artifactType) { + public Index withArtifactType(ArtifactType artifactType) { return new Index( schemaVersion, mediaType, artifactType, manifests, annotations, subject, descriptor, registry, json); } @@ -274,7 +328,8 @@ public static Index fromPath(Path path) { * @return The index */ public static Index fromManifests(List descriptors) { - return new Index(2, Const.DEFAULT_INDEX_MEDIA_TYPE, null, descriptors, null, null, null, null, null); + return new Index( + 2, Const.DEFAULT_INDEX_MEDIA_TYPE, (ArtifactType) null, descriptors, null, null, null, null, null); } @Override diff --git a/src/test/java/land/oras/IndexTest.java b/src/test/java/land/oras/IndexTest.java index d61c0e3..e113341 100644 --- a/src/test/java/land/oras/IndexTest.java +++ b/src/test/java/land/oras/IndexTest.java @@ -41,7 +41,8 @@ void shouldReadAndWriteIndex() { assertNull(index.getMediaType()); assertEquals(2, index.getSchemaVersion()); assertEquals(1, index.getManifests().size()); - assertNull(index.getArtifactType()); + assertNull(index.getArtifactTypeAsString()); + assertEquals(Const.DEFAULT_ARTIFACT_MEDIA_TYPE, index.getArtifactType().getMediaType()); assertNull(index.getAnnotations()); assertNull(index.getDescriptor()); assertEquals( @@ -58,7 +59,7 @@ void shouldReadAndWriteIndex() { @Test void shouldAddArtifactType() { Index index = Index.fromManifests(List.of()); - index = index.withArtifactType("application/vnd.opentofu.provider"); + index = index.withArtifactType(ArtifactType.from("application/vnd.opentofu.provider")); assertNotNull(index.getArtifactType()); assertEquals( "application/vnd.opentofu.provider", index.getArtifactType().getMediaType()); @@ -75,7 +76,8 @@ void shouldReadAndWriteIndexWithAnnotations() { assertNull(index.getMediaType()); assertEquals(2, index.getSchemaVersion()); assertEquals(1, index.getManifests().size()); - assertNull(index.getArtifactType()); + assertNull(index.getArtifactTypeAsString()); + assertEquals(Const.DEFAULT_ARTIFACT_MEDIA_TYPE, index.getArtifactType().getMediaType()); assertNull(index.getAnnotations()); assertNull(index.getDescriptor()); assertEquals(