Skip to content

Commit d8eda7a

Browse files
committed
Make library trim-compatible
1 parent 3d971de commit d8eda7a

File tree

4 files changed

+255
-8
lines changed

4 files changed

+255
-8
lines changed

src/Microsoft.OpenApi/Any/OpenApiAnyCloneHelper.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System.Reflection;
4+
using System.Diagnostics.CodeAnalysis;
55

66
namespace Microsoft.OpenApi.Any
77
{
@@ -15,17 +15,16 @@ public class OpenApiAnyCloneHelper
1515
/// </summary>
1616
/// <param name="obj">The object instance.</param>
1717
/// <returns>A clone copy or the object itself.</returns>
18-
public static IOpenApiAny CloneFromCopyConstructor(IOpenApiAny obj)
18+
public static IOpenApiAny CloneFromCopyConstructor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(T obj) where T : IOpenApiAny
1919
{
2020
if (obj != null)
2121
{
22-
var t = obj.GetType();
23-
foreach (var ci in t.GetConstructors())
22+
foreach (var ci in typeof(T).GetConstructors())
2423
{
2524
var pi = ci.GetParameters();
26-
if (pi.Length == 1 && pi[0].ParameterType == t)
25+
if (pi.Length == 1 && pi[0].ParameterType == typeof(T))
2726
{
28-
return (IOpenApiAny)ci.Invoke(new object[] { obj });
27+
return (IOpenApiAny)ci.Invoke([obj]);
2928
}
3029
}
3130
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
#nullable enable
5+
6+
namespace System.Diagnostics.CodeAnalysis
7+
{
8+
#if !NET7_0_OR_GREATER
9+
/// <summary>
10+
/// Indicates that certain members on a specified <see cref="Type"/> are accessed dynamically,
11+
/// for example through <see cref="System.Reflection"/>.
12+
/// </summary>
13+
/// <remarks>
14+
/// This allows tools to understand which members are being accessed during the execution
15+
/// of a program.
16+
///
17+
/// This attribute is valid on members whose type is <see cref="Type"/> or <see cref="string"/>.
18+
///
19+
/// When this attribute is applied to a location of type <see cref="string"/>, the assumption is
20+
/// that the string represents a fully qualified type name.
21+
///
22+
/// When this attribute is applied to a class, interface, or struct, the members specified
23+
/// can be accessed dynamically on <see cref="Type"/> instances returned from calling
24+
/// <see cref="object.GetType"/> on instances of that class, interface, or struct.
25+
///
26+
/// If the attribute is applied to a method it's treated as a special case and it implies
27+
/// the attribute should be applied to the "this" parameter of the method. As such the attribute
28+
/// should only be used on instance methods of types assignable to System.Type (or string, but no methods
29+
/// will use it there).
30+
/// </remarks>
31+
[AttributeUsage(
32+
AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter |
33+
AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Method |
34+
AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct,
35+
Inherited = false)]
36+
internal sealed class DynamicallyAccessedMembersAttribute : Attribute
37+
{
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="DynamicallyAccessedMembersAttribute"/> class
40+
/// with the specified member types.
41+
/// </summary>
42+
/// <param name="memberTypes">The types of members dynamically accessed.</param>
43+
public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes)
44+
{
45+
MemberTypes = memberTypes;
46+
}
47+
48+
/// <summary>
49+
/// Gets the <see cref="DynamicallyAccessedMemberTypes"/> which specifies the type
50+
/// of members dynamically accessed.
51+
/// </summary>
52+
public DynamicallyAccessedMemberTypes MemberTypes { get; }
53+
}
54+
55+
/// <summary>
56+
/// Specifies the types of members that are dynamically accessed.
57+
///
58+
/// This enumeration has a <see cref="FlagsAttribute"/> attribute that allows a
59+
/// bitwise combination of its member values.
60+
/// </summary>
61+
[Flags]
62+
internal enum DynamicallyAccessedMemberTypes
63+
{
64+
/// <summary>
65+
/// Specifies no members.
66+
/// </summary>
67+
None = 0,
68+
69+
/// <summary>
70+
/// Specifies the default, parameterless public constructor.
71+
/// </summary>
72+
PublicParameterlessConstructor = 0x0001,
73+
74+
/// <summary>
75+
/// Specifies all public constructors.
76+
/// </summary>
77+
PublicConstructors = 0x0002 | PublicParameterlessConstructor,
78+
79+
/// <summary>
80+
/// Specifies all non-public constructors.
81+
/// </summary>
82+
NonPublicConstructors = 0x0004,
83+
84+
/// <summary>
85+
/// Specifies all public methods.
86+
/// </summary>
87+
PublicMethods = 0x0008,
88+
89+
/// <summary>
90+
/// Specifies all non-public methods.
91+
/// </summary>
92+
NonPublicMethods = 0x0010,
93+
94+
/// <summary>
95+
/// Specifies all public fields.
96+
/// </summary>
97+
PublicFields = 0x0020,
98+
99+
/// <summary>
100+
/// Specifies all non-public fields.
101+
/// </summary>
102+
NonPublicFields = 0x0040,
103+
104+
/// <summary>
105+
/// Specifies all public nested types.
106+
/// </summary>
107+
PublicNestedTypes = 0x0080,
108+
109+
/// <summary>
110+
/// Specifies all non-public nested types.
111+
/// </summary>
112+
NonPublicNestedTypes = 0x0100,
113+
114+
/// <summary>
115+
/// Specifies all public properties.
116+
/// </summary>
117+
PublicProperties = 0x0200,
118+
119+
/// <summary>
120+
/// Specifies all non-public properties.
121+
/// </summary>
122+
NonPublicProperties = 0x0400,
123+
124+
/// <summary>
125+
/// Specifies all public events.
126+
/// </summary>
127+
PublicEvents = 0x0800,
128+
129+
/// <summary>
130+
/// Specifies all non-public events.
131+
/// </summary>
132+
NonPublicEvents = 0x1000,
133+
134+
/// <summary>
135+
/// Specifies all interfaces implemented by the type.
136+
/// </summary>
137+
Interfaces = 0x2000,
138+
139+
/// <summary>
140+
/// Specifies all members.
141+
/// </summary>
142+
All = ~None
143+
}
144+
#endif
145+
}

src/Microsoft.OpenApi/Extensions/EnumExtensions.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Reflection;
77
using Microsoft.OpenApi.Attributes;
8+
using Microsoft.OpenApi.Models;
89

910
namespace Microsoft.OpenApi.Extensions
1011
{
@@ -42,5 +43,107 @@ public static string GetDisplayName(this Enum enumValue)
4243
var attribute = enumValue.GetAttributeOfType<DisplayAttribute>();
4344
return attribute == null ? enumValue.ToString() : attribute.Name;
4445
}
46+
47+
/// <summary>
48+
/// Gets the enum display name for <see typeparamref="T"/> without the use of reflection.
49+
/// </summary>
50+
/// <typeparam name="T">The type of the enum value.</typeparam>
51+
/// <param name="enumValue">The enum value.</param>
52+
/// <returns>The display string to use.</returns>
53+
public static string GetDisplayName<T>(this T enumValue) where T : Enum
54+
{
55+
return enumValue switch
56+
{
57+
ParameterStyle parameterStyle => parameterStyle.GetDisplayName(),
58+
ParameterLocation parameterLocation => parameterLocation.GetDisplayName(),
59+
ReferenceType referenceType => referenceType.GetDisplayName(),
60+
OperationType operationType => operationType.GetDisplayName(),
61+
SecuritySchemeType securitySchemeType => securitySchemeType.GetDisplayName(),
62+
_ => enumValue.ToString()
63+
};
64+
}
65+
66+
/// <summary>
67+
/// Gets the enum display for name <see cref="ParameterStyle" /> without the use of reflection.
68+
/// </summary>
69+
/// <param name="parameterStyle">The enum value.</param>
70+
/// <returns>The display string to use.</returns>
71+
internal static string GetDisplayName(this ParameterStyle parameterStyle) => parameterStyle switch
72+
{
73+
ParameterStyle.Matrix => "matrix",
74+
ParameterStyle.Label => "label",
75+
ParameterStyle.Form => "form",
76+
ParameterStyle.Simple => "simple",
77+
ParameterStyle.SpaceDelimited => "spaceDelimited",
78+
ParameterStyle.PipeDelimited => "pipeDelimited",
79+
ParameterStyle.DeepObject => "deepObject",
80+
_ => parameterStyle.ToString()
81+
};
82+
83+
/// <summary>
84+
/// Gets the enum display for name <see cref="ParameterLocation" /> without the use of reflection.
85+
/// </summary>
86+
/// <param name="parameterLocation">The enum value.</param>
87+
/// <returns>The display string to use.</returns>
88+
internal static string GetDisplayName(this ParameterLocation parameterLocation) => parameterLocation switch
89+
{
90+
ParameterLocation.Query => "query",
91+
ParameterLocation.Header => "header",
92+
ParameterLocation.Path => "path",
93+
ParameterLocation.Cookie => "cookie",
94+
_ => parameterLocation.ToString()
95+
};
96+
97+
/// <summary>
98+
/// Gets the enum display for name <see cref="ReferenceType" /> without the use of reflection.
99+
/// </summary>
100+
/// <param name="referenceType">The enum value.</param>
101+
/// <returns>The display string to use.</returns>
102+
internal static string GetDisplayName(this ReferenceType referenceType) => referenceType switch
103+
{
104+
ReferenceType.Schema => "schemas",
105+
ReferenceType.Response => "responses",
106+
ReferenceType.Parameter => "parameters",
107+
ReferenceType.Example => "examples",
108+
ReferenceType.RequestBody => "requestBodies",
109+
ReferenceType.Header => "headers",
110+
ReferenceType.SecurityScheme => "securitySchemes",
111+
ReferenceType.Link => "links",
112+
ReferenceType.Callback => "callbacks",
113+
ReferenceType.Tag => "tags",
114+
_ => referenceType.ToString()
115+
};
116+
117+
/// <summary>
118+
/// Gets the enum display for name <see cref="OperationType" /> without the use of reflection.
119+
/// </summary>
120+
/// <param name="operationType">The enum value.</param>
121+
/// <returns>The display string to use.</returns>
122+
internal static string GetDisplayName(this OperationType operationType) => operationType switch
123+
{
124+
OperationType.Get => "get",
125+
OperationType.Put => "put",
126+
OperationType.Post => "post",
127+
OperationType.Delete => "delete",
128+
OperationType.Options => "options",
129+
OperationType.Head => "head",
130+
OperationType.Patch => "patch",
131+
OperationType.Trace => "trace",
132+
_ => operationType.ToString()
133+
};
134+
135+
/// <summary>
136+
/// Gets the enum display for name <see cref="SecuritySchemeType" /> without the use of reflection.
137+
/// </summary>
138+
/// <param name="securitySchemeType">The enum value.</param>
139+
/// <returns>The display string to use.</returns>
140+
internal static string GetDisplayName(this SecuritySchemeType securitySchemeType) => securitySchemeType switch
141+
{
142+
SecuritySchemeType.ApiKey => "apiKey",
143+
SecuritySchemeType.Http => "http",
144+
SecuritySchemeType.OAuth2 => "oauth2",
145+
SecuritySchemeType.OpenIdConnect => "openIdConnect",
146+
_ => securitySchemeType.ToString()
147+
};
45148
}
46149
}

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public string ReferenceV3
8383
return Id;
8484
}
8585

86-
return "#/components/" + Type.GetDisplayName() + "/" + Id;
86+
return "#/components/" + Type.Value.GetDisplayName() + "/" + Id;
8787
}
8888
}
8989

@@ -201,7 +201,7 @@ private string GetExternalReferenceV3()
201201
return ExternalResource + "#" + Id;
202202
}
203203

204-
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
204+
return ExternalResource + "#/components/" + Type.Value.GetDisplayName() + "/"+ Id;
205205
}
206206

207207
return ExternalResource;

0 commit comments

Comments
 (0)