diff --git a/lib/schemable/configuration.rb b/lib/schemable/configuration.rb index 85d3a00..3c2dbbe 100644 --- a/lib/schemable/configuration.rb +++ b/lib/schemable/configuration.rb @@ -8,7 +8,9 @@ module Schemable :custom_type_mappers, :disable_factory_bot, :use_serialized_instance, - :custom_defined_enum_method + :custom_defined_enum_method, + :infer_attributes_from_custom_method, + :infer_attributes_from_jsonapi_serializable ) def initialize @@ -18,8 +20,10 @@ module Schemable @custom_type_mappers = {} @decimal_as_string = false @disable_factory_bot = true - @custom_defined_enum_method = nil @use_serialized_instance = false + @custom_defined_enum_method = nil + @infer_attributes_from_custom_method = nil + @infer_attributes_from_jsonapi_serializable = false end def type_mapper(type_name) diff --git a/lib/schemable/definition.rb b/lib/schemable/definition.rb index 065f00e..122c36c 100644 --- a/lib/schemable/definition.rb +++ b/lib/schemable/definition.rb @@ -18,7 +18,9 @@ module Schemable # V1::UserSerializer # def serializer - raise NotImplementedError, 'serializer method must be implemented in the definition class' + raise NotImplementedError, 'You must implement the serializer method in the definition class in order to use the infer_serializer_from_jsonapi_serializable configuration option.' if configuration.infer_attributes_from_jsonapi_serializable + + nil end # Returns the attributes defined in the serializer (Auto generated from the serializer). @@ -28,7 +30,11 @@ module Schemable # @example # [:id, :name, :email, :created_at, :updated_at] def attributes - serializer.attribute_blocks.transform_keys { |key| key.to_s.underscore.to_sym }.keys || nil + return (serializer&.attribute_blocks&.transform_keys { |key| key.to_s.underscore.to_sym }&.keys || nil) if configuration.infer_attributes_from_jsonapi_serializable + + return model.send(configuration.infer_attributes_from_custom_method).map(&:to_sym) if configuration.infer_attributes_from_custom_method + + model.attribute_names end # Returns the relationships defined in the serializer. @@ -271,7 +277,7 @@ module Schemable # @example # User def model - self.class.name.gsub('Swagger::Definitions::', '').gsub(':Class', '').constantize + self.class.name.gsub('Swagger::Definitions::', '').constantize end def serialized_instance diff --git a/sig/schemable/configuration.rbs b/sig/schemable/configuration.rbs index bef6c84..9e776ff 100644 --- a/sig/schemable/configuration.rbs +++ b/sig/schemable/configuration.rbs @@ -1,5 +1,6 @@ module Schemable class Configuration + attr_accessor orm: Symbol attr_accessor timestamps: bool attr_accessor float_as_string: bool @@ -8,6 +9,8 @@ module Schemable attr_accessor use_serialized_instance: bool 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? def initialize: -> void