Skip to content

Commit 69026ea

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
add docker.io/library prefix to image name annotation; fix json serialization symbol escaping
1 parent af3a548 commit 69026ea

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/Containers/Microsoft.NET.Build.Containers/ImageIndexGenerator.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text.Encodings.Web;
45
using System.Text.Json;
56
using System.Text.Json.Serialization;
67
using Microsoft.NET.Build.Containers.Resources;
@@ -46,11 +47,6 @@ internal static (string, string) GenerateImageIndex(BuiltImage[] images)
4647

4748
internal static string GenerateImageIndex(BuiltImage[] images, string manifestMediaType, string imageIndexMediaType)
4849
{
49-
if (images.Length == 0)
50-
{
51-
throw new ArgumentException(string.Format(Strings.ImagesEmpty));
52-
}
53-
5450
// Here we are using ManifestListV2 struct, but we could use ImageIndexV1 struct as well.
5551
// We are filling the same fields, so we can use the same struct.
5652
var manifests = new PlatformSpecificManifest[images.Length];
@@ -77,12 +73,7 @@ internal static string GenerateImageIndex(BuiltImage[] images, string manifestMe
7773
manifests = manifests
7874
};
7975

80-
var options = new JsonSerializerOptions
81-
{
82-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
83-
};
84-
85-
return JsonSerializer.SerializeToNode(imageIndex, options)?.ToJsonString() ?? "";
76+
return GetJsonStringFromImageIndex(imageIndex);
8677
}
8778

8879
internal static string GenerateImageIndexWithAnnotations(string manifestMediaType, string manifestDigest, long manifestSize, string repository, string[] tags)
@@ -98,7 +89,7 @@ internal static string GenerateImageIndexWithAnnotations(string manifestMediaTyp
9889
digest = manifestDigest,
9990
annotations = new Dictionary<string, string>
10091
{
101-
{ "io.containerd.image.name", $"{repository}:{tag}" },
92+
{ "io.containerd.image.name", $"docker.io/library/{repository}:{tag}" },
10293
{ "org.opencontainers.image.ref.name", tag }
10394
}
10495
};
@@ -111,11 +102,20 @@ internal static string GenerateImageIndexWithAnnotations(string manifestMediaTyp
111102
manifests = manifests
112103
};
113104

114-
var options = new JsonSerializerOptions
105+
return GetJsonStringFromImageIndex(index);
106+
}
107+
108+
private static string GetJsonStringFromImageIndex<T>(T imageIndex)
109+
{
110+
var nullIgnoreOptions = new JsonSerializerOptions
115111
{
116112
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
117113
};
114+
var escapeOptions = new JsonSerializerOptions
115+
{
116+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
117+
};
118118

119-
return JsonSerializer.SerializeToNode(index, options)?.ToJsonString() ?? "";
119+
return JsonSerializer.SerializeToNode(imageIndex, nullIgnoreOptions)?.ToJsonString(escapeOptions) ?? "";
120120
}
121121
}

0 commit comments

Comments
 (0)