lib/schemable/response_schema_generator.rb in schemable-1.0.2 vs lib/schemable/response_schema_generator.rb in schemable-1.0.3
- old
+ new
@@ -25,16 +25,20 @@
# It also adds meta and jsonapi information to 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 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
- # 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.
- 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 = {
type: :object,
properties: {
type: { type: :string, default: @model_definition.model_name },
id: { type: :string },
@@ -49,10 +53,10 @@
}
schema = collection ? { data: { type: :array, items: data } } : { data: }
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, '.')
end
@schema_modifier.add_properties(schema, { meta: }, '.') if collection
@schema_modifier.add_properties(schema, { jsonapi: }, '.')