Skip to content

Serialize derived attributes during xml and key_value serialization#660

Merged
HassanAkbar merged 4 commits intomainfrom
serialize-derived-attributes
Apr 27, 2026
Merged

Serialize derived attributes during xml and key_value serialization#660
HassanAkbar merged 4 commits intomainfrom
serialize-derived-attributes

Conversation

@HassanAkbar
Copy link
Copy Markdown
Member

fixes #639

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes #639 by ensuring derived (method:) attributes are not incorrectly treated as “defaulted” after XML deserialization, so they continue to serialize in downstream formats.

Changes:

  • Add derived-attribute detection to bypass “skip default” logic in RenderPolicy.
  • Ensure MappingRule#render? renders derived attributes even when using_default? is true.
  • Add an RSpec covering derived attribute serialization after XML deserialization (YAML + JSON/key-value).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/lutaml/model/render_policy.rb Avoids skipping derived attributes due to default-render policy.
lib/lutaml/model/mapping/mapping_rule.rb Forces derived attributes to render even when instance reports “using default”.
spec/lutaml/model/derived_attribute_serialization_spec.rb Adds regression coverage for YAML + JSON after XML deserialization.
Comments suppressed due to low confidence (1)

lib/lutaml/model/render_policy.rb:100

  • should_skip_default? now calls derived_attribute? unconditionally, which does an attribute lookup on every non-nil/non-empty value even when using_default? is false (the common case for instances created via .new). To avoid a potential serialization hot-path regression, consider only checking derived_attribute? inside the existing using_default? && !render_default branch (i.e., only when the rule would otherwise be skipped due to defaults).
      def should_skip_default?(value, rule, context_obj, attr_name)
        return false if derived_attribute?(context_obj, attr_name)

        # Skip if context object is using default and render_default is false
        # But for collections, check if they were mutated (non-empty)
        if context_obj.respond_to?(:using_default?) &&
            context_obj.using_default?(attr_name) &&
            !extract_option(rule, :render_default)
          # For collections: if mutated to non-empty, serialize them

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/lutaml/model/mapping/mapping_rule.rb Outdated
Comment thread spec/lutaml/model/derived_attribute_serialization_spec.rb
# frozen_string_literal: true

require "spec_helper"
class DerivedAttributeParent < Lutaml::Model::Serializable
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For custom classes defined in specs, we should wrap this in a module just for this spec to prevent pollution. We should add this rule to CLAUDE.md or AGENT.md. Or, use anonymous classes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@ronaldtse wrapped the class inside a module to prevent pollution. Let me know if you want me to change anything else. Thanks.

@ronaldtse
Copy link
Copy Markdown
Contributor

@HassanAkbar LGTM feel free to merge when ready.

@HassanAkbar
Copy link
Copy Markdown
Member Author

@ronaldtse Sure, I’ll merge it once the CI is green.

@HassanAkbar HassanAkbar marked this pull request as ready for review April 27, 2026 08:18
@HassanAkbar HassanAkbar merged commit 6bf8533 into main Apr 27, 2026
72 of 88 checks passed
@HassanAkbar HassanAkbar deleted the serialize-derived-attributes branch April 27, 2026 08:44
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.

Derived (method:) attributes incorrectly skipped in YAML serialization after XML deserialization

3 participants