Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ module PlaceOS::Model
text_model : String? = nil,
)
row = SignageAIProvider.new
row.name = name || Faker::Hacker.noun
row.name = name || "provider-#{UUID.random}"
row.provider = provider
row.authority_id = authority.try(&.id.as(String))
row.credentials = credentials
Expand Down
50 changes: 50 additions & 0 deletions spec/openapi_model_fields_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "json-schema"
require "spec"
require "../src/placeos-models"

module PlaceOS::Model
describe "Model OpenAPI field types" do
it "describes serialized scope strings in JWTs and API keys" do
scopes = [UserJWT::Scope::PUBLIC, UserJWT::Scope.new("custom", UserJWT::Scope::Access::Read)]
JSON.parse(scopes.to_json).should eq JSON.parse(%(["public","custom.Read"]))

refs = JSON::Schema::Definitions.new("#/components/schemas/")
jwt_scope = JSON.parse(UserJWT.json_schema(refs: refs).to_json)["properties"]["scope"]
key_scopes = JSON.parse(ApiKey.json_schema(true, refs).to_json)["properties"]["scopes"]
string_schema = JSON.parse(%({"type":"string"}))
jwt_scope["items"].should eq string_schema
key_scopes["items"].should eq string_schema
end

it "describes metadata entries for both User metadata views" do
refs = JSON::Schema::Definitions.new("#/components/schemas/")
{User::AdminMetadataResponse, User::PublicMetadataResponse}.each do |response|
properties = JSON.parse(response.json_schema(true, refs).to_json)["properties"]
metadata = properties["associated_metadata"]
metadata["additionalProperties"]["$ref"].should eq "#/components/schemas/PlaceOS.Model.Metadata.Interface"
end
end

it "serializes Driver schema objects as JSON strings and preserves other JSON values" do
driver = Driver.new
driver.json_schema = JSON.parse(%({"type":"object"}))
JSON.parse(driver.to_json)["json_schema"].as_s.should eq %({"type":"object"})

{"[]", "true", "7", %("schema"), "null"}.each do |value|
driver.json_schema = JSON.parse(value)
JSON.parse(driver.to_json)["json_schema"].should eq JSON.parse(value)
end
end

it "serializes Guest event metadata as an object" do
metadata = EventMetadata.new(event_id: "event-1")
metadata.render_linked_bookings = false
guest = Guest.new(email: "visitor@example.com")
guest.event_metadata = metadata.to_json

JSON.parse(guest.to_json)["event_metadata"].should eq JSON.parse(metadata.to_json)
guest.event_metadata = nil
JSON.parse(guest.to_json)["event_metadata"]?.should be_nil
end
end
end
21 changes: 21 additions & 0 deletions spec/user_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,27 @@ module PlaceOS::Model
end
end

it "preserves the metadata payload in typed response views" do
user = Generator.user.save!
empty_payload = JSON.parse(user.to_public_metadata_struct.to_json)
empty_payload["associated_metadata"].as_h.should be_empty

metadata = Generator.metadata(name: "profile", parent: user, modifier: user)
metadata.details = JSON.parse(%([null,{"active":true}]))
metadata.save!

{user.to_admin_metadata_struct, user.to_public_metadata_struct}.each do |response|
payload = JSON.parse(response.to_json)
payload["associated_metadata"].as_h.keys.should eq ["profile"]
payload["associated_metadata"]["profile"]["details"].should eq metadata.details

payload.as_h["extra"] = JSON.parse(%({"value":null}))
parsed = response.class.from_json(payload.to_json)
JSON.parse(parsed.to_json)["associated_metadata"].should eq payload["associated_metadata"]
JSON.parse(parsed.to_json)["extra"].should eq payload["extra"]
end
end

describe "#subsystem_access" do
Spec.before_each do
History.clear
Expand Down
3 changes: 2 additions & 1 deletion src/placeos-models/driver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module PlaceOS::Model

attribute name : String, sanitize: :text, es_subfield: "keyword"
attribute description : String = "", sanitize: :common
attribute json_schema : JSON::Any = JSON::Any.new({} of String => JSON::Any), converter: JSON::Any::StringConverter, es_type: "text"
attribute json_schema : JSON::Any = JSON::Any.new({} of String => JSON::Any), converter: JSON::Any::StringConverter, es_type: "text",
description: "Schema data. JSON objects are encoded as JSON strings in responses."

attribute default_uri : String?
attribute default_port : Int32?
Expand Down
2 changes: 1 addition & 1 deletion src/placeos-models/guest.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module PlaceOS::Model
attribute visit_expected : Bool?, persistence: false, show: true, ignore_deserialize: true
attribute booking : Booking?, persistence: false, show: true, ignore_deserialize: true
attribute event : PlaceCalendar::Event?, persistence: false, show: true, ignore_deserialize: true
attribute event_metadata : String?, persistence: false, converter: String::RawConverter, show: true, ignore_deserialize: true
attribute event_metadata : String?, persistence: false, converter: String::RawConverter, type: "object", show: true, ignore_deserialize: true

attribute booking_rep : Booking? = nil, persistence: false, show: true, json_key: "booking", ignore_deserialize: true

Expand Down
2 changes: 1 addition & 1 deletion src/placeos-models/user.cr
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ module PlaceOS::Model
define_to_json :admin, only: ADMIN_DATA, methods: :id
define_to_json :admin_metadata, only: ADMIN_DATA, methods: [:id, :associated_metadata, :subsystem_access]

def associated_metadata
def associated_metadata : Hash(String, Model::Metadata::Interface)
Model::Metadata.build_metadata(self)
end

Expand Down
4 changes: 4 additions & 0 deletions src/placeos-models/user_jwt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ module PlaceOS::Model
def to_json(builder : JSON::Builder)
builder.string(to_s)
end

def self.json_schema(_openapi : Bool? = nil)
{type: "string"}
end
end

struct Metadata
Expand Down
Loading