Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seperates Create & Update Request Schema and Updates Documentations #1

Open
wants to merge 87 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
4321390
Updates .gitignore
muhammadnawzad Jul 28, 2023
c12bdc8
Updates attributes in excluded_request_attributes generated method
muhammadnawzad Jul 28, 2023
33d42c8
Fixes bug where required attributes were not caught properly due case…
muhammadnawzad Jul 29, 2023
55c7ec7
Separates create and update request schema and prettifies documentations
muhammadnawzad Jul 29, 2023
ec3a318
Separates create and update excluded request attributes on file gener…
muhammadnawzad Jul 29, 2023
64f81a3
Updates version
muhammadnawzad Jul 29, 2023
9f6ecb9
Fixes rubocop offenses
muhammadnawzad Jul 29, 2023
cddc117
Uses configs to check whether to use string or number for decimal and…
muhammadnawzad Sep 26, 2023
6715712
Updates gem version
muhammadnawzad Sep 26, 2023
7396dde
Updates Rubocop
muhammadnawzad Nov 8, 2023
923f3eb
Adds SchemaModifier Class
muhammadnawzad Nov 9, 2023
01049c4
Adds Configuration class for global configurations
muhammadnawzad Nov 9, 2023
bfdf652
Adds Constants module to store constants
muhammadnawzad Nov 9, 2023
5a0efd1
Uses any for values of a Hash in .rbs files
muhammadnawzad Nov 9, 2023
bda6e08
Adds custom_defined_enum_method config to global configs
muhammadnawzad Nov 9, 2023
33c2ef4
Adds use_serialized_instance config
muhammadnawzad Nov 9, 2023
213fece
Adds AttributeSchemaGenerator class
muhammadnawzad Nov 9, 2023
15fae70
Updates method names and generate_attributes_schema's logic
muhammadnawzad Nov 9, 2023
00c4b7f
Invokes attributes from @model_definition
muhammadnawzad Nov 9, 2023
cab9577
Moves types map into configuration
muhammadnawzad Nov 9, 2023
b4e995a
Temporarily Updates Schemable Module
muhammadnawzad Nov 9, 2023
8e3cd6e
Adds incomplete Definitions class
muhammadnawzad Nov 9, 2023
dd7598b
Adds configurations to infer attributes from
muhammadnawzad Nov 9, 2023
01d459b
Updates Definition Class
muhammadnawzad Nov 9, 2023
8b00983
Deletes Blanks
muhammadnawzad Nov 9, 2023
b480c82
Sets configurations by default
muhammadnawzad Nov 10, 2023
0118d57
Sets configuration by default
muhammadnawzad Nov 10, 2023
0da8634
Adds Relationship Generator Class
muhammadnawzad Nov 10, 2023
b03fcc6
Uses model_name as an instance method
muhammadnawzad Nov 10, 2023
21d601e
No need to pass in configuration instance on definition descendant in…
muhammadnawzad Nov 10, 2023
e046194
Adds included schema generator
muhammadnawzad Nov 12, 2023
e2ba1a7
Updates RelationshipSchemaGenerator to correctly generate schemas
muhammadnawzad Nov 12, 2023
861a740
Renames method
muhammadnawzad Nov 12, 2023
15fb52e
Passes parameters to the function on invoke rather than on class init…
muhammadnawzad Nov 12, 2023
2d7dd77
Adds ResponseSchemaGenerator Class
muhammadnawzad Nov 12, 2023
215c101
Properly adds meta and jsonapi
muhammadnawzad Nov 12, 2023
159dfc5
Properly adds relationships
muhammadnawzad Nov 12, 2023
5d80d66
Includes IncludedSchemaGenerator in Schemable
muhammadnawzad Nov 12, 2023
ac70f1e
Adds RequestSchemaGenerator class
muhammadnawzad Nov 12, 2023
92d9f33
Changes Exclude Logic
muhammadnawzad Nov 12, 2023
e25dfff
Adds Camelize Keys Method to Definitions
muhammadnawzad Nov 12, 2023
06aafcf
Speeds up the definition's retrieval
muhammadnawzad Nov 13, 2023
2b57b1e
Adds mongoid specific configurations
muhammadnawzad Nov 13, 2023
5a630a7
Adds mongoid specific conditions
muhammadnawzad Nov 13, 2023
1d9e06a
Adds missing signature
muhammadnawzad Nov 13, 2023
a9fbe3e
Updates Schemable Module
muhammadnawzad Nov 13, 2023
dcfc80d
Adds generate method
muhammadnawzad Nov 13, 2023
b8928ee
Uses attribute reader instead of accessor
muhammadnawzad Nov 13, 2023
d7d260f
Updates Gem Version
muhammadnawzad Nov 14, 2023
a832924
Updates some of the old documentations
muhammadnawzad Nov 14, 2023
ac5a10e
Slightly Updates Readme
muhammadnawzad Nov 14, 2023
2e8cb4b
Add documentation for Schemable module
muhammadnawzad Nov 16, 2023
86e208d
Add documentation for SchemaModifier class
muhammadnawzad Nov 16, 2023
e75e18c
Add documentation for Configuration class
muhammadnawzad Nov 16, 2023
c4a2af5
Add documentation for AttributeSchemaGenerator class
muhammadnawzad Nov 16, 2023
f95b047
Fixes Indentation
muhammadnawzad Nov 16, 2023
26ec4d9
Adds documentations for definition class
muhammadnawzad Nov 17, 2023
dc89944
Fixes typos and ensures type safety
muhammadnawzad Nov 17, 2023
6908df3
Ensures type safety
muhammadnawzad Nov 17, 2023
6fa863a
Removes timestamps config
muhammadnawzad Nov 17, 2023
04f7ded
Adds documentations for IncludedSchemaGenerator
muhammadnawzad Nov 17, 2023
f4f2734
Adds documentations for RelationshipSchemaGenerator
muhammadnawzad Nov 17, 2023
d2f3c8f
Adds documentations for RequestSchemaGenerator class
muhammadnawzad Nov 17, 2023
e522958
Allows custom meta for response
muhammadnawzad Nov 17, 2023
561a909
Adds documentations for ResponseSchemaGenerator
muhammadnawzad Nov 17, 2023
86460ae
Removes factory bot option
muhammadnawzad Nov 17, 2023
9b7c7d4
Updates the gem's generators
muhammadnawzad Nov 17, 2023
9aba52b
Fixes rubocop offenses
muhammadnawzad Nov 17, 2023
7fb82b3
Updates gems
muhammadnawzad Nov 17, 2023
9abdfe2
Moves example to readme documentations
muhammadnawzad Nov 17, 2023
33a11e9
Updates the gem's documentation
muhammadnawzad Nov 17, 2023
5b32cb2
Adds `default_value_for_enum_attributes` method to Schemable::Definition
muhammadnawzad Jan 29, 2024
d00737f
Update gem versions
muhammadnawzad Jan 29, 2024
e1a9a0d
Refactor enum attribute handling in attribute_schema_generator to set…
muhammadnawzad Jan 29, 2024
e95a143
Adds documentations for `default_value_for_enum_attributes` method
muhammadnawzad Jan 29, 2024
72bf660
Update Schemable version to 1.0.1
muhammadnawzad Jan 29, 2024
a2fb1cf
Updates changelog for `v1.0.1`
muhammadnawzad Jan 29, 2024
213a265
Add nullable_relationships method to Schemable::Definition
muhammadnawzad Jan 30, 2024
dbfb9c1
Refactor generate_schema method to handle nullable relationships
muhammadnawzad Jan 30, 2024
51d1113
Adds docs for nullable_relationships method
muhammadnawzad Jan 30, 2024
e894e46
Updates Changelog
muhammadnawzad Jan 30, 2024
1afb31b
Update Schemable gem version to 1.0.2
muhammadnawzad Jan 30, 2024
49992ff
Add expand_nested parameter to generate method
muhammadnawzad Jan 30, 2024
f90a997
Adds infer_expand_nested_from_expand global configuration option
muhammadnawzad Jan 30, 2024
d212a29
Add infer_expand_nested_from_expand configuration docs
muhammadnawzad Jan 30, 2024
96911aa
Updates change log for `v1.0.3`
muhammadnawzad Jan 30, 2024
aaf209b
Bump version to 1.0.3
muhammadnawzad Jan 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds AttributeSchemaGenerator class
  • Loading branch information
