typed_params 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 014c42e82286c1ffc175d08aca51e631da96a5b2aee8e9bbf575fc0bea7e1d57
4
- data.tar.gz: 3a0ea8bac6b1021143b768767763899a6f5328f16bb6011a87b2f1ed694b6325
3
+ metadata.gz: 160a6e63cc497232a6c33d2cdc06050682c44e71624bee8d70f72e25b3ef3374
4
+ data.tar.gz: f0e1c30200d4ac4fc555cd5418546ed3ceba69dc54b09a98a02e4b349dcfd014
5
5
  SHA512:
6
- metadata.gz: c563758456e6359e37c75926124925141c59f535fc77070be23d46d3f86e0772f2cf2c52d2e57f165e2ba11f93717c6a0b15f90e61fcbe00baa19b3c1985f2a8
7
- data.tar.gz: a277c1c13a5d0ac14ef293fbc9611bec82eba5df00686f9afaf8e324e89148aef7c49fa672bf025570a9482ec277e330edc1049a1e59051e41a559a31a4b6584
6
+ metadata.gz: 773babee3c280766879e615a654206f1665a3811ac98fe673dcab655ef6e42471335f9cc2605e999eed2aa06a3aa5e6a85ed74fcd4e138b39f97467fc28adef0
7
+ data.tar.gz: 0ab8544aa6011e99256af88dafe207b27509f78b7db906cc41f47192967fc5b24f7fe8d8b352729908083026804ec39f5aec314b27747c2ea222443e092e4276
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.2
4
+
5
+ - Fix issue where `as:` keyword was not handled in JSONAPI relationship formatter.
6
+
7
+ ## 1.2.1
8
+
9
+ - Update JSONAPI formatter to simplify logic for polymorphic relationships.
10
+
3
11
  ## 1.2.0
4
12
 
5
13
  - Add `aliases:` keyword to `#param` to allow the schema to be accessible by different names.
data/README.md CHANGED
@@ -689,8 +689,10 @@ param :invalid, type: :string, validate: -> v {
689
689
 
690
690
  #### Polymorphic parameter
691
691
 
692
- ***Currently, this option is only utilized by the JSONAPI formatter. Define
693
- a polymorphic parameter, used when formatting JSONAPI relationships.***
692
+ _Note: currently, this option is only utilized by the JSONAPI formatter._
693
+
694
+ Define a polymorphic parameter. Actual behavior will vary based on the
695
+ formatter being used.
694
696
 
695
697
  ```ruby
696
698
  format :jsonapi
@@ -711,8 +713,8 @@ typed_params # => { owner_type: 'User', owner_id: 1 }
711
713
 
712
714
  In this example, a polymorphic `:owner` relationship is defined. When run
713
715
  through the JSONAPI formatter, instead of formatting the relationship
714
- into the `:owner_id` key, it also includes the `:owner_type` key
715
- for a polymorphic association.
716
+ into solely the `:owner_id` key, it also includes the `:owner_type`
717
+ key for a polymorphic association.
716
718
 
717
719
  ### Shared options
718
720
 
@@ -58,9 +58,10 @@ module TypedParams
58
58
  end
59
59
 
60
60
  def self.format_hash_data(data, schema:)
61
- rels = data[:relationships]
62
- attrs = data[:attributes]
63
- res = data.except(
61
+ relationships = data[:relationships]
62
+ attributes = data[:attributes]
63
+
64
+ res = data.except(
64
65
  :attributes,
65
66
  :links,
66
67
  :meta,
@@ -69,25 +70,28 @@ module TypedParams
69
70
  )
70
71
 
71
72
  # Move attributes over to top-level params
72
- attrs&.each do |key, attr|
73
- res[key] = attr
73
+ attributes&.each do |key, attribute|
74
+ res[key] = attribute
74
75
  end
75
76
 
76
77
  # Move relationships over. This will use x_id and x_ids when the
77
78
  # relationship data only contains :type and :id, otherwise it
78
79
  # will use the x_attributes key.
79
- rels&.each do |key, rel|
80
- child = schema.children.dig(:relationships, key)
81
-
82
- case rel
80
+ relationships&.each do |key, relationship|
81
+ child = schema.children.fetch(:relationships).then do |rels|
82
+ rels.children.fetch(key) {
83
+ rels.children.values.find { _1.as == key || _1.alias == key }
84
+ }
85
+ end
86
+
87
+ case relationship
83
88
  # FIXME(ezekg) We need https://bugs.ruby-lang.org/issues/18961 to
84
89
  # clean this up (i.e. remove the if guard).
85
90
  in data: [{ type:, id:, **nil }, *] => linkage if linkage.all? { _1 in type:, id:, **nil }
86
91
  res[:"#{key.to_s.singularize}_ids"] = linkage.map { _1[:id] }
87
92
  in data: []
88
93
  res[:"#{key.to_s.singularize}_ids"] = []
89
- # FIXME(ezekg) Not sure how to make this cleaner, but this handles polymorphic relationships.
90
- in data: { type:, id:, **nil } if key.to_s.underscore.classify != type.underscore.classify && child.polymorphic?
94
+ in data: { type:, id:, **nil } if child.polymorphic?
91
95
  res[:"#{key}_type"], res[:"#{key}_id"] = type.underscore.classify, id
92
96
  in data: { type:, id:, **nil }
93
97
  res[:"#{key}_id"] = id
@@ -96,7 +100,7 @@ module TypedParams
96
100
  else
97
101
  # NOTE(ezekg) Embedded relationships are non-standard as per the
98
102
  # JSONAPI spec, but I don't really care. :)
99
- res[:"#{key}_attributes"] = call(rel, schema: child)
103
+ res[:"#{key}_attributes"] = call(relationship, schema: child)
100
104
  end
101
105
  end
102
106
 
@@ -10,6 +10,7 @@ module TypedParams
10
10
  :source,
11
11
  :type,
12
12
  :key,
13
+ :as,
13
14
  :alias,
14
15
  :if,
15
16
  :unless
@@ -80,6 +81,7 @@ module TypedParams
80
81
  @strict = strict
81
82
  @parent = parent
82
83
  @key = key
84
+ @as = as
83
85
  @alias = binding.local_variable_get(:alias)
84
86
  @optional = optional
85
87
  @coerce = coerce && @type.coercable?
@@ -272,7 +274,7 @@ module TypedParams
272
274
  def allow_nil? = !!@allow_nil
273
275
  def allow_non_scalars? = !!@allow_non_scalars
274
276
  def nilify_blanks? = !!@nilify_blanks
275
- def endless? = !!@endless
277
+ def endless? = !!@endless
276
278
  def indexed? = !endless?
277
279
  def if? = !@if.nil?
278
280
  def unless? = !@unless.nil?
@@ -285,9 +287,6 @@ module TypedParams
285
287
  "#<#{self.class.name} key=#{key.inspect} type=#{type.inspect} children=#{children.inspect}>"
286
288
  end
287
289
 
288
- # @private
289
- def dig(*keys) = children.dig(*keys)
290
-
291
290
  private
292
291
 
293
292
  attr_reader :controller,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TypedParams
4
- VERSION = '1.2.0'
4
+ VERSION = '1.2.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zeke Gabrielse