From e5229586af62856cf3df6c641c24730b9ccc2dbb Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Fri, 17 Nov 2023 17:15:23 +0300 Subject: [PATCH] Allows custom meta for response --- lib/schemable/configuration.rb | 4 ++ lib/schemable/response_schema_generator.rb | 53 ++++++++++++--------- sig/schemable/configuration.rbs | 6 ++- sig/schemable/response_schema_generator.rbs | 1 + 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/lib/schemable/configuration.rb b/lib/schemable/configuration.rb index 09d1b57..5dd8b87 100644 --- a/lib/schemable/configuration.rb +++ b/lib/schemable/configuration.rb @@ -9,12 +9,14 @@ module Schemable :orm, :float_as_string, :decimal_as_string, + :pagination_enabled, :custom_type_mappers, :disable_factory_bot, :use_serialized_instance, :custom_defined_enum_method, :enum_prefix_for_simple_enum, :enum_suffix_for_simple_enum, + :custom_meta_response_schema, :infer_attributes_from_custom_method, :infer_attributes_from_jsonapi_serializable ) @@ -24,10 +26,12 @@ module Schemable @orm = :active_record # orm options are :active_record, :mongoid @float_as_string = false @custom_type_mappers = {} + @pagination_enabled = true @decimal_as_string = false @disable_factory_bot = true @use_serialized_instance = false @custom_defined_enum_method = nil + @custom_meta_response_schema = nil @enum_prefix_for_simple_enum = nil @enum_suffix_for_simple_enum = nil @infer_attributes_from_custom_method = nil diff --git a/lib/schemable/response_schema_generator.rb b/lib/schemable/response_schema_generator.rb index c59464d..5f6d38b 100644 --- a/lib/schemable/response_schema_generator.rb +++ b/lib/schemable/response_schema_generator.rb @@ -1,11 +1,12 @@ module Schemable class ResponseSchemaGenerator - attr_reader :model_definition, :model, :schema_modifier + attr_reader :model_definition, :model, :schema_modifier, :configuration def initialize(model_definition) @model_definition = model_definition @model = model_definition.model @schema_modifier = SchemaModifier.new + @configuration = Schemable.configuration end def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false) @@ -38,32 +39,38 @@ module Schemable end def meta - { - type: :object, - properties: { - page: { - type: :object, - properties: { - totalPages: { - type: :integer, - default: 1 - }, - count: { - type: :integer, - default: 1 - }, - rowsPerPage: { - type: :integer, - default: 1 - }, - currentPage: { - type: :integer, - default: 1 + return @configuration.custom_meta_response_schema if @configuration.custom_meta_response_schema.present? + + if @configuration.pagination_enabled + { + type: :object, + properties: { + page: { + type: :object, + properties: { + totalPages: { + type: :integer, + default: 1 + }, + count: { + type: :integer, + default: 1 + }, + rowsPerPage: { + type: :integer, + default: 1 + }, + currentPage: { + type: :integer, + default: 1 + } } } } } - } + else + {} + end end def jsonapi diff --git a/sig/schemable/configuration.rbs b/sig/schemable/configuration.rbs index c5388a5..c645625 100644 --- a/sig/schemable/configuration.rbs +++ b/sig/schemable/configuration.rbs @@ -3,14 +3,16 @@ module Schemable attr_accessor orm: Symbol attr_accessor float_as_string: bool attr_accessor decimal_as_string: bool + attr_accessor pagination_enabled: bool attr_accessor disable_factory_bot: bool attr_accessor use_serialized_instance: bool + attr_accessor custom_defined_enum_method: Symbol? attr_accessor enum_prefix_for_simple_enum: String? attr_accessor enum_suffix_for_simple_enum: String? - attr_accessor custom_defined_enum_method: Symbol? attr_accessor custom_type_mappers: Hash[Symbol, any] - attr_accessor infer_attributes_from_jsonapi_serializable: bool attr_accessor infer_attributes_from_custom_method: Symbol? + attr_accessor custom_meta_response_schema: Hash[Symbol, any]? + attr_accessor infer_attributes_from_jsonapi_serializable: bool def initialize: -> void diff --git a/sig/schemable/response_schema_generator.rbs b/sig/schemable/response_schema_generator.rbs index 22d45a2..84c0faa 100644 --- a/sig/schemable/response_schema_generator.rbs +++ b/sig/schemable/response_schema_generator.rbs @@ -3,6 +3,7 @@ module Schemable attr_reader model: Class attr_reader model_definition: Definition attr_reader schema_modifier: SchemaModifier + attr_reader configuration: Configuration def initialize: (Definition) -> void def meta: -> Hash[Symbol, any]