mirror of
https://github.com/ditkrg/schemable.git
synced 2026-01-22 22:26:41 +00:00
Refactor generate_schema method to handle nullable relationships
This commit is contained in:
parent
213a265ddd
commit
dbfb9c1dd7
@ -84,36 +84,43 @@ module Schemable
|
|||||||
#
|
#
|
||||||
# @return [Hash] The generated schema for the relationship.
|
# @return [Hash] The generated schema for the relationship.
|
||||||
def generate_schema(type_name, collection: false)
|
def generate_schema(type_name, collection: false)
|
||||||
if collection
|
schema = if collection
|
||||||
{
|
{
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
data: {
|
data: {
|
||||||
type: :array,
|
type: :array,
|
||||||
items: {
|
items: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: :string },
|
id: { type: :string },
|
||||||
type: { type: :string, default: type_name }
|
type: { type: :string, default: type_name }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
data: {
|
data: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: :string },
|
id: { type: :string },
|
||||||
type: { type: :string, default: type_name }
|
type: { type: :string, default: type_name }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Modify the schema to nullable if the relationship is in nullable
|
||||||
|
is_relation_nullable = @model_definition.nullable_relationships.include?(type_name)
|
||||||
|
|
||||||
|
return schema unless is_relation_nullable
|
||||||
|
|
||||||
|
@schema_modifier.add_properties(schema, { nullable: true }, 'properties.data')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user