schemable 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97fad83e9bdcc4e077ed7890e3c35bacb010a70869349f481d52ebe241b16bf3
4
- data.tar.gz: 87ac04d61402266a554ca6ce2422a0ba98784ac4477495dcf38a6b5fc76ed8ad
3
+ metadata.gz: 7d63ce1b1c91d8f6344b5800d9e793bb33a3b9694f91f646546a0e59f470a675
4
+ data.tar.gz: 04c750e85b0095266a86b5e74ccf64212af0c7669476b23229b3d0874af4a287
5
5
  SHA512:
6
- metadata.gz: 1260ee980f234852ee7b941c36574dfb3c06f7327d758ac08e688277c06f441e96de6a9ddda4c1f44ba7d596aee07443cbf97e5053db28c072999daf2b90e29c
7
- data.tar.gz: 78dc9c6902ecfce157bd97aefbf042b82d06837c809a3a61961122dbcc1444f7b9e69573254ea83b676645bab16940a031aee3ff56e6564c1b92316cf1f1f72b
6
+ metadata.gz: 182e79396d00e3eb16d2344467cde65f9a8a0841b152f38c9fccafe4f3dfbaf47febdeb1a00063206090eb1fed37942f00788ee7530a936bda983a9c67944d34
7
+ data.tar.gz: 188035d9d158f7f28b2b79b98c1b9dea08df8482344c287eebac12116fddd5758637871917b8c79194537ad8af80ea72676f17f0e8cb5df2d811f5605240909e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
- ## [Unreleased]
1
+ # Changelog
2
+ This file is used to list changes made in each version of the Schemable gem.
2
3
 
3
- ## [0.1.0] - 2023-05-10
4
+ ## Schemable 1.0.2 (2024-01-30)
4
5
 
5
- - Initial release
6
+ * Added configuration for making certain associations nullable in the response's relationship. This can be done by adding the name of the relation in the `nullable_relationships` method's array of strings.
7
+
8
+ ## Schemable 1.0.1 (2024-01-29)
9
+
10
+ * Added configuration for changing the default value of enums. By default first key is used, or alternatively default can be set manually by the method `default_value_for_enum_attributes` from the definition.
11
+
12
+ ## Schemable 1.0.0 (2023-11-17)
13
+
14
+ * Initial release
data/Gemfile CHANGED
@@ -6,10 +6,10 @@ gemspec
6
6
 
7
7
  gem 'rake', '~> 13.1.0'
8
8
  gem 'rspec', '~> 3.12.0'
9
- gem 'rubocop', '~> 1.57.2'
10
- gem 'rubocop-rails', '~> 2.22.1'
9
+ gem 'rubocop', '~> 1.60.2'
10
+ gem 'rubocop-rails', '~> 2.23.1'
11
11
 
12
12
  group :development, :test do
13
- gem 'factory_bot_rails', '~> 6.2.0'
13
+ gem 'factory_bot_rails', '~> 6.4.3'
14
14
  gem 'jsonapi-rails', '~> 0.4.1'
15
15
  end
