mirror of
https://github.com/ditkrg/schemable.git
synced 2026-01-25 07:36:40 +00:00
Updates RelationshipSchemaGenerator to correctly generate schemas
This commit is contained in:
parent
e046194f37
commit
e2ba1a7b8c
@ -33,20 +33,9 @@ module Schemable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result = {
|
result = relation_type == :belongs_to ? generate_schema(definition.model_name) : generate_schema(definition.model_name, collection: true)
|
||||||
type: :object,
|
|
||||||
properties: {
|
|
||||||
data: {
|
|
||||||
type: :object,
|
|
||||||
properties: {
|
|
||||||
id: { type: :string },
|
|
||||||
type: { type: :string, default: definition[:definition].model_name }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result = non_expanded_data_properties if !expand || @relationships_to_exclude_from_expansion.include?(definition[:definition].model_name)
|
result = non_expanded_data_properties if !expand || @relationships_to_exclude_from_expansion.include?(definition.model_name)
|
||||||
|
|
||||||
schema[:properties].merge!(relation => result)
|
schema[:properties].merge!(relation => result)
|
||||||
end
|
end
|
||||||
@ -62,5 +51,38 @@ module Schemable
|
|||||||
|
|
||||||
schema
|
schema
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def generate_schema(type_name, collection: false)
|
||||||
|
if collection
|
||||||
|
{
|
||||||
|
type: :object,
|
||||||
|
properties: {
|
||||||
|
data: {
|
||||||
|
type: :array,
|
||||||
|
items: {
|
||||||
|
type: :object,
|
||||||
|
properties: {
|
||||||
|
id: { type: :string },
|
||||||
|
type: { type: :string, default: type_name }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
type: :object,
|
||||||
|
properties: {
|
||||||
|
data: {
|
||||||
|
type: :object,
|
||||||
|
properties: {
|
||||||
|
id: { type: :string },
|
||||||
|
type: { type: :string, default: type_name }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -10,5 +10,7 @@ module Schemable
|
|||||||
def initialize: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> void
|
def initialize: (Definition, ?relationships_to_exclude_from_expansion: Array[String], ?expand: bool) -> void
|
||||||
|
|
||||||
def generate: -> (Hash[Symbol, any] | Array[Hash[Symbol, any]])
|
def generate: -> (Hash[Symbol, any] | Array[Hash[Symbol, any]])
|
||||||
|
|
||||||
|
def generate_schema: (String, ?collection: bool) -> Hash[Symbol, any]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user