Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
Fix serializing readonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 committed Feb 5, 2020
1 parent 24177ff commit 65c527c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
17 changes: 17 additions & 0 deletions spec/cr_serializer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -736,5 +736,22 @@ describe CRS do
p.class.should eq VirtualProperty
end
end

describe CRS::ReadOnly do
it "should include ReadOnly properties" do
properties = ReadOnly.new.serialization_properties
properties.size.should eq 2

p = properties[0]

p.name.should eq "name"
p.external_name.should eq "name"

p = properties[1]

p.name.should eq "password"
p.external_name.should eq "password"
end
end
end
end
6 changes: 4 additions & 2 deletions spec/models/read_only.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class ReadOnly
include CrSerializer

property name : String
def initialize; end

property name : String = "name"

@[CRS::ReadOnly]
property password : String?
property password : String? = nil
end
1 change: 0 additions & 1 deletion src/CrSerializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ module CrSerializer
ivars = @type.instance_vars
.reject { |ivar| ivar.annotation(CRS::Skip) }
.reject { |ivar| ivar.annotation(CRS::IgnoreOnSerialize) }
.reject { |ivar| (ann = ivar.annotation(CRS::ReadOnly)); ann && !ivar.has_default_value? && !ivar.type.nilable? ? raise "#{@type}##{ivar.name} is read-only but is not nilable nor has a default value" : ann }
.reject do |ivar|
not_exposed = (ann = @type.annotation(CRS::ExclusionPolicy)) && ann[0] == :all && !ivar.annotation(CRS::Expose)
excluded = (ann = @type.annotation(CRS::ExclusionPolicy)) && ann[0] == :none && ivar.annotation(CRS::Exclude)
Expand Down

0 comments on commit 65c527c

Please sign in to comment.