Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AOT support #1377

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open

AOT support #1377

wants to merge 21 commits into from

Conversation

notelgnis
Copy link

@notelgnis notelgnis commented May 7, 2024

Description

This pull request introduces support for Ahead-Of-Time (AOT) compilation in the library. As the original branch has already had System.Text.Json serialisation implemented, the main effort was to make all the API requests and types involved in communication AOT friendly and to remove everything that wasn't AOT friendly. This introduces another source generator in addition to EnumSerializer.Generator which is standard System.Text.Json source generator.

The library API has been preserved, however, if you for any reason want to serialise or deserialise a request manually in your code (say you get a raw input from your web hook) you'll have to use TelegramBotClientJsonSerializerContext like this:

Update update =  
    JsonSerializer.Deserialize(rawTelegramUpdate.Update, TelegramBotClientJsonSerializerContext.Instance.Update);

The TelegramBotClientJsonSerializerContext contains all the generated code needed for serialisation and deserialisation. Another thing worth mentioning is this TelegramBotClientJsonSerializerContext.Instance static member which should be used instead of default implementation. The standard implementation of JsonSerializerContext generator generates TelegramBotClientJsonSerializerContext.Default but it lacks some important JsonSerializerOptions like JsonNamingPolicy.SnakeCaseLower etc. The Instance instance has them. You can also change default JsonSerializerOptions in Instance like this:

TelegramBotClientJsonSerializerContext.JsonSerializerOptions = new JsonSerializerOptions { // your options }

However, if you just use the library API methods for sending or polling requests you won't need this and there are no changes needed in your code as well.

Changes

There are some major internal changes in the library though. First comes from the necessity of having two source generators and the fact that the one depends on the output of the other. Two of them don't properly work in the same project so the library has been split in two: Telegram.Bot and Telegram.Bot.Base. The last one includes the code generated by EnumSerializer.Generatorand the first the code generated by System.Text.Json source generator. So, there will be most likely two Nuget packages as well.

Other modifications include removing JsonSerializerOptionsProvider and some convenient attributes like CustomJsonDerivedTypeAttribute, CustomJsonPolymorphicAttribute, PolymorphicJsonConverter,PolymorphicJsonConverterFactory as they weren't AOT friendly.

Unit Tests Changes

Unit tests had to be changed as well. To test the same serialisation context that is used in actual API calls it was necessary to test serialisation with the actual (not container-like) types.

tuscen and others added 21 commits March 27, 2024 22:44
# Conflicts:
#	src/Telegram.Bot/Requests/Available methods/Messages/Location/SendLocationRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/Location/SendVenueRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendAnimationRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendAudioRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendChatActionRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendContactRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendDiceRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendDocumentRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendMediaGroupRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendMessageRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendPhotoRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendPollRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendVideoNoteRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendVideoRequest.cs
#	src/Telegram.Bot/Requests/Available methods/Messages/SendVoiceRequest.cs
#	src/Telegram.Bot/Requests/Games/SendGameRequest.cs
#	src/Telegram.Bot/Requests/Stickers/CreateNewStickerSetRequest.cs
#	src/Telegram.Bot/Requests/Stickers/SendStickerRequest.cs
#	src/Telegram.Bot/Types/ReplyMarkups/KeyboardButtonRequestChat.cs
#	src/Telegram.Bot/Types/StickerSet.cs
#	src/Telegram.Bot/Types/UsersShared.cs
#	test/Telegram.Bot.Tests.Unit/EnumConverter/UpdateTypeConverterTests.cs
#	test/Telegram.Bot.Tests.Unit/Serialization/InputStickerSerializationTests.cs
@notelgnis
Copy link
Author

Another thing I forgot to mention is that AOT warnings only start appearing in .NET 7. However, the target frameworks have been left unchanged for compatibility reasons. Therefore, to check for any new AOT compatibility warnings, it is necessary to temporarily switch the target framework to .NET 7 or higher.

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

Successfully merging this pull request may close these issues.

None yet

3 participants