muhammadnawzad committed Nov 9, 2023

Verified

This commit was signed with the committer’s verified signature.
muhammadnawzad Muhammad Nawzad
commit 213fecefd2b5ebbd0b5b18d2da2fe1840c70b1ad
92 changes: 92 additions & 0 deletions lib/schemable/attribute_schema_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module Schemable
class AttributeSchemaGenerator
attr_accessor :model_definition, :configuration, :model, :schema_modifier, :response

def initialize(model_definition, configuration)
@model_definition = model_definition
@model = model_definition.model
@configuration = configuration
@schema_modifier = SchemaModifier.new
@response = nil
end

# Generate the JSON schema for attributes
def generate_attribute_schema
schema = {
type: :object,
properties: {}
}

@model.attribute_names.each do |attribute|
schema[:properties][attribute.to_sym] = generate_property_schema(attribute)
end

schema
end

# Generate the JSON schema for a specific attribute
def generate_property_schema(attribute)
if @configuration.orm == :mongoid
# Get the column hash for the attribute
attribute_hash = @model.fields[attribute.to_s]

# Check if this attribute has a custom JSON Schema definition
return @model_definition.array_types[attribute] if @model_definition.array_types.keys.include?(attribute)
return @model_definition.additional_response_attributes[attribute] if @model_definition.additional_response_attributes.keys.include?(attribute)

