From 74dc095d1f5e5d50b423c5e95c9a83da9a231c2c Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Wed, 18 Nov 2026 17:10:02 +1100 Subject: [PATCH] fix: describe converted model timestamps as integers --- spec/timestamp_schema_spec.cr | 33 +++++++++++++++++++++++++++++++++ src/placeos-models/edge.cr | 3 +-- src/placeos-models/shortener.cr | 4 ++-- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 spec/timestamp_schema_spec.cr diff --git a/spec/timestamp_schema_spec.cr b/spec/timestamp_schema_spec.cr new file mode 100644 index 00000000..24b84455 --- /dev/null +++ b/spec/timestamp_schema_spec.cr @@ -0,0 +1,33 @@ +require "json-schema" +require "spec" +require "../src/placeos-models" + +module PlaceOS::Model + describe "Epoch timestamp schemas" do + it "describes the Edge heartbeat using its serialized integer type" do + time = Time.unix(1_700_000_000) + edge = Edge.new + edge.last_seen = time + JSON.parse(edge.to_json)["last_seen"].as_i64.should eq time.to_unix + + refs = JSON::Schema::Definitions.new("#/components/schemas/") + properties = JSON.parse(Edge.json_schema(true, refs).to_json)["properties"] + properties["last_seen"].should eq JSON.parse(%({"type":"integer","format":"Int64","nullable":true})) + end + + it "describes the Shortener validity timestamps using their serialized integer type" do + start = Time.unix(1_700_000_000) + finish = start + 1.hour + shortener = Shortener.new(valid_from: start, valid_until: finish) + json = JSON.parse(shortener.to_json) + json["valid_from"].as_i64.should eq start.to_unix + json["valid_until"].as_i64.should eq finish.to_unix + + refs = JSON::Schema::Definitions.new("#/components/schemas/") + properties = JSON.parse(Shortener.json_schema(true, refs).to_json)["properties"] + epoch = JSON.parse(%({"type":"integer","format":"Int64","nullable":true})) + properties["valid_from"].should eq epoch + properties["valid_until"].should eq epoch + end + end +end diff --git a/src/placeos-models/edge.cr b/src/placeos-models/edge.cr index b65ce340..2fa215ae 100644 --- a/src/placeos-models/edge.cr +++ b/src/placeos-models/edge.cr @@ -16,8 +16,7 @@ module PlaceOS::Model attribute user_id : String, mass_assignment: false - @[JSON::Field(type: "integer", format: "Int64")] - attribute last_seen : Time?, converter: Time::EpochConverterOptional, mass_assignment: false + attribute last_seen : Time?, converter: Time::EpochConverterOptional, type: "integer", format: "Int64", mass_assignment: false attribute online : Bool = false, mass_assignment: false @[JSON::Field(ignore: true)] diff --git a/src/placeos-models/shortener.cr b/src/placeos-models/shortener.cr index 79f16c0b..c9bf19b2 100644 --- a/src/placeos-models/shortener.cr +++ b/src/placeos-models/shortener.cr @@ -17,8 +17,8 @@ module PlaceOS::Model attribute redirect_count : Int64 = 0 attribute enabled : Bool = true - attribute valid_from : Time? = nil, converter: Time::EpochConverterOptional - attribute valid_until : Time? = nil, converter: Time::EpochConverterOptional + attribute valid_from : Time? = nil, converter: Time::EpochConverterOptional, type: "integer", format: "Int64" + attribute valid_until : Time? = nil, converter: Time::EpochConverterOptional, type: "integer", format: "Int64" belongs_to Authority