data/Gemfile.lock CHANGED
@@ -1,168 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- schemable (1.0.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- actionpack (7.1.2)
10
- actionview (= 7.1.2)
11
- activesupport (= 7.1.2)
12
- nokogiri (>= 1.8.5)
13
- racc
14
- rack (>= 2.2.4)
15
- rack-session (>= 1.0.1)
16
- rack-test (>= 0.6.3)
17
- rails-dom-testing (~> 2.2)
18
- rails-html-sanitizer (~> 1.6)
19
- actionview (7.1.2)
20
- activesupport (= 7.1.2)
21
- builder (~> 3.1)
22
- erubi (~> 1.11)
23
- rails-dom-testing (~> 2.2)
24
- rails-html-sanitizer (~> 1.6)
25
- activesupport (7.1.2)
26
- base64
27
- bigdecimal
28
- concurrent-ruby (~> 1.0, >= 1.0.2)
29
- connection_pool (>= 2.2.5)
30
- drb
31
- i18n (>= 1.6, < 2)
32
- minitest (>= 5.1)
33
- mutex_m
34
- tzinfo (~> 2.0)
35
- ast (2.4.2)
36
- base64 (0.2.0)
37
- bigdecimal (3.1.4)
38
- builder (3.2.4)
39
- concurrent-ruby (1.2.2)
40
- connection_pool (2.4.1)
41
- crass (1.0.6)
42
- diff-lcs (1.5.0)
43
- drb (2.2.0)
44
- ruby2_keywords
45
- erubi (1.12.0)
46
- factory_bot (6.2.1)
47
- activesupport (>= 5.0.0)
48
- factory_bot_rails (6.2.0)
49
- factory_bot (~> 6.2.0)
50
- railties (>= 5.0.0)
51
- i18n (1.14.1)
52
- concurrent-ruby (~> 1.0)
53
- io-console (0.6.0)
54
- irb (1.9.0)
55
- rdoc
56
- reline (>= 0.3.8)
57
- json (2.6.3)
58
- jsonapi-deserializable (0.2.0)
59
- jsonapi-parser (0.1.1)
60
- jsonapi-rails (0.4.1)
61
- jsonapi-parser (~> 0.1.0)
62
- jsonapi-rb (~> 0.5.0)
63
- jsonapi-rb (0.5.0)
64
- jsonapi-deserializable (~> 0.2.0)
65
- jsonapi-serializable (~> 0.3.0)
66
- jsonapi-renderer (0.2.2)
67
- jsonapi-serializable (0.3.1)
68
- jsonapi-renderer (~> 0.2.0)
69
- language_server-protocol (3.17.0.3)
70
- loofah (2.22.0)
71
- crass (~> 1.0.2)
72
- nokogiri (>= 1.12.0)
73
- minitest (5.20.0)
74
- mutex_m (0.2.0)
75
- nokogiri (1.15.4-x86_64-linux)
76
- racc (~> 1.4)
77
- parallel (1.23.0)
78
- parser (3.2.2.4)
79
- ast (~> 2.4.1)
80
- racc
81
- psych (5.1.1.1)
82
- stringio
83
- racc (1.7.3)
84
- rack (3.0.8)
85
- rack-session (2.0.0)
86
- rack (>= 3.0.0)
87
- rack-test (2.1.0)
88
- rack (>= 1.3)
89
- rackup (2.1.0)
90
- rack (>= 3)
91
- webrick (~> 1.8)
92
- rails-dom-testing (2.2.0)
93
- activesupport (>= 5.0.0)
94
- minitest
95
- nokogiri (>= 1.6)
96
- rails-html-sanitizer (1.6.0)
97
- loofah (~> 2.21)
98
- nokogiri (~> 1.14)
99
- railties (7.1.2)
100
- actionpack (= 7.1.2)
101
- activesupport (= 7.1.2)
102
- irb
103
- rackup (>= 1.0.0)
104
- rake (>= 12.2)
105
- thor (~> 1.0, >= 1.2.2)
106
- zeitwerk (~> 2.6)
107
- rainbow (3.1.1)
108
- rake (13.1.0)
109
- rdoc (6.6.0)
110
- psych (>= 4.0.0)
111
- regexp_parser (2.8.2)
112
- reline (0.4.0)
113
- io-console (~> 0.5)
114
- rexml (3.2.6)
115
- rspec (3.12.0)
116
- rspec-core (~> 3.12.0)
117
- rspec-expectations (~> 3.12.0)
118
- rspec-mocks (~> 3.12.0)
119
- rspec-core (3.12.2)
120
- rspec-support (~> 3.12.0)
121
- rspec-expectations (3.12.3)
122
- diff-lcs (>= 1.2.0, < 2.0)
123
- rspec-support (~> 3.12.0)
124
- rspec-mocks (3.12.6)
125
- diff-lcs (>= 1.2.0, < 2.0)
126
- rspec-support (~> 3.12.0)
127
- rspec-support (3.12.1)
128
- rubocop (1.57.2)
129
- json (~> 2.3)
130
- language_server-protocol (>= 3.17.0)
131
- parallel (~> 1.10)
132
- parser (>= 3.2.2.4)
133
- rainbow (>= 2.2.2, < 4.0)
134
- regexp_parser (>= 1.8, < 3.0)
135
- rexml (>= 3.2.5, < 4.0)
136
- rubocop-ast (>= 1.28.1, < 2.0)
137
- ruby-progressbar (~> 1.7)
138
- unicode-display_width (>= 2.4.0, < 3.0)
139
- rubocop-ast (1.30.0)
140
- parser (>= 3.2.1.0)
141
- rubocop-rails (2.22.1)
142
- activesupport (>= 4.2.0)
143
- rack (>= 1.1)
144
- rubocop (>= 1.33.0, < 2.0)
145
- ruby-progressbar (1.13.0)
146
- ruby2_keywords (0.0.5)
147
- stringio (3.0.9)
148
- thor (1.3.0)
149
- tzinfo (2.0.6)
150
- concurrent-ruby (~> 1.0)
151
- unicode-display_width (2.5.0)
152
- webrick (1.8.1)
153
- zeitwerk (2.6.12)
154
-
155
- PLATFORMS
156
- x86_64-linux
157
-
158
- DEPENDENCIES
159
- factory_bot_rails (~> 6.2.0)
160
- jsonapi-rails (~> 0.4.1)
161
- rake (~> 13.1.0)
162
- rspec (~> 3.12.0)
163
- rubocop (~> 1.57.2)
164
- rubocop-rails (~> 2.22.1)
165
- schemable!
166
-
167
- BUNDLED WITH
168
- 2.4.17
data/README.md CHANGED
@@ -14,11 +14,11 @@ gem 'schemable'
14
14
 
15
15
  And then execute:
16
16
 
17
- $ bundle install
17
+ bundle install
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install schemable
21
+ gem install schemable
22
22
 
23
23
  ## Usage
24
24
 
@@ -32,7 +32,6 @@ rails g schemable:install
32
32
 
33
33
  This will generate `schemable.rb` in your `config/initializers` directory. This file will contain the configuration for the Schemable gem. You can modify the configuration to your liking. For more information on the configuration options, see the [Configuration](#configuration) section below.
34
34
 
35
-
36
35
  ### Generating Definition Files
37
36
 
38
37
  The Schemable gem provides a generator that can be used to generate definition files for your models. To generate a definition file for a model, run the following command:
@@ -43,74 +42,76 @@ rails g schemable:model --model_name <model_name>
43
42
 
44
43
  This will generate a definition file for the specified model in the `lib/swagger/definitions` directory. The definition file will be named `<model_name>.rb`. This file will have the bare minimum code required to generate a schema for the model. You can then modify the definition file to your liking by overriding the default methods. For example, you can add or remove attributes from the schema, or you can add or remove relationships from the schema. You can also add custom attributes to the schema. For more information on how to customize the schema, see the [Customizing the Schema](#customizing-the-schema) section below.
45
44
 
46
-
47
45
  ### Configuration
46
+
48
47
  The Schemable gem provides a number of configuration options that can be used to customize the behavior of the gem. The following is a list of the configuration options that are available.
49
48
 
50
49
  Please note that the configurations options below are defined in the `Schemable` module of the gem. To configure the gem, simply override the default values in the `config/initializers/schemable.rb` file. Also the changes will affect all the definition classes globally.
51
50
 
52
51
  ---
53
- | Option Name | Description | Default Value |
54
- | -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
55
- | `orm` | The ORM that is used in the application. The options are `:active_record` and `:mongoid`. | `true` |
56
- | `float_as_string` | Whether or not to convert the `float` type to a `string` type in the schema. | `false` |
57
- | `decimal_as_string` | Whether or not to convert the `decimal` type to a `string` type in the schema. | `false` |
58
- | `custom_type_mappers` | A hash of custom type mappers that can be used to override the default type mappers. A specific method should be used, see [Annex 1.0 - Add custom type mapper](#annex-10---add-custom-type-mapper) for more information. | `{}` |
59
- | `use_serialized_instance` | Whether or not to use the serialized instance in the process of schema generation as type fallback for virtual attributes. See [Annex 1.1 - Use serialized instance](#annex-11---use-serialized-instance) for more information. | `false` |
60
- | `custom_defined_enum_method` | The name of the method that is used to get the enum keys and values. This allows applications with the orm `mongoid` define a method that mimicks what `defined_enums` does in `activerecord`. Please see [Annex 1.2 - Custom defined enum method](#annex-12---custom-defined-enum-method) for an example. | `nil` |
61
- | `enum_prefix_for_simple_enum` | The prefix to be used for the enum values when `mongoid` is used. | `nil` |
62
- | `enum_suffix_for_simple_enum` | The suffix to be used for the enum values when `mongoid` is used. | `nil` |
63
- | `infer_attributes_from_custom_method` | The name of the custom method that is used to get the attributes to be generated in the schema. See [Annex 1.3 - Infer attributes from custom method](#annex-13---infer-attributes-from-custom-method) for more information. | `nil` |
64
- | `infer_attributes_from_jsonapi_serializable` | Whether or not to infer the attributes from the `JSONAPI::Serializable::Resource` class. See the previous example [Annex 1.1 - Use serialized instance](#annex-11---use-serialized-instance) for more information. | `false` |
65
- | `custom_meta_response_schema` | A hash of custom meta response schema that can be used to override the default meta response schema. See [Annex 1.4 - Custom meta response schema](#annex-14---custom-meta-response-schema) for more information. | `nil` |
66
- | `pagination_enabled` | Enable pagination schema generation in the `meta` section of the response schema. | `true` |
67
52
 
53
+ | Option Name | Description | Default Value |
54
+ | -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
55
+ | `orm` | The ORM that is used in the application. The options are `:active_record` and `:mongoid`. | `true` |
56
+ | `float_as_string` | Whether or not to convert the `float` type to a `string` type in the schema. | `false` |
57
+ | `decimal_as_string` | Whether or not to convert the `decimal` type to a `string` type in the schema. | `false` |
58
+ | `custom_type_mappers` | A hash of custom type mappers that can be used to override the default type mappers. A specific method should be used, see [Annex 1.0 - Add custom type mapper](#annex-10---add-custom-type-mapper) for more information. | `{}` |
59
+ | `use_serialized_instance` | Whether or not to use the serialized instance in the process of schema generation as type fallback for virtual attributes. See [Annex 1.1 - Use serialized instance](#annex-11---use-serialized-instance) for more information. | `false` |
60
+ | `custom_defined_enum_method` | The name of the method that is used to get the enum keys and values. This allows applications with the orm `mongoid` define a method that mimicks what `defined_enums` does in `activerecord`. Please see [Annex 1.2 - Custom defined enum method](#annex-12---custom-defined-enum-method) for an example. | `nil` |
61
+ | `enum_prefix_for_simple_enum` | The prefix to be used for the enum values when `mongoid` is used. | `nil` |
62
+ | `enum_suffix_for_simple_enum` | The suffix to be used for the enum values when `mongoid` is used. | `nil` |
63
+ | `infer_attributes_from_custom_method` | The name of the custom method that is used to get the attributes to be generated in the schema. See [Annex 1.3 - Infer attributes from custom method](#annex-13---infer-attributes-from-custom-method) for more information. | `nil` |
64
+ | `infer_attributes_from_jsonapi_serializable` | Whether or not to infer the attributes from the `JSONAPI::Serializable::Resource` class. See the previous example [Annex 1.1 - Use serialized instance](#annex-11---use-serialized-instance) for more information. | `false` |
65
+ | `custom_meta_response_schema` | A hash of custom meta response schema that can be used to override the default meta response schema. See [Annex 1.4 - Custom meta response schema](#annex-14---custom-meta-response-schema) for more information. | `nil` |
66
+ | `pagination_enabled` | Enable pagination schema generation in the `meta` section of the response schema. | `true` |
68
67
 
69
68
  ---
70
69
 
71
70
  ### Customizing the Schema
72
71
 
73
- The Schemable gem provides a number of methods that can be used to customize the schema. These methods are defined in the `Schemable::Definition` class of the gem. To customize the schema for a specific model, simply override the default methods in the `Schemable::Definition` class for the model.
72
+ The Schemable gem provides a number of methods that can be used to customize the schema. These methods are defined in the `Schemable::Definition` class of the gem. To customize the schema for a specific model, simply override the default methods in the `Schemable::Definition` class for the model.
74
73
 
75
74
  Please read the method inline documentation before overriding to avoid any unexpected behavior.
76
75
 
77
76
  The following is a list of the methods that can be overridden. (See the example in [Annex 1.5 - Highly Customized Definition](#annex-15---highly-customized-definition) for a highly customized definition file.)
78
77
 
79
- ----
80
-
81
- | Method Name | Description |
82
- |---------------------------------------|------------------------------------------------------------------------------------------------------------|
83
- | `serializer` | Returns the serializer of the model for the definition. |
84
- | `attributes` | Returns the attributes for the definition based on the configuration. |
85
- | `relationships` | Returns the relationships defined in the model. |
86
- | `array_types` | Returns a hash of all the arrays defined for the model. |
87
- | `optional_create_request_attributes` | Returns the attributes that are not required in the create request. |
88
- | `optional_update_request_attributes` | Returns the attributes that are not required in the update request. |
89
- | `nullable_attributes` | Returns the attributes that are nullable in the request/response body. |
90
- | `additional_create_request_attributes`| Returns the additional create request attributes that are not automatically generated. |
91
- | `additional_update_request_attributes`| Returns the additional update request attributes that are not automatically generated. |
92
- | `additional_response_attributes` | Returns the additional response attributes that are not automatically generated. |
93
- | `additional_response_relations` | Returns the additional response relations that are not automatically generated. |
94
- | `additional_response_included` | Returns the additional response included that are not automatically generated. |
95
- | `excluded_create_request_attributes` | Returns the attributes that are excluded from the create request schema. |
96
- | `excluded_update_request_attributes` | Returns the attributes that are excluded from the update request schema. |
97
- | `excluded_response_attributes` | Returns the attributes that are excluded from the response schema. |
98
- | `excluded_response_relations` | Returns the relationships that are excluded from the response schema. |
99
- | `excluded_response_included` | Returns the included that are excluded from the response schema. |
100
- | `serialized_instance` | Returns an instance of the model class that is already serialized into jsonapi format. |
101
- | `model` | Returns the model class (Constantized from the definition class name). |
102
- | `model_name` | Returns the model name. Used for schema type naming. |
103
- | `camelize_keys` | Given a hash, it returns a new hash with all the keys camelized. |
104
- | `generate` | Returns the schema for the create request body, update request body, and response body. |
105
- ----
78
+ ---
79
+
80
+ | Method Name | Description |
81
+ | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
82
+ | `serializer` | Returns the serializer of the model for the definition. |
83
+ | `attributes` | Returns the attributes for the definition based on the configuration. |
84
+ | `relationships` | Returns the relationships defined in the model. |
85
+ | `array_types` | Returns a hash of all the arrays defined for the model. |
86
+ | `optional_create_request_attributes` | Returns the attributes that are not required in the create request. |
87
+ | `optional_update_request_attributes` | Returns the attributes that are not required in the update request. |
88
+ | `nullable_attributes` | Returns the attributes that are nullable in the request/response body. |
89
+ | `nullable_relationships` | Returns the relationships that are nullable in the response body. |
90
+ | `additional_create_request_attributes` | Returns the additional create request attributes that are not automatically generated. |
91
+ | `additional_update_request_attributes` | Returns the additional update request attributes that are not automatically generated. |
92
+ | `additional_response_attributes` | Returns the additional response attributes that are not automatically generated. |
93
+ | `additional_response_relations` | Returns the additional response relations that are not automatically generated. |
94
+ | `additional_response_included` | Returns the additional response included that are not automatically generated. |
95
+ | `excluded_create_request_attributes` | Returns the attributes that are excluded from the create request schema. |
96
+ | `excluded_update_request_attributes` | Returns the attributes that are excluded from the update request schema. |
97
+ | `default_value_for_enum_attributes` | Returns the default value for the enum attributes. Used when you want a custom value for the default enum. By default the first key is used as default |
98
+ | `excluded_response_attributes` | Returns the attributes that are excluded from the response schema. |
99
+ | `excluded_response_relations` | Returns the relationships that are excluded from the response schema. |
100
+ | `excluded_response_included` | Returns the included that are excluded from the response schema. |
101
+ | `serialized_instance` | Returns an instance of the model class that is already serialized into jsonapi format. |
102
+ | `model` | Returns the model class (Constantized from the definition class name). |
103
+ | `model_name` | Returns the model name. Used for schema type naming. |
104
+ | `camelize_keys` | Given a hash, it returns a new hash with all the keys camelized. |
105
+ | `generate` | Returns the schema for the create request body, update request body, and response body. |
106
+
107
+ ---
106
108
 
107
109
  ## Examples
108
110
 
109
111
  The followings are some examples of configuration of the gem to have different behaviors based on the application needs. In the above section, we have already seen how to generate the definition files for the models. The following examples will show how to customize the schema for the models. Also, we will see how to use the generated schema in RSwag to generate the swagger documentation for the API.
110
112
 
111
-
112
113
  ### Annex 1.0 - Add custom type mapper
113
-
114
+
114
115
  ```ruby
115
116
  # config/initializers/schemable.rb
116
117
 
@@ -197,6 +198,7 @@ def self.custom_defined_enum(suffix: '_cd', prefix: nil)
197
198
  defined_enums
198
199
  end
199
200
  ```
201
+
200
202
  This method will work for all the models that have enum fields. Since Simple Enum gem defines enum fields with the suffix `_cd`, we can use the `suffix` option to get the enum fields. However, if the enum fields are defined with a different suffix, we can use the `prefix` option to get the enum fields.
201
203
 
202
204
  Now, we need to specify theses options in the configuration file:
@@ -405,12 +407,13 @@ module Swagger
405
407
  end
406
408
  end
407
409
  ```
410
+
408
411
  </Details>
409
412
  </Summary>
410
413
 
411
414
  ## Contributing
412
415
 
413
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/schemable. This project is intended to be a safe, welcoming space for collaboration, and contributors. Please go to issues page to report any bugs or feature requests. If you would like to contribute, please fork the repository and submit a pull request.
416
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/[USERNAME]/schemable>. This project is intended to be a safe, welcoming space for collaboration, and contributors. Please go to issues page to report any bugs or feature requests. If you would like to contribute, please fork the repository and submit a pull request.
414
417
 
415
418
  To, use the gem locally, clone the repository and run `bundle install` to install dependencies. Then, run `bundle exec rspec` to run the tests.
416
419
 
@@ -101,9 +101,25 @@ module Schemable
101
101
  if @configuration.custom_defined_enum_method && @model.respond_to?(@configuration.custom_defined_enum_method)
102
102
  defined_enums = @model.send(@configuration.custom_defined_enum_method)
103
103
  enum_attribute = attribute.to_s.gsub(@configuration.enum_prefix_for_simple_enum || @configuration.enum_suffix_for_simple_enum, '').to_s
104
- return @schema_modifier.add_properties(@response, { enum: defined_enums[enum_attribute].keys }, '.') if @response && defined_enums[enum_attribute].present?
105
- elsif @model.respond_to?(:defined_enums)
106
- return @schema_modifier.add_properties(@response, { enum: @model.defined_enums[attribute.to_s].keys }, '.') if @response && @model.defined_enums.key?(attribute.to_s)
104
+ if @response && defined_enums[enum_attribute].present?
105
+ return @schema_modifier.add_properties(
106
+ @response,
107
+ {
108
+ enum: defined_enums[enum_attribute].keys,
109
+ default: @model_definition.default_value_for_enum_attributes[attribute.to_sym] || defined_enums[enum_attribute].keys.first
110
+ },
111
+ '.'
112
+ )
113
+ end
114
+ elsif @model.respond_to?(:defined_enums) && @response && @model.defined_enums.key?(attribute.to_s)
115
+ return @schema_modifier.add_properties(
116
+ @response,
117
+ {
118
+ enum: @model.defined_enums[attribute.to_s].keys,
119
+ default: @model_definition.default_value_for_enum_attributes[attribute.to_sym] || @model.defined_enums[attribute.to_s].keys.first
120
+ },
121
+ '.'
122
+ )
107
123
  end
108
124
 
109
125
  return @response unless @response.nil?
@@ -125,6 +125,18 @@ module Schemable
125
125
  %i[]
126
126
  end
127
127
 
128
+ # Returns the relationships that are nullable in the response body.
129
+ # This means that they can be present in the response body but they can be null.
130
+ # They are not required to be present in the request body.
131
+ #
132
+ # @example
133
+ # ['users', 'applicant']
134
+ #
135
+ # @return [Array<String>] The attributes that are nullable in the response body.
136
+ def nullable_relationships
137
+ %w[]
138
+ end
139
+
128
140
  # Returns the additional create request attributes that are not automatically generated.
129
141
  # These attributes are appended to the create request schema.
130
142
  #
@@ -265,6 +277,19 @@ module Schemable
265
277
  %i[]
266
278
  end
267
279
 
280
+ # Returns the default value for the enum attributes.
281
+ #
282
+ # @example
283
+ # {
284
+ # status: 'pending',
285
+ # flag: 0
286
+ # }
287
+ #
288
+ # @return [Hash] The custom default values for the enum attributes.
289
+ def default_value_for_enum_attributes
290
+ {}
291
+ end
292
+
268
293
  # Returns an instance of the model class that is already serialized into jsonapi format.
269
294
  #
270
295
  # @return [Hash] The serialized instance of the model class.
@@ -84,36 +84,43 @@ module Schemable
84
84
  #
85
85
  # @return [Hash] The generated schema for the relationship.
86
86
  def generate_schema(type_name, collection: false)
87
- if collection
88
- {
89
- type: :object,
90
- properties: {
91
- data: {
92
- type: :array,
93
- items: {
94
- type: :object,
95
- properties: {
96
- id: { type: :string },
97
- type: { type: :string, default: type_name }
98
- }
99
- }
100
- }
101
- }
102
- }
103
- else
104
- {
105
- type: :object,
106
- properties: {
107
- data: {
108
- type: :object,
109
- properties: {
110
- id: { type: :string },
111
- type: { type: :string, default: type_name }
112
- }
113
- }
114
- }
115
- }
116
- end
87
+ schema = if collection
88
+ {
89
+ type: :object,
90
+ properties: {
91
+ data: {
92
+ type: :array,
93
+ items: {
94
+ type: :object,
95
+ properties: {
96
+ id: { type: :string },
97
+ type: { type: :string, default: type_name }
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+ else
104
+ {
105
+ type: :object,
106
+ properties: {
107
+ data: {
108
+ type: :object,
109
+ properties: {
110
+ id: { type: :string },
111
+ type: { type: :string, default: type_name }
112
+ }
113
+ }
114
+ }
115
+ }
116
+ end
117
+
118
+ # Modify the schema to nullable if the relationship is in nullable
119
+ is_relation_nullable = @model_definition.nullable_relationships.include?(type_name)
120
+
121
+ return schema unless is_relation_nullable
122
+
123
+ @schema_modifier.add_properties(schema, { nullable: true }, 'properties.data')
117
124
  end
118
125
  end
119
126
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Schemable
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.2'
5
5
  end
@@ -13,6 +13,7 @@ module Schemable
13
13
  def array_types: -> Hash[Symbol, any]
14
14
  def relationships: -> Hash[Symbol, any]
15
15
  def nullable_attributes: -> Array[Symbol]
16
+ def nullable_relationships: -> Array[String]
16
17
  def serialized_instance: -> Hash[Symbol, any]
17
18
  def self.generate: -> Array[Hash[Symbol, any]]
18
19
  def excluded_response_included: -> Array[Symbol]
@@ -25,6 +26,7 @@ module Schemable
25
26
  def excluded_update_request_attributes: -> Array[Symbol]
26
27
  def optional_create_request_attributes: -> Array[Symbol]
27
28
  def optional_update_request_attributes: -> Array[Symbol]
29
+ def default_value_for_enum_attributes: -> Hash[Symbol, any]
28
30
  def additional_create_request_attributes: -> Hash[Symbol, any]
29
31
  def additional_update_request_attributes: -> Hash[Symbol, any]
30
32
  def camelize_keys: (Hash[Symbol, any]) -> (Array[Hash[Symbol, any]] | Hash[Symbol, any])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammad Nawzad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-17 00:00:00.000000000 Z
11
+ date: 2024-01-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The schemable gem is an opinionated Gem for Rails applications to auto
14
14
  generate schema for models in JSONAPI format. It is designed to work with rswag's
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.4.10
78
+ rubygems_version: 3.5.4
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: An opinionated Gem for Rails applications to auto generate schema in JSONAPI