# Check if this is an array attribute
return @configuration.type_mapper(:array) if attribute_hash.try(:[], 'options').try(:[], 'type') == 'Array'

# Map the column type to a JSON Schema type if none of the above conditions are met
@response = @configuration.type_mapper(attribute_hash.try(:type).to_s.downcase.to_sym)

elsif @configuration.orm == :active_record
# Get the column hash for the attribute
attribute_hash = @model.columns_hash[attribute.to_s]

# Check if this attribute has a custom JSON Schema definition
return @model_definition.array_types[attribute] if @model_definition.array_types.keys.include?(attribute)
return @model_definition.additional_response_attributes[attribute] if @model_definition.additional_response_attributes.keys.include?(attribute)

# Check if this is an array attribute
return @configuration.type_mapper(:array) if attribute_hash.as_json.try(:[], 'sql_type_metadata').try(:[], 'sql_type').include?('[]')

# Map the column type to a JSON Schema type if none of the above conditions are met
@response = @configuration.type_mapper(attribute_hash.try(:type))

else
raise 'ORM not supported'
end

# If the attribute is nullable, modify the schema accordingly
return @schema_modifier.add_properties(@response, { nullable: true }, '.') if @response && @model_definition.nullable_attributes.include?(attribute)

# If attribute is an enum, modify the schema accordingly
if @configuration.custom_defined_enum_method
return @schema_modifier.add_properties(@response, { enum: @model.send(@configuration.custom_defined_enum_method, attribute.to_s) }, '.') if @response && @model.respond_to?(@configuration.custom_defined_enum_method)
elsif @model.respond_to?(:defined_enums)
return @schema_modifier.add_properties(@response, { enum: @model.defined_enums[attribute.to_s].keys }, '.') if @response && @model.defined_enums.key?(attribute.to_s)
end

return @response unless @response.nil?

# If we haven't found a schema type yet, try to infer it from the type of the attribute's value in the instance data
if @configuration.use_serialized_instance
serialized_instance = @model_definition.serialized_instance

type_from_instance = serialized_instance.as_json['data']['attributes'][attribute.to_s.camelize(:lower)]&.class&.name&.downcase

@response = @configuration.type_mapper(type_from_instance) if type_from_instance.present?

return @response unless @response.nil?
end

# If we still haven't found a schema type, default to object
@configuration.type_mapper(:object)
rescue NoMethodError
# Log a warning if the attribute does not exist on the @model
Rails.logger.warn("\e[33mWARNING: #{@model} does not have an attribute named \e[31m#{attribute}\e[0m")
{}
end
end
end
14 changes: 14 additions & 0 deletions sig/schemable/attribute_schema_generator.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Schemable
class AttributeSchemaGenerator
attr_accessor model: Class
attr_accessor model_definition: Definition
attr_accessor configuration: Configuration
attr_accessor response: Hash[Symbol, any]?
attr_accessor schema_modifier: SchemaModifier

def initialize: (Definition, Configuration) -> void
def generate_attribute_schema: -> Hash[Symbol, any]

def generate_property_schema: (Symbol) -> Hash[Symbol, any]
end
end