Skip to content

Bug: serialization getter must return the same type as the property #513

@syeopite

Description

@syeopite
require "uri"
require "athena-serializer"

struct Website
  include ASR::Serializable

  @[ASRA::Accessor(getter: get_url)]
  property url : URI = URI.new()

  def get_url : String
    return @url.host || ""
  end

  def initialize(@url : URI)
  end
end

website = Website.new(URI.parse("https://example.com"))
puts ASR.serializer.serialize website, :json
Code in macro 'included'

 27 | {% if true %}
      ^
Called macro defined in macro 'included'

 27 | {% if true %}

Which expanded to:

 > 32 | external_name: "url",
 > 33 | annotation_configurations: ADI::AnnotationConfigurations.new({} of ADI::AnnotationConfigurations::Classes => Array(ADI::AnnotationConfigurations::ConfigurationBase)),
 > 34 | value: get_url,
        ^
Error: expected argument 'value' to 'Athena::Serializer::PropertyMetadata(URI, URI, Website).new' to be URI, not String

This leads to a compile error instead of the expected {"url": "example.com"}

This can be worked around by using a VirtualProperty but I feel like this pattern is more natural

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions