Skip to content

Commit 6e904d0

Browse files
committed
Merge branch 'dev' into fix/reference-copy
2 parents 733185f + c87a3c2 commit 6e904d0

File tree

11 files changed

+54
-24
lines changed

11 files changed

+54
-24
lines changed

.github/workflows/ci-cd.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,18 @@ jobs:
3636
shell: pwsh
3737
run: |
3838
dotnet test Microsoft.OpenApi.sln -c Release -v n
39+
40+
validate-trimming:
41+
name: Validate Project for Trimming
42+
runs-on: windows-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
46+
- name: Setup .NET
47+
uses: actions/setup-dotnet@v4
48+
with:
49+
dotnet-version: 8.x
50+
51+
- name: Validate Trimming warnings
52+
run: dotnet publish -c Release -r win-x64 /p:TreatWarningsAsErrors=true /warnaserror -f net8.0
53+
working-directory: ./test/Microsoft.OpenApi.Trimming.Tests

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netstandard2.0;net6.0;</TargetFrameworks>
3+
<TargetFrameworks>netstandard2.0;net8.0;</TargetFrameworks>
44
<!-- net6.0 target is present because of the conditional build in OpenApiYamlReader.Read -->
55
<LangVersion>latest</LangVersion>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Version>2.0.0-preview4</Version>
88
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
99
<SignAssembly>true</SignAssembly>
10+
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)','net5.0'))">true</IsTrimmable>
11+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)','net8.0'))">true</IsAotCompatible>
1012
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
1113
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1214
<NoWarn>NU5048</NoWarn>

src/Microsoft.OpenApi/Extensions/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static class StringExtensions
3434
{
3535
var type = typeof(T);
3636

37-
var displayMap = EnumDisplayCache.GetOrAdd(type, GetEnumValues<T>);
37+
var displayMap = EnumDisplayCache.GetOrAdd(type, _=> GetEnumValues<T>(type));
3838

3939
if (displayMap.TryGetValue(displayName, out var cachedValue))
4040
{
@@ -45,7 +45,7 @@ internal static class StringExtensions
4545
result = default;
4646
return false;
4747
}
48-
private static ReadOnlyDictionary<string, object> GetEnumValues<T>(Type enumType) where T : Enum
48+
private static ReadOnlyDictionary<string, object> GetEnumValues<T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type enumType) where T : Enum
4949
{
5050
var result = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
5151
foreach (var field in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<Version>2.0.0-preview4</Version>
77
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
88
<SignAssembly>true</SignAssembly>
9+
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)','net5.0'))">true</IsTrimmable>
10+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)','net8.0'))">true</IsAotCompatible>
911
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
1012
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1113
<NoWarn>NU5048</NoWarn>

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,11 @@ where Type.Value.HasFlag(flag)
846846
writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s));
847847
}
848848

849+
#if NET5_0_OR_GREATER
850+
private static readonly Array jsonSchemaTypeValues = System.Enum.GetValues<JsonSchemaType>();
851+
#else
849852
private static readonly Array jsonSchemaTypeValues = System.Enum.GetValues(typeof(JsonSchemaType));
853+
#endif
850854

851855
private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter writer, OpenApiSpecVersion version)
852856
{

src/Microsoft.OpenApi/Reader/ParseNodes/MapNode.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Text.Json;
1010
using System.Text.Json.Nodes;
11+
using System.Text.Json.Serialization;
1112
using Microsoft.OpenApi.Any;
1213
using Microsoft.OpenApi.Exceptions;
1314
using Microsoft.OpenApi.Interfaces;
@@ -114,7 +115,7 @@ IEnumerator IEnumerable.GetEnumerator()
114115

115116
public override string GetRaw()
116117
{
117-
var x = JsonSerializer.Serialize(_node);
118+
var x = JsonSerializer.Serialize(_node, SourceGenerationContext.Default.JsonObject);
118119
return x;
119120
}
120121

@@ -176,4 +177,7 @@ public override JsonNode CreateAny()
176177
return _node;
177178
}
178179
}
180+
181+
[JsonSerializable(typeof(JsonObject))]
182+
internal partial class SourceGenerationContext : JsonSerializerContext { }
179183
}

