Repository navigation
feat: add text serialization #177
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
Merged
Vincent Biret (baywet)
merged 6 commits into
microsoft:main
from
andreaTP:feat/serialization-text
Oct 15, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e0a18b
feat: add text serialization
andreaTP 7ceaf06
ci: add the text serialization gem to the build and release
andreaTP a69f4cc
refactor: count an empty string as the single value a text writer holds
andreaTP a5a7d8a
refactor: read unparseable text values as nil
andreaTP 738b008
refactor: read a text date, time or date time only in its own shape
andreaTP b796bda
fix: write a zero duration as PT0S
andreaTP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| source 'https://rubygems.org' | ||
|
|
||
| ruby '>= 3.3.0' | ||
|
|
||
| gem 'microsoft_kiota_abstractions', path: '../../abstractions' | ||
| gem 'rake', '~> 13.0' | ||
| gem 'rspec', '~> 3.0' | ||
|
|
||
| gemspec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'bundler/gem_tasks' | ||
| require 'rspec/core/rake_task' | ||
|
|
||
| RSpec::Core::RakeTask.new(:spec) | ||
|
|
||
| task default: :spec |
10 changes: 10 additions & 0 deletions
10
components/serialization/text/lib/microsoft_kiota_serialization_text.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'microsoft_kiota_serialization_text/text_parse_node' | ||
| require_relative 'microsoft_kiota_serialization_text/text_parse_node_factory' | ||
| require_relative 'microsoft_kiota_serialization_text/text_serialization_writer' | ||
| require_relative 'microsoft_kiota_serialization_text/text_serialization_writer_factory' | ||
| require_relative 'microsoft_kiota_serialization_text/version' | ||
|
|
||
| module MicrosoftKiotaSerializationText | ||
| end |
8 changes: 8 additions & 0 deletions
8
components/serialization/text/lib/microsoft_kiota_serialization_text/encoding.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module MicrosoftKiotaSerializationText | ||
| # bytes are read as UTF-8; a string Faraday tagged with its charset is converted | ||
| def self.to_utf8(text) | ||
| text.encoding == Encoding::BINARY ? text.dup.force_encoding(Encoding::UTF_8) : text.encode(Encoding::UTF_8) | ||
| end | ||
| end |
117 changes: 117 additions & 0 deletions
117
components/serialization/text/lib/microsoft_kiota_serialization_text/text_parse_node.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'time' | ||
| require 'date' | ||
| require 'uuidtools' | ||
| require 'microsoft_kiota_abstractions' | ||
|
|
||
| module MicrosoftKiotaSerializationText | ||
| class TextParseNode | ||
| include MicrosoftKiotaAbstractions::ParseNode | ||
|
|
||
| NO_STRUCTURED_DATA_MESSAGE = 'Text does not support structured data' | ||
|
|
||
| # temporal readers take only their own shape, as in the form gem, so a union picks the right member | ||
| DATE = /\A\d{4}-\d{2}-\d{2}\z/ | ||
| TIME = /\A\d{2}:\d{2}(:\d{2}(\.\d+)?)?\z/ | ||
| DATE_TIME = /\A\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?([Zz]|[+-]\d{2}(:?\d{2})?)?\z/ | ||
|
|
||
| def initialize(text) | ||
| @text = text | ||
| end | ||
|
|
||
| def get_string_value | ||
| @text unless @text.nil? || @text.empty? | ||
| end | ||
|
|
||
| def get_boolean_value | ||
| case scalar_text&.downcase | ||
| when 'true' then true | ||
| when 'false' then false | ||
| end | ||
| end | ||
|
|
||
| def get_number_value | ||
| text = scalar_text | ||
| Integer(text, 10, exception: false) if text | ||
| end | ||
|
|
||
| def get_float_value | ||
| text = scalar_text | ||
| Float(text, exception: false) if text | ||
| end | ||
|
|
||
| def get_guid_value | ||
| parsed { |text| UUIDTools::UUID.parse(text) } | ||
| end | ||
|
|
||
| def get_date_value | ||
| parsed { |text| Date.parse(text) if DATE.match?(text) } | ||
| end | ||
|
|
||
| def get_time_value | ||
| parsed { |text| Time.parse(text) if TIME.match?(text) } | ||
| end | ||
|
|
||
| def get_date_time_value | ||
| parsed { |text| DateTime.parse(text) if DATE_TIME.match?(text) } | ||
| end | ||
|
|
||
| def get_duration_value | ||
| parsed { |text| MicrosoftKiotaAbstractions::ISODuration.new(text) } | ||
| end | ||
|
|
||
| def get_byte_array_value | ||
| scalar_text&.unpack1('m') | ||
| end | ||
|
|
||
| def get_enum_value(type) | ||
| text = scalar_text | ||
| resolve_enum_member(type, text) if text | ||
| end | ||
|
|
||
| def get_child_node(_name) | ||
| raise StandardError, NO_STRUCTURED_DATA_MESSAGE | ||
| end | ||
|
|
||
| def get_collection_of_primitive_values(_type) | ||
| raise StandardError, NO_STRUCTURED_DATA_MESSAGE | ||
| end | ||
|
|
||
| def get_collection_of_object_values(_factory) | ||
| raise StandardError, NO_STRUCTURED_DATA_MESSAGE | ||
| end | ||
|
|
||
| def get_collection_of_enum_values(_type) | ||
| raise StandardError, NO_STRUCTURED_DATA_MESSAGE | ||
| end | ||
|
|
||
| def get_object_value(_factory) | ||
| raise StandardError, NO_STRUCTURED_DATA_MESSAGE | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # text that does not parse reads as nil, as .NET's TryParse and the form gem do | ||
| def parsed | ||
| text = scalar_text | ||
| yield text if text | ||
| rescue StandardError | ||
| nil | ||
| end | ||
|
|
||
| # typed values ignore surrounding whitespace, as .NET's TryParse does | ||
| def scalar_text | ||
| text = get_string_value&.strip | ||
| text unless text.nil? || text.empty? | ||
| end | ||
|
|
||
| # same resolution as JsonParseNode: the wire value first, then the member name | ||
| def resolve_enum_member(type, raw) | ||
| return raw.to_sym unless type.is_a?(Hash) | ||
|
|
||
| type.each_value { |value| return value if value.to_s == raw } | ||
| type[raw.to_sym] || type[(raw[0].to_s.upcase + raw[1..].to_s).to_sym] | ||
| end | ||
| end | ||
| end |
22 changes: 22 additions & 0 deletions
22
...ents/serialization/text/lib/microsoft_kiota_serialization_text/text_parse_node_factory.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'microsoft_kiota_abstractions' | ||
| require_relative 'encoding' | ||
|
|
||
| module MicrosoftKiotaSerializationText | ||
| class TextParseNodeFactory | ||
| include MicrosoftKiotaAbstractions::ParseNodeFactory | ||
|
|
||
| def get_valid_content_type | ||
| 'text/plain' | ||
| end | ||
|
|
||
| def get_parse_node(content_type, content) | ||
| raise ArgumentError, 'content type cannot be undefined or empty' if content_type.nil? || content_type.empty? | ||
| raise ArgumentError, "expected a #{get_valid_content_type} content type" unless get_valid_content_type.casecmp?(content_type) | ||
| raise ArgumentError, 'content cannot be undefined or empty' if content.nil? || content.empty? | ||
|
|
||
| TextParseNode.new(MicrosoftKiotaSerializationText.to_utf8(content).delete_prefix("\uFEFF")) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.