schemable 1.0.1 → 1.0.3

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: 75af2b99b8def68455dd086ca7d2b95dfbd5bcec8ccd16cca5a0695ef992c0f4
4
- data.tar.gz: '058291928127918abf3d5a7f64f7d6c3c42a1e262c76c7d06b23060f534bda47'
3
+ metadata.gz: 76b5ccf66c28d0bcfc644321e370e1c4404518ee1a4ba056fe20bb72a22cc415
4
+ data.tar.gz: cd7ad60e43634159a2b845c15ef557a3a60b02cb5c465e63e5f90ff47807afe5
5
5
  SHA512:
6
- metadata.gz: f56ed0334197fd366ca8881d95aed351efe847486be33fac94a122b0b0c19d8fb3c958ae3edc94ec020d38bc3757396aea8d7c8010b8130907b10468b71b392f
7
- data.tar.gz: ea1b2cc748f66c9fdbc620a8914450debaddc2f468ce34bf2e11257d7aeb426d6bf52e72944ddfe9203ebfb9a305f3cf7de7510481c984ace3759e0b63b375cd
6
+ metadata.gz: 835b1048ab2efd7419b45e77d4733e769d9c657a5cd08e67a26584f3cf6f3ca23588d7875eb21353d9a283f422b50bb50bfc8a5ef0dd2edcad3dcf0c76cfda16
7
+ data.tar.gz: accc7a59d075dfc07437f7d2c6bfa8367336b3aafd8d04aca652c38a98cebdf3e7f80c614c1e90524091df6f32e7306ac4f2d183ab79157863f4014376a3b14a
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
  This file is used to list changes made in each version of the Schemable gem.
3
3
 
4
+ ## Schemable 1.0.3 (2024-01-30)
5
+
6
+ * Added configuration for preventing expansion for nested relationships. This can be done by setting the `expand_nested` to `true` when invoking `ResponseSchemaGenerator`'s `generate` instance method (e.g. `ResponseSchemaGenerator.new(instance).generate(expand: true, expand_nested: true)`. Additionally, you could globally set the value of `expand_nested` to the same value as `expand` by setting the configuration `infer_expand_nested_from_expand` to `true` in the `/config/initializers/schemable.rb`.
7
+
8
+ ## Schemable 1.0.2 (2024-01-30)
9
+
10
+ * 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.
11
+
4
12
  ## Schemable 1.0.1 (2024-01-29)
5
13
 
6
14
  * 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.
data/README.md CHANGED
@@ -61,6 +61,7 @@ Please note that the configurations options below are defined in the `Schemable`
61
61
  | `enum_prefix_for_simple_enum` | The prefix to be used for the enum values when `mongoid` is used. | `nil` |
62
62
  | `enum_suffix_for_simple_enum` | The suffix to be used for the enum values when `mongoid` is used. | `nil` |
63
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_expand_nested_from_expand` | Configures `ResponseSchemaGenerator`'s `generate` method to prevent expansion for nested relationships. It globally set the value of `expand_nested` to the same value as `expand` by setting the configuration to `true` | `flase` |
64
65
  | `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
