1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Text . Encodings . Web ;
4
5
using System . Text . Json ;
5
6
using System . Text . Json . Serialization ;
6
7
using Microsoft . NET . Build . Containers . Resources ;
@@ -46,11 +47,6 @@ internal static (string, string) GenerateImageIndex(BuiltImage[] images)
46
47
47
48
internal static string GenerateImageIndex ( BuiltImage [ ] images , string manifestMediaType , string imageIndexMediaType )
48
49
{
49
- if ( images . Length == 0 )
50
- {
51
- throw new ArgumentException ( string . Format ( Strings . ImagesEmpty ) ) ;
52
- }
53
-
54
50
// Here we are using ManifestListV2 struct, but we could use ImageIndexV1 struct as well.
55
51
// We are filling the same fields, so we can use the same struct.
56
52
var manifests = new PlatformSpecificManifest [ images . Length ] ;
@@ -77,12 +73,7 @@ internal static string GenerateImageIndex(BuiltImage[] images, string manifestMe
77
73
manifests = manifests
78
74
} ;
79
75
80
- var options = new JsonSerializerOptions
81
- {
82
- DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
83
- } ;
84
-
85
- return JsonSerializer . SerializeToNode ( imageIndex , options ) ? . ToJsonString ( ) ?? "" ;
76
+ return GetJsonStringFromImageIndex ( imageIndex ) ;
86
77
}
87
78
88
79
internal static string GenerateImageIndexWithAnnotations ( string manifestMediaType , string manifestDigest , long manifestSize , string repository , string [ ] tags )
@@ -98,7 +89,7 @@ internal static string GenerateImageIndexWithAnnotations(string manifestMediaTyp
98
89
digest = manifestDigest ,
99
90
annotations = new Dictionary < string , string >
100
91
{
101
- { "io.containerd.image.name" , $ "{ repository } :{ tag } " } ,
92
+ { "io.containerd.image.name" , $ "docker.io/library/ { repository } :{ tag } " } ,
102
93
{ "org.opencontainers.image.ref.name" , tag }
103
94
}
104
95
} ;
@@ -111,11 +102,20 @@ internal static string GenerateImageIndexWithAnnotations(string manifestMediaTyp
111
102
manifests = manifests
112
103
} ;
113
104
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
115
111
{
116
112
DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull
117
113
} ;
114
+ var escapeOptions = new JsonSerializerOptions
115
+ {
116
+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
117
+ } ;
118
118
119
- return JsonSerializer . SerializeToNode ( index , options ) ? . ToJsonString ( ) ?? "" ;
119
+ return JsonSerializer . SerializeToNode ( imageIndex , nullIgnoreOptions ) ? . ToJsonString ( escapeOptions ) ?? "" ;
120
120
}
121
121
}
0 commit comments