Skip to content

Make OpenAPI.NET library trim-compatible - #1717

Merged
Maggie Kimani (MaggieKimani1) merged 20 commits into
microsoft:vnextfrom
captainsafia:safia/trimmable
Jul 22, 2024
Merged

Maggie Kimani (MaggieKimani1) merged 20 commits into
microsoft:vnextfrom
captainsafia:safia/trimmable

Conversation

@captainsafia

@captainsafia Safia Abdalla (captainsafia) commented Jul 4, 2024 •

Copy link
Copy Markdown
Contributor

This PR removes the use of reflection in GetDisplayName for enums and adds [DynamicallyAccessMembers] attributes to CloneFromCopyConstructor.

Prior to this change, using the Microsoft.OpenApi package in an application with trimming enabled would present the following warnings:

    /_/src/Microsoft.OpenApi/Extensions/EnumExtensions.cs(27): Trim analysis warning IL2075: Microsoft.OpenApi.Extensions.EnumExtensions.GetAttributeOfType<T>(Enum): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMember(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    /_/src/Microsoft.OpenApi/Any/OpenApiAnyCloneHelper.cs(23): Trim analysis warning IL2075: Microsoft.OpenApi.Any.OpenApiAnyCloneHelper.CloneFromCopyConstructor(IOpenApiAny): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructors()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Closes #1715
Closes #1114

@baywet Vincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be interesting to set-up a trimming test project to ensure no regressions happen in the future here.
microsoft/kiota-dotnet#291
microsoft/kiota-dotnet#295
microsoft/kiota-dotnet#296

@captainsafia

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews Darrel (@darrelmiller) and Maggie Kimani (@MaggieKimani1) -- I spotted a few more issues after adding a TrimmingTest project (mostly related to ValidationRuleset) so we'll need to hold off on merging this a bit longer.

@captainsafia

Copy link
Copy Markdown
Contributor Author

Vincent Biret (@baywet) Eric Erhardt (@eerhardt) I think this is good for one last review...

@baywet Vincent Biret (baywet) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this great contribution! And for the ongoing discussion here.

@baywet

Copy link
Copy Markdown
Member

Maggie Kimani (@MaggieKimani1) for final review and merge

@eerhardt Eric Erhardt (eerhardt) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
@MaggieKimani1

Copy link
Copy Markdown
Contributor

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid use of unbounded reflection to resolve Enum display names Make simple usage of Microsoft.OpenAPI trimmable

7 participants