Add expand_nested parameter to generate method

This commit is contained in:
Muhammad Nawzad 2024-01-30 16:29:07 +03:00
parent 1afb31bccb
commit 49992ff75b
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
2 changed files with 8 additions and 4 deletions

View File

@ -27,12 +27,16 @@ module Schemable
# @param expand [Boolean] Whether to include the included resources in the schema. # @param expand [Boolean] Whether to include the included resources in the schema.
# @param relationships_to_exclude_from_expansion [Array] The relationships to exclude from expansion in the schema. # @param relationships_to_exclude_from_expansion [Array] The relationships to exclude from expansion in the schema.
# @param collection [Boolean] Whether the response is for a collection of resources. # @param collection [Boolean] Whether the response is for a collection of resources.
# @param expand_nested [Boolean] Whether to include the nested relationships in the schema.
# #
# @example # @example
# schema = generator.generate(expand: true, relationships_to_exclude_from_expansion: ['some_relationship'], collection: true) # schema = generator.generate(expand: true, relationships_to_exclude_from_expansion: ['some_relationship'], collection: true, expand_nested: true)
# #
# @return [Hash] The generated schema. # @return [Hash] The generated schema.
def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false) def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false, expand_nested: false)
# Override expand_nested if infer_expand_nested_from_expand is true
expand_nested = expand if @configuration.infer_expand_nested_from_expand
data = { data = {
type: :object, type: :object,
properties: { properties: {
@ -51,7 +55,7 @@ module Schemable
schema = collection ? { data: { type: :array, items: data } } : { data: } schema = collection ? { data: { type: :array, items: data } } : { data: }
if expand if expand
included_schema = IncludedSchemaGenerator.new(@model_definition).generate(expand:, relationships_to_exclude_from_expansion:) included_schema = IncludedSchemaGenerator.new(@model_definition).generate(expand: expand_nested, relationships_to_exclude_from_expansion:)
@schema_modifier.add_properties(schema, included_schema, '.') @schema_modifier.add_properties(schema, included_schema, '.')
end end

View File

@ -8,6 +8,6 @@ module Schemable
def initialize: (Definition) -> void def initialize: (Definition) -> void
def meta: -> Hash[Symbol, any] def meta: -> Hash[Symbol, any]
def jsonapi: -> Hash[Symbol, any] def jsonapi: -> Hash[Symbol, any]
def generate: (expand: bool, relationships_to_exclude_from_expansion: Array[Symbol], collection: bool) -> Hash[Symbol, any] def generate: (expand: bool, relationships_to_exclude_from_expansion: Array[Symbol], collection: bool, expand_nested: bool) -> Hash[Symbol, any]
end end
end end