src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public static void ValidateDataTypeMismatch(
5252
}
5353

5454
// convert value to JsonElement and access the ValueKind property to determine the type.
55-
var jsonElement = JsonDocument.Parse(JsonSerializer.Serialize(value)).RootElement;
55+
var valueKind = value.GetValueKind();
5656

5757
var type = schema.Type.ToIdentifier();
5858
var format = schema.Format;
5959
var nullable = schema.Nullable;
6060

6161
// Before checking the type, check first if the schema allows null.
6262
// If so and the data given is also null, this is allowed for any type.
63-
if (nullable && jsonElement.ValueKind is JsonValueKind.Null)
63+
if (nullable && valueKind is JsonValueKind.Null)
6464
{
6565
return;
6666
}
@@ -70,7 +70,7 @@ public static void ValidateDataTypeMismatch(
7070
// It is not against the spec to have a string representing an object value.
7171
// To represent examples of media types that cannot naturally be represented in JSON or YAML,
7272
// a string value can contain the example with escaping where necessary
73-
if (jsonElement.ValueKind is JsonValueKind.String)
73+
if (valueKind is JsonValueKind.String)
7474
{
7575
return;
7676
}
@@ -110,7 +110,7 @@ public static void ValidateDataTypeMismatch(
110110
// It is not against the spec to have a string representing an array value.
111111
// To represent examples of media types that cannot naturally be represented in JSON or YAML,
112112
// a string value can contain the example with escaping where necessary
113-
if (jsonElement.ValueKind is JsonValueKind.String)
113+
if (valueKind is JsonValueKind.String)
114114
{
115115
return;
116116
}
@@ -138,7 +138,7 @@ public static void ValidateDataTypeMismatch(
138138

139139
if (type is "integer" or "number" && format is "int32")
140140
{
141-
if (jsonElement.ValueKind is not JsonValueKind.Number)
141+
if (valueKind is not JsonValueKind.Number)
142142
{
143143
context.CreateWarning(
144144
ruleName,
@@ -150,7 +150,7 @@ public static void ValidateDataTypeMismatch(
150150

151151
if (type is "integer" or "number" && format is "int64")
152152
{
153-
if (jsonElement.ValueKind is not JsonValueKind.Number)
153+
if (valueKind is not JsonValueKind.Number)
154154
{
155155
context.CreateWarning(
156156
ruleName,
@@ -162,7 +162,7 @@ public static void ValidateDataTypeMismatch(
162162

163163
if (type is "integer")
164164
{
165-
if (jsonElement.ValueKind is not JsonValueKind.Number)
165+
if (valueKind is not JsonValueKind.Number)
166166
{
167167
context.CreateWarning(
168168
ruleName,
@@ -174,7 +174,7 @@ public static void ValidateDataTypeMismatch(
174174

175175
if (type is "number" && format is "float")
176176
{
177-
if (jsonElement.ValueKind is not JsonValueKind.Number)
177+
if (valueKind is not JsonValueKind.Number)
178178
{
179179
context.CreateWarning(
180180
ruleName,
@@ -186,7 +186,7 @@ public static void ValidateDataTypeMismatch(
186186

187187
if (type is "number" && format is "double")
188188
{
189-
if (jsonElement.ValueKind is not JsonValueKind.Number)
189+
if (valueKind is not JsonValueKind.Number)
190190
{
191191
context.CreateWarning(
192192
ruleName,
@@ -198,7 +198,7 @@ public static void ValidateDataTypeMismatch(
198198

199199
if (type is "number")
200200
{
201-
if (jsonElement.ValueKind is not JsonValueKind.Number)
201+
if (valueKind is not JsonValueKind.Number)
202202
{
203203
context.CreateWarning(
204204
ruleName,
@@ -210,7 +210,7 @@ public static void ValidateDataTypeMismatch(
210210

211211
if (type is "string" && format is "byte")
212212
{
213-
if (jsonElement.ValueKind is not JsonValueKind.String)
213+
if (valueKind is not JsonValueKind.String)
214214
{
215215
context.CreateWarning(
216216
ruleName,
@@ -222,7 +222,7 @@ public static void ValidateDataTypeMismatch(
222222

223223
if (type is "string" && format is "date")
224224
{
225-
if (jsonElement.ValueKind is not JsonValueKind.String)
225+
if (valueKind is not JsonValueKind.String)
226226
{
227227
context.CreateWarning(
228228
ruleName,
@@ -234,7 +234,7 @@ public static void ValidateDataTypeMismatch(
234234

235235
if (type is "string" && format is "date-time")
236236
{
237-
if (jsonElement.ValueKind is not JsonValueKind.String)
237+
if (valueKind is not JsonValueKind.String)
238238
{
239239
context.CreateWarning(
240240
ruleName,
@@ -246,7 +246,7 @@ public static void ValidateDataTypeMismatch(
246246

247247
if (type is "string" && format is "password")
248248
{
249-
if (jsonElement.ValueKind is not JsonValueKind.String)
249+
if (valueKind is not JsonValueKind.String)
250250
{
251251
context.CreateWarning(
252252
ruleName,
@@ -258,7 +258,7 @@ public static void ValidateDataTypeMismatch(
258258

259259
if (type is "string")
260260
{
261-
if (jsonElement.ValueKind is not JsonValueKind.String)
261+
if (valueKind is not JsonValueKind.String)
262262
{
263263
context.CreateWarning(
264264
ruleName,
@@ -270,7 +270,7 @@ public static void ValidateDataTypeMismatch(
270270

271271
if (type is "boolean")
272272
{
273-
if (jsonElement.ValueKind is not JsonValueKind.True && jsonElement.ValueKind is not JsonValueKind.False)
273+
if (valueKind is not JsonValueKind.True && valueKind is not JsonValueKind.False)
274274
{
275275
context.CreateWarning(
276276
ruleName,

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PackageReference Include="coverlet.collector" Version="6.0.3" PrivateAssets="all" />
1919
<PackageReference Include="coverlet.msbuild" Version="6.0.3" PrivateAssets="all" />
2020
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
21-
<PackageReference Include="FluentAssertions" Version="7.0.0" />
21+
<PackageReference Include="FluentAssertions" Version="7.1.0" />
2222
<PackageReference Include="SharpYaml" Version="2.1.1" />
2323
<PackageReference Include="xunit" Version="2.9.3" />
2424
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" PrivateAssets="all" />

test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="coverlet.collector" Version="6.0.3" PrivateAssets="all" />
1212
<PackageReference Include="coverlet.msbuild" Version="6.0.3" PrivateAssets="all" />
13-
<PackageReference Include="FluentAssertions" Version="7.0.0" />
13+
<PackageReference Include="FluentAssertions" Version="7.1.0" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1515
<PackageReference Include="Moq" Version="4.20.72" />
1616
<PackageReference Include="SharpYaml" Version="2.1.1" />

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[assembly: System.Reflection.AssemblyMetadata("IsTrimmable", "True")]
12
[assembly: System.Reflection.AssemblyMetadata("RepositoryUrl", "https://github.com/Microsoft/OpenAPI.NET")]
23
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(@"Microsoft.OpenApi.Readers.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100957cb48387b2a5f54f5ce39255f18f26d32a39990db27cf48737afc6bc62759ba996b8a2bfb675d4e39f3d06ecb55a178b1b4031dcb2a767e29977d88cce864a0d16bfc1b3bebb0edf9fe285f10fffc0a85f93d664fa05af07faa3aad2e545182dbf787e3fd32b56aca95df1a3c4e75dec164a3f1a4c653d971b01ffc39eb3c4")]
34
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(@"Microsoft.OpenApi.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100957cb48387b2a5f54f5ce39255f18f26d32a39990db27cf48737afc6bc62759ba996b8a2bfb675d4e39f3d06ecb55a178b1b4031dcb2a767e29977d88cce864a0d16bfc1b3bebb0edf9fe285f10fffc0a85f93d664fa05af07faa3aad2e545182dbf787e3fd32b56aca95df1a3c4e75dec164a3f1a4c653d971b01ffc39eb3c4")]

0 commit comments

Comments
 (0)