66
  | `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
67
  | `pagination_enabled` | Enable pagination schema generation in the `meta` section of the response schema. | `true` |
@@ -86,6 +87,7 @@ The following is a list of the methods that can be overridden. (See the example
86
87
  | `optional_create_request_attributes` | Returns the attributes that are not required in the create request. |
87
88
  | `optional_update_request_attributes` | Returns the attributes that are not required in the update request. |
88
89
  | `nullable_attributes` | Returns the attributes that are nullable in the request/response body. |
90
+ | `nullable_relationships` | Returns the relationships that are nullable in the response body. |
89
91
  | `additional_create_request_attributes` | Returns the additional create request attributes that are not automatically generated. |
90
92
  | `additional_update_request_attributes` | Returns the additional update request attributes that are not automatically generated. |
91
93
  | `additional_response_attributes` | Returns the additional response attributes that are not automatically generated. |
@@ -399,7 +401,7 @@ module Swagger
399
401
  "#{schema_instance.model}CreateRequest": schema_instance.camelize_keys(Schemable::RequestSchemaGenerator.new(schema_instance).generate_for_create),
400
402
  "#{schema_instance.model}UpdateRequest": schema_instance.camelize_keys(Schemable::RequestSchemaGenerator.new(schema_instance).generate_for_update),
401
403
  "#{schema_instance.model}Response": schema_instance.camelize_keys(Schemable::ResponseSchemaGenerator.new(schema_instance).generate(expand: true, collection: true, relationships_to_exclude_from_expansion: %w[addresses stores attachments])),
402
- "#{schema_instance.model}ResponseExpanded": schema_instance.camelize_keys(Schemable::ResponseSchemaGenerator.new(schema_instance).generate(expand: true))
404
+ "#{schema_instance.model}ResponseExpanded": schema_instance.camelize_keys(Schemable::ResponseSchemaGenerator.new(schema_instance).generate(expand: true, expand_nested: true))
403
405
  ]
404
406
  end
405
407
  end
@@ -16,6 +16,7 @@ module Schemable
16
16
  :enum_prefix_for_simple_enum,
17
17
  :enum_suffix_for_simple_enum,
18
18
  :custom_meta_response_schema,
19
+ :infer_expand_nested_from_expand,
19
20
  :infer_attributes_from_custom_method,
20
21
  :infer_attributes_from_jsonapi_serializable
21
22
  )
@@ -32,6 +33,7 @@ module Schemable
32
33
  @custom_meta_response_schema = nil
33
34
  @enum_prefix_for_simple_enum = nil
34
35
  @enum_suffix_for_simple_enum = nil
36
+ @infer_expand_nested_from_expand = false
35
37
  @infer_attributes_from_custom_method = nil
36
38
  @infer_attributes_from_jsonapi_serializable = false
37
39
  end
@@ -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
  #
@@ -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
@@ -27,12 +27,16 @@ module Schemable
27
27
  # @param expand [Boolean] Whether to include the included resources in the schema.
28
28
  # @param relationships_to_exclude_from_expansion [Array] The relationships to exclude from expansion in the schema.
29
29
  # @param collection [Boolean] Whether the response is for a collection of resources.
30
+ # @param expand_nested [Boolean] Whether to include the nested relationships in the schema.
30
31
  #
31
32
  # @example
32
- # schema = generator.generate(expand: true, relationships_to_exclude_from_expansion: ['some_relationship'], collection: true)
33
+ # schema = generator.generate(expand: true, relationships_to_exclude_from_expansion: ['some_relationship'], collection: true, expand_nested: true)
33
34
  #
34
35
  # @return [Hash] The generated schema.
35
- def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false)
36
+ def generate(expand: false, relationships_to_exclude_from_expansion: [], collection: false, expand_nested: false)
37
+ # Override expand_nested if infer_expand_nested_from_expand is true
38
+ expand_nested = expand if @configuration.infer_expand_nested_from_expand
39
+
36
40
  data = {
37
41
  type: :object,
38
42
  properties: {
@@ -51,7 +55,7 @@ module Schemable
51
55
  schema = collection ? { data: { type: :array, items: data } } : { data: }
52
56
 
53
57
  if expand
54
- included_schema = IncludedSchemaGenerator.new(@model_definition).generate(expand:, relationships_to_exclude_from_expansion:)
58
+ included_schema = IncludedSchemaGenerator.new(@model_definition).generate(expand: expand_nested, relationships_to_exclude_from_expansion:)
55
59
  @schema_modifier.add_properties(schema, included_schema, '.')
56
60
  end
57
61
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Schemable
4
- VERSION = '1.0.1'
4
+ VERSION = '1.0.3'
5
5
  end
@@ -41,6 +41,11 @@ Schemable.configure do |config|
41
41
  #
42
42
  # config.infer_attributes_from_custom_method = nil
43
43
 
44
+ # If you want to recursively expand the relationships in the response schema
45
+ # then set this option to true, otherwise set it to false (default).
46
+ #
47
+ # config.infer_expand_nested_from_expand = true
48
+
44
49
  # If you want to get the list of attributes from the jsonapi-rails gem's
45
50
  # JSONAPI::Serializable::Resource class, set this option to true.
46
51
  # It uses the attribute_blocks method to get the list of attributes.
@@ -8,6 +8,7 @@ module Schemable
8
8
  attr_accessor custom_defined_enum_method: Symbol?
9
9
  attr_accessor enum_prefix_for_simple_enum: String?
10
10
  attr_accessor enum_suffix_for_simple_enum: String?
11
+ attr_accessor infer_expand_nested_from_expand: bool
11
12
  attr_accessor custom_type_mappers: Hash[Symbol, any]
12
13
  attr_accessor infer_attributes_from_custom_method: Symbol?
13
14
  attr_accessor custom_meta_response_schema: Hash[Symbol, any]?
@@ -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]
@@ -8,6 +8,6 @@ module Schemable
8
8
  def initialize: (Definition) -> void
9
9
  def meta: -> Hash[Symbol, any]
10
10
  def jsonapi: -> Hash[Symbol, any]
11
- def generate: (expand: bool, relationships_to_exclude_from_expansion: Array[Symbol], collection: bool) -> Hash[Symbol, any]
11
+ def generate: (expand: bool, relationships_to_exclude_from_expansion: Array[Symbol], collection: bool, expand_nested: bool) -> Hash[Symbol, any]
12
12
  end
13
13
  end
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.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muhammad Nawzad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-29 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