Class: Schemable::ResponseSchemaGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/schemable/response_schema_generator.rb

Overview

The ResponseSchemaGenerator class is responsible for generating JSON schemas for responses. This class generates schemas based on the model definition, including attributes, relationships, and included resources.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_definition) ⇒ ResponseSchemaGenerator

Initializes a new ResponseSchemaGenerator instance.

Examples:

generator = ResponseSchemaGenerator.new(model_definition)

Parameters:

  • model_definition (ModelDefinition)

    The model definition to generate the schema for.



15
16
17
18
19
20
# File 'lib/schemable/response_schema_generator.rb', line 15

def initialize(model_definition)
  @model_definition = model_definition
  @model = model_definition.model
  @schema_modifier = SchemaModifier.new
  @configuration = Schemable.configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/schemable/response_schema_generator.rb', line 7

def configuration
  @configuration
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/schemable/response_schema_generator.rb', line 7

def model
  @model
end

#model_definitionObject (readonly)

Returns the value of attribute model_definition.



7
8
9
# File 'lib/schemable/response_schema_generator.rb', line 7

def model_definition
  @model_definition
end

#schema_modifierObject (readonly)

Returns the value of attribute schema_modifier.



7
8
9
# File 'lib/schemable/response_schema_generator.rb', line 7

def schema_modifier
  @schema_modifier
end

Instance Method Details

#generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false, expand_nested: false) ⇒ Hash

Generates the JSON schema for a response. It generates a schema for the model attributes and relationships, and if the ‘expand’ option is true, it also includes the included resources in the schema. It also adds meta and jsonapi information to the schema.

Examples:

schema = generator.generate(expand: true, relationships_to_exclude_from_expansion: ['some_relationship'], collection: true, expand_nested: true)

Parameters:

  • expand (Boolean) (defaults to: false)

    Whether to include the included resources in the schema.

  • relationships_to_exclude_from_expansion (Array) (defaults to: [])

    The relationships to exclude from expansion in the schema.

  • collection (Boolean) (defaults to: false)

    Whether the response is for a collection of resources.

  • expand_nested (Boolean) (defaults to: false)

    Whether to include the nested relationships in the schema.

Returns:

  • (Hash)

    The generated schema.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/schemable/response_schema_generator.rb', line 36

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 },
      attributes: AttributeSchemaGenerator.new(@model_definition).generate
    }.merge(
      if @model_definition.relationships.blank? || @model_definition.relationships == { belongs_to: {}, has_many: {} }
        {}
      else
        { relationships: RelationshipSchemaGenerator.new(@model_definition).generate(expand:, relationships_to_exclude_from_expansion:) }
      end
    )
  }

  schema = collection ? { data: { type: :array, items: data } } : { data: }

  if expand
    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: }, '.')

  { type: :object, properties: schema }.compact_blank
end

#jsonapiHash

Generates the JSON schema for the ‘jsonapi’ part of a response.

Examples:

jsonapi_schema = generator.jsonapi

Returns:

  • (Hash)

    The generated schema for the ‘jsonapi’ part of a response.



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/schemable/response_schema_generator.rb', line 116

def jsonapi
  {
    type: :object,
    properties: {
      version: {
        type: :string,
        default: '1.0'
      }
    }
  }
end

#metaHash

Generates the JSON schema for the ‘meta’ part of a response. It returns a custom meta response schema if one is defined in the configuration, otherwise it generates a default meta schema.

Examples:

meta_schema = generator.meta

Returns:

  • (Hash)

    The generated schema for the ‘meta’ part of a response.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/schemable/response_schema_generator.rb', line 75

def meta
  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