media_types-serialization 1.3.1 → 1.3.6

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: a7e886054d63df27dd6a7cd6deca3d00d9b0a58edcb4a3fa30ae45f369dc2c20
4
- data.tar.gz: c601dca445a03064381dc387a3db4395e97bd8ef427710a591318df699380aa8
3
+ metadata.gz: d53689a0c81861b7305fc606a2b73b7866e4b46915284a716951735926b5bb73
4
+ data.tar.gz: 29add50810aa2bf8b3792d38eebd50eca96570c716a33be6ef58a2f64813be81
5
5
  SHA512:
6
- metadata.gz: 5b23ee08585bbe29ca4caff191afd280745f5325082e9ba4b9f2f0a797ba0fc8368e2df8b21fcb8aa32d50d10740461503bdcfc1c669a5fc5615dac4f6508500
7
- data.tar.gz: 8ae9a64b67d90619947fcf4d50629f2885786776996c070398b12a7b4753caebbb3523a7bce96d91b82a2f1f2c261b2c77490ada853f9b28e4bc49ef7596f2ed
6
+ metadata.gz: 56db0c8b8f39b108e80d028459b0fc7e788bc9cbfd3b7a1e26dfbab993883d7b19735f2d638e8f17edfbcfb9837f610f30ea46ec61bf84c4c141f45000ae0c36
7
+ data.tar.gz: 2985c51bc0f5b59df56d11fe665dc5be479fa5cf494dbd8edf5e57961464bb5e413f82af8e9e867557bb15dd715542aa5f6f10cd9cd9fb12028899fa00d11bf8
@@ -13,21 +13,20 @@ on:
13
13
 
14
14
  jobs:
15
15
  build:
16
-
17
16
  runs-on: ubuntu-latest
18
17
 
19
18
  strategy:
20
19
  matrix:
21
- ruby-version: [2.7.x, 2.6.x]
20
+ ruby-version: [2.7.x, 2.6.x, 2.5.x]
22
21
 
23
22
  steps:
24
- - uses: actions/checkout@v1
25
- - name: Set up Ruby ${{ matrix.ruby-version }}
26
- uses: actions/setup-ruby@v1
27
- with:
28
- ruby-version: ${{ matrix.ruby-version }}
29
- - name: Build and test with Rake
30
- run: |
31
- gem install bundler
32
- bundle install --jobs 4 --retry 3
33
- bundle exec rake
23
+ - uses: actions/checkout@v1
24
+ - name: Set up Ruby ${{ matrix.ruby-version }}
25
+ uses: actions/setup-ruby@v1
26
+ with:
27
+ ruby-version: ${{ matrix.ruby-version }}
28
+ - name: Build and test with Rake
29
+ run: |
30
+ gem install bundler
31
+ bundle install --jobs 4 --retry 3
32
+ bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.6
4
+
5
+ - 🐛 Fix issue with `override_detail` of `Problem`
6
+
7
+ ## 1.3.5
8
+
9
+ - 🐛 Upgrade media-types library so Ruby 2.5 and 2.6 work again
10
+
11
+ ## 1.3.4
12
+
13
+ - Same as `1.3.3`
14
+
15
+ ## 1.3.3
16
+
17
+ - 🐛 Fix override suffix not being picked up correctly
18
+ - 🐛 Fix inability to override suffix for aliases
19
+ - 🐛 Fix inability to override suffix for raw
20
+ - 🐛 Fix default suffix for raw
21
+
22
+ ## 1.3.2
23
+
24
+ - 🐛 Fix override suffix not returning self or new
25
+
3
26
  ## 1.3.1
4
27
 
5
28
  - 🐛 Fix api viewer
data/Gemfile.lock CHANGED
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types-serialization (1.3.1)
4
+ media_types-serialization (1.3.6)
5
5
  actionpack (>= 4.0.0)
6
6
  activesupport (>= 4.0.0)
7
- media_types (>= 2.0.0, < 3.0.0)
7
+ media_types (>= 2.1.1, < 3.0.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
@@ -56,26 +56,26 @@ GEM
56
56
  concurrent-ruby (1.1.9)
57
57
  crass (1.0.6)
58
58
  erubi (1.10.0)
59
- globalid (0.4.2)
60
- activesupport (>= 4.2.0)
59
+ globalid (0.5.2)
60
+ activesupport (>= 5.0)
61
61
  i18n (1.8.10)
62
62
  concurrent-ruby (~> 1.0)
63
- loofah (2.10.0)
63
+ loofah (2.12.0)
64
64
  crass (~> 1.0.2)
65
65
  nokogiri (>= 1.5.9)
66
66
  mail (2.7.1)
67
67
  mini_mime (>= 0.1.1)
68
68
  marcel (1.0.1)
69
- media_types (2.0.1)
69
+ media_types (2.1.1)
70
70
  method_source (1.0.0)
71
71
  mini_mime (1.1.0)
72
72
  minitest (5.14.4)
73
- nio4r (2.5.7)
74
- nokogiri (1.11.7-x64-mingw32)
73
+ nio4r (2.5.8)
74
+ nokogiri (1.12.3-x64-mingw32)
75
75
  racc (~> 1.4)
76
- nokogiri (1.11.7-x86_64-linux)
76
+ nokogiri (1.12.3-x86_64-linux)
77
77
  racc (~> 1.4)
78
- oj (3.12.3)
78
+ oj (3.13.4)
79
79
  racc (1.5.2)
80
80
  rack (2.2.3)
81
81
  rack-test (1.1.0)
@@ -96,7 +96,7 @@ GEM
96
96
  rails-dom-testing (2.0.3)
97
97
  activesupport (>= 4.2.0)
98
98
  nokogiri (>= 1.6)
99
- rails-html-sanitizer (1.3.0)
99
+ rails-html-sanitizer (1.4.1)
100
100
  loofah (~> 2.3)
101
101
  railties (5.2.6)
102
102
  actionpack (= 5.2.6)
@@ -134,4 +134,4 @@ DEPENDENCIES
134
134
  rake (~> 13.0)
135
135
 
136
136
  BUNDLED WITH
137
- 2.2.7
137
+ 2.2.17
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MediaTypes::Serialization
2
2
 
3
- [![Build Status: master](https://travis-ci.com/XPBytes/media_types-serialization.svg)](https://travis-ci.com/XPBytes/media_types-serialization)
3
+ [![Build Status](https://github.com/XPBytes/media_types-serialization/actions/workflows/ci.yml/badge.svg)](https://github.com/XPBytes/media_types-serialization/actions/workflows/ci.yml)
4
4
  [![Gem Version](https://badge.fury.io/rb/media_types-serialization.svg)](https://badge.fury.io/rb/media_types-serialization)
5
5
  [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
6
6
 
@@ -16,11 +16,15 @@ gem 'media_types-serialization'
16
16
 
17
17
  And then execute:
18
18
 
19
- $ bundle
19
+ ```shell
20
+ bundle
21
+ ```
20
22
 
21
23
  Or install it yourself as:
22
24
 
23
- $ gem install media_types-serialization
25
+ ```shell
26
+ gem install media_types-serialization
27
+ ```
24
28
 
25
29
  ## Usage
26
30
 
@@ -221,10 +225,10 @@ BookSerializer.serialize(book, BookValidator.version(3), context: controller)
221
225
 
222
226
  There are convenience methods for serializing arrays of objects based on a template.
223
227
 
224
- #### Indexes
228
+ #### Indexes (index based collections)
225
229
 
226
230
  An index is a collection of urls that point to members of the array.
227
- The index method automatically generates it based on the self links defined in the default view of the same version.
231
+ The index method automatically generates it based on the `self` links defined in the default view (`view: nil`) of the given version.
228
232
 
229
233
  ```ruby
230
234
  class BookSerializer < MediaTypes::Serialization::Base
@@ -264,7 +268,90 @@ BookSerializer.serialize([book], BookValidator.view(:index).version(3), context:
264
268
  # }
265
269
  ```
266
270
 
267
- #### Collections
271
+ ##### How to validate?
272
+
273
+ The `index` dsl does _not_ exist in the validation gem.
274
+ This is how you validate indices:
275
+
276
+ ```ruby
277
+ class BookValidator
278
+ include MediaTypes::Dsl
279
+
280
+ def self.organisation
281
+ 'acme'
282
+ end
283
+
284
+ use_name 'book'
285
+
286
+ validations do
287
+ view :index do
288
+ version 3 do
289
+ attribute :books do
290
+ link :self
291
+
292
+ collection :_index, allow_empty: true do
293
+ attribute :href, String
294
+ end
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
300
+ ```
301
+
302
+ If the `:self` link contains _more attributes_, they will show up here too.
303
+
304
+ ```ruby
305
+ class BookSerializer < MediaTypes::Serialization::Base
306
+ validator BookValidator
307
+
308
+ output versions: [1, 2, 3] do |obj, version, context|
309
+ attribute :book do
310
+ link :self, href: context.book_url(obj), isbn: obj.isbn if version >= 3
311
+
312
+ attribute :title, obj.title
313
+ attribute :description, obj.description if version >= 2
314
+ end
315
+ end
316
+
317
+ output view: :index, version: 3 do |arr, version, context|
318
+ attribute :books do
319
+ link :self, href: context.book_index_url
320
+
321
+ index arr, version: version
322
+ end
323
+ end
324
+ end
325
+ ```
326
+
327
+ ```ruby
328
+ class BookValidator
329
+ include MediaTypes::Dsl
330
+
331
+ def self.organisation
332
+ 'acme'
333
+ end
334
+
335
+ use_name 'book'
336
+
337
+ validations do
338
+ view :index do
339
+ version 3 do
340
+ attribute :books do
341
+ link :self
342
+
343
+ collection :_index, allow_empty: true do
344
+ attribute :href, String
345
+ attribute :isbn, AllowNil(String)
346
+ end
347
+ end
348
+ end
349
+ end
350
+ end
351
+ end
352
+ ```
353
+
354
+ #### Collections (embedding collections)
268
355
 
269
356
  A collection inlines the member objects.
270
357
  The collection method automatically generates it based on the default view of the same version.
@@ -321,6 +408,38 @@ BookSerializer.serialize([book], BookValidator.view(:collection).version(3), con
321
408
  # }
322
409
  ```
323
410
 
411
+ The `collection` dsl is _not_ the same as the one in the validation gem.
412
+ This is how you could validate collections:
413
+
414
+ ```ruby
415
+ class BookValidator
416
+ include MediaTypes::Dsl
417
+
418
+ def self.organisation
419
+ 'acme'
420
+ end
421
+
422
+ use_name 'book'
423
+
424
+ validations do
425
+ view :collection do
426
+ version 3 do
427
+ attribute :books do
428
+ link :self
429
+
430
+ collection :_embedded, allow_empty: true do
431
+ link :self
432
+
433
+ attribute :title, String
434
+ attribute :description, AllowNil(String)
435
+ end
436
+ end
437
+ end
438
+ end
439
+ end
440
+ end
441
+ ```
442
+
324
443
  ### Input deserialization
325
444
 
326
445
  You can mark a media type as something that's allowed to be sent along with a PUT request as follows:
@@ -454,12 +573,15 @@ end
454
573
  ### Remapping media type identifiers
455
574
 
456
575
  Sometimes you already have old clients using an `application/json` media type identifier when they do requests.
457
- While this is not a good practise as this makes it hard to add new fields or remove old ones, this library has support for migrating away:
576
+ While this is not a good practice as this makes it hard to add new fields or remove old ones, this library has support for migrating away:
458
577
 
459
578
  ```ruby
460
579
  class BookSerializer < MediaTypes::Serialization::Base
461
580
  validator BookValidator
462
581
 
582
+ # applicaton/vnd.acme.book.v3+json
583
+ # applicaton/vnd.acme.book.v2+json
584
+ # applicaton/vnd.acme.book.v1+json
463
585
  output versions: [1, 2, 3] do |obj, version, context|
464
586
  attribute :book do
465
587
  link :self, href: context.book_url(obj) if version >= 3
@@ -468,8 +590,18 @@ class BookSerializer < MediaTypes::Serialization::Base
468
590
  attribute :description, obj.description if version >= 2
469
591
  end
470
592
  end
471
- output_alias 'application/json' # maps application/json to to applicaton/vnd.acme.book.v1+json
472
593
 
594
+ # applicaton/vnd.acme.book+json
595
+ output version: nil do |obj, version, context|
596
+ attribute :book do
597
+ attribute :title, obj.title
598
+ end
599
+ end
600
+ output_alias 'application/json' # maps application/json to to applicaton/vnd.acme.book+json
601
+
602
+ # applicaton/vnd.acme.book.v3.create+json
603
+ # applicaton/vnd.acme.book.v2.create+json
604
+ # applicaton/vnd.acme.book.v1.create+json
473
605
  input view: :create, versions: [1, 2, 3] do |json, version, context|
474
606
  book = Book.new
475
607
  book.title = json['book']['title']
@@ -479,10 +611,21 @@ class BookSerializer < MediaTypes::Serialization::Base
479
611
  # Make sure not to save here but only save in the controller
480
612
  book
481
613
  end
482
- input_alias 'application/json', view: :create # maps application/json to to applicaton/vnd.acme.book.v1+json
614
+
615
+ # applicaton/vnd.acme.book.create+json
616
+ input view: :create, version: nil do |json, version, context|
617
+ book = Book.new
618
+ book.title = json['book']['title']
619
+ book.description = 'Not available'
620
+
621
+ # Make sure not to save here but only save in the controller
622
+ book
623
+ end
624
+ input_alias 'application/json', view: :create # maps application/json to to applicaton/vnd.acme.book.create+json
483
625
  ```
484
626
 
485
- Validation will be done using the remapped validator. Aliasses map to version `nil` if that is available or `1` otherwise. It is not possible to configure this version.
627
+ Validation will be done using the remapped validator. Aliasses map to version `nil`.
628
+ It is not possible to configure this version.
486
629
 
487
630
  ### HTML
488
631
 
@@ -563,12 +706,13 @@ class BookController < ActionController::API
563
706
  end
564
707
  ```
565
708
 
566
- The exception you specified will be rescued by the controller and will be displayed to the user along with a link to the shared wiki page for that error type. Feel free to add instructions there on how clients should solve this problem.
709
+ The exception you specified will be rescued by the controller and will be displayed to the user along with a link to the shared wiki page for that error type.
710
+ Feel free to add instructions there on how clients should solve this problem.
567
711
  You can find more information at: [https://docs.delftsolutions.nl/wiki/Error](https://docs.delftsolutions.nl/wiki/Error)
568
712
  If you want to override this url you can use the `problem_output.url(href)` function.
569
713
 
570
714
  By default the `message` property of the error is used to fill the `details` field.
571
- You can override this by using the `problem_output.override_details(description, lang:)` function.
715
+ You can override this by using the `problem_output.override_detail(description, lang:)` function.
572
716
 
573
717
  Custom attributes can be added using the `problem_output.attribute(name, value)` function.
574
718
 
@@ -578,7 +722,7 @@ Custom attributes can be added using the `problem_output.attribute(name, value)`
578
722
 
579
723
  ## API
580
724
 
581
- ### Serializer definition
725
+ ### Serializer class definition
582
726
 
583
727
  These methods become available during class definition if you inherit from `MediaTypes::Serialization::Base`.
584
728
 
@@ -597,6 +741,7 @@ Either validator or unvalidated must be used while defining a serializer.
597
741
  #### `output( view:, version:, versions: ) do |obj, version, context|`
598
742
 
599
743
  Defines a serialization block. Either version or versions can be set.
744
+ `nil` is allowed for unversioned.
600
745
  View should be a symbol or unset.
601
746
 
602
747
  Obj is the object to be serialized, version is the negotiated version and context is the context passed in from the serialize function.
@@ -604,26 +749,30 @@ When using the controller integration, context is the current controller.
604
749
 
605
750
  The block should return an object to convert into JSON.
606
751
 
607
- #### `output_raw( view:, version:, versions: ) do |obj, version, context|`
752
+ #### `output_raw( view:, version:, versions:, suffix: ) do |obj, version, context|`
608
753
 
609
754
  This has the same behavior as `output` but should return a string instead of an object.
610
755
  Output is not validated.
756
+ By default, `input_raw` is expected to _not_ be JSON.
757
+ Override `suffix` with `:json` if it _is_ JSON.
611
758
 
612
- #### `output_alias( media_type_identifier, view:, hide_variant: false )`
759
+ #### `output_alias( media_type_identifier, view:, hide_variant: false, suffix: '~' )`
613
760
 
614
761
  Defines a legacy mapping. This will make the deserializer parse the media type `media_type_identifier` as if it was version `nil` of the specified view.
615
762
  If `view` is undefined it will use the output serializer without a view defined.
763
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
616
764
 
617
765
  Response will have a content type equal to `[media_type_identifier]; variant=[mapped_media_type_identifier]`.
618
766
  If `hide_variant:` is true, the content type emitted will only be `[media_type_identifier]`.
619
767
 
620
768
  > You cannot alias a _versioned_ media type, otherwise it would be easy to later break the definition by changing the version it aliases.
621
769
 
622
- #### `output_alias_optional( media_type_identifier, view:, hide_variant: false )`
770
+ #### `output_alias_optional( media_type_identifier, view:, hide_variant: false, suffix: '~' )`
623
771
 
624
772
  Has the same behavior as `output_alias` but can be used by multiple serializers.
625
773
  The serializer that is loaded last in the controller 'wins' control over this media type identifier.
626
774
  If any of the serializers have an `output_alias` defined with the same media type identifier that one will win instead.
775
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
627
776
 
628
777
  Response will have a content type equal to `[media_type_identifier]; variant=[mapped_media_type_identifier]`. If `hide_variant:` is true, the content type emitted will only be `[media_type_identifier]`.
629
778
 
@@ -638,29 +787,34 @@ When using the controller integration, context is the current controller.
638
787
  The block should return the internal representation of the object.
639
788
  Best practise is to make sure not to change state in this function but to leave that up to the controller.
640
789
 
641
- #### `input_raw( view:, version:, versions: ) do |bytes, version, context|`
790
+ #### `input_raw( view:, version:, versions:, suffix: nil ) do |bytes, version, context|`
642
791
 
643
- This has the same behavior as `input` but takes in raw data. Input is not validated.
792
+ This has the same behavior as `input` but takes in raw data.
793
+ Input is not validated.
794
+ By default, `input_raw` is expected to _not_ be JSON.
795
+ Override `suffix` with `:json` if it _is_ JSON.
644
796
 
645
- #### `input_alias( media_type_identifier, view: )`
797
+ #### `input_alias( media_type_identifier, view:, suffix: '~' )`
646
798
 
647
799
  Defines a legacy mapping.
648
800
  This will make the serializer parse the media type `media_type_identifier` as if it was version `nil` of the specified view.
649
801
  If view is undefined it will use the input serializer without a view defined.
802
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
650
803
 
651
804
  > You cannot alias a _versioned_ media type, otherwise it would be easy to later break the definition by changing the version it aliases.
652
805
 
653
- #### `input_alias_optional( media_type_identifier, view: )`
806
+ #### `input_alias_optional( media_type_identifier, view:, suffix: '~' )`
654
807
 
655
808
  Has the same behavior as `input_alias` but can be used by multiple serializers.
656
809
  The serializer that is loaded last in the controller 'wins' control over this media type identifier.
657
810
  If any of the serializers have an `input_alias` defined with the same media type identifier that one will win instead.
811
+ By default, suffix is `:json` if `media_type_identifier` is a JSON type.
658
812
 
659
813
  #### `disable_wildcards`
660
814
 
661
815
  Disables registering wildcard media types.
662
816
 
663
- ### Serializer definition
817
+ ### Serializer output definition
664
818
 
665
819
  The following methods are available within an `output ... do` block.
666
820
 
@@ -682,12 +836,16 @@ Returns the built up context so far.
682
836
 
683
837
  #### `index( array, serializer, version:, view: nil )`
684
838
 
839
+ > Not the same as a validator `collection`.
840
+
685
841
  Adds an `_index` block to the current context. Uses the self links of the specified view to construct an index of urls to the child objects.
686
842
 
687
843
  Returns the built up context so far.
688
844
 
689
845
  #### `collection( array, serializer, version:, view: nil )`
690
846
 
847
+ > Not the same as a validator `collection`.
848
+
691
849
  Adds an `_embedded` block to the current context. Uses the specified serializer to embed the child objects.
692
850
  Optionally a block can be used to modify the output from the child serializer.
693
851
 
@@ -711,9 +869,25 @@ Returns the built up context so far.
711
869
 
712
870
  Runs a block in a new context and returns the result
713
871
 
872
+ > Most common use-case is emitting from an enumerable.
873
+ >
874
+ > ```ruby
875
+ > results = [item, item, item].map do |current_item|
876
+ > object do
877
+ > attribute :foo, current_item.bar
878
+ > end
879
+ > end
880
+ >
881
+ > attribute :items, results
882
+ > ```
883
+
714
884
  #### `render_view( view, context:, **args)`
715
885
 
716
- Can be used to render a view. You can set local variables in the view by assigning a hash to the `assigns:` parameter.
886
+ Can be used to render a view.
887
+ You can set local variables in the view by assigning a hash to the `assigns:` parameter.
888
+ Returns a `string`
889
+
890
+ > When possible, prefer `output_raw` with `context.class.render(params)`
717
891
 
718
892
  ### Controller definition
719
893
 
@@ -31,7 +31,7 @@ module MediaTypes
31
31
 
32
32
  def override_detail(detail, lang:)
33
33
  raise 'Unable to override detail message without having a title in the same language.' unless translations[lang]
34
- translations[lang][:detail] = title
34
+ translations[lang][:detail] = detail
35
35
  end
36
36
 
37
37
  def attribute(name, value)
@@ -44,35 +44,76 @@ module MediaTypes
44
44
  validator = serializer_validator.view(view).version(v)
45
45
  validator.override_suffix(:json) unless serializer_validated
46
46
 
47
- serializer_output_registration.register_block(self, validator, v, block, false, wildcards: !self.serializer_disable_wildcards)
47
+ serializer_output_registration.register_block(
48
+ self,
49
+ validator,
50
+ v,
51
+ block,
52
+ false,
53
+ wildcards:
54
+ !serializer_disable_wildcards
55
+ )
48
56
  end
49
57
  end
50
58
 
51
- def output_raw(view: nil, version: nil, versions: nil, &block)
59
+ def output_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
52
60
  versions = [version] if versions.nil?
53
61
  raise VersionsNotAnArrayError unless versions.is_a? Array
54
62
 
55
63
  raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?
56
64
 
57
65
  versions.each do |v|
58
- validator = serializer_validator.view(view).version(v).override_suffix('')
59
-
60
- serializer_output_registration.register_block(self, validator, v, block, true, wildcards: !self.serializer_disable_wildcards)
66
+ validator = serializer_validator.view(view)
67
+ .version(v)
68
+ .override_suffix(suffix)
69
+
70
+ serializer_output_registration.register_block(
71
+ self,
72
+ validator,
73
+ v,
74
+ block,
75
+ true,
76
+ wildcards: !serializer_disable_wildcards
77
+ )
61
78
  end
62
79
  end
63
80
 
64
- def output_alias(media_type_identifier, view: nil, hide_variant: false)
65
- validator = serializer_validator.view(view)
81
+ def output_alias(
82
+ media_type_identifier,
83
+ view: nil,
84
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
85
+ hide_variant: false
86
+ )
87
+ validator = serializer_validator.view(view).override_suffix(suffix)
66
88
  victim_identifier = validator.identifier
67
89
 
68
- serializer_output_registration.register_alias(self, media_type_identifier, victim_identifier, false, hide_variant, wildcards: !self.serializer_disable_wildcards)
69
- end
70
-
71
- def output_alias_optional(media_type_identifier, view: nil, hide_variant: false)
72
- validator = serializer_validator.view(view)
90
+ serializer_output_registration.register_alias(
91
+ self,
92
+ media_type_identifier,
93
+ victim_identifier,
94
+ false,
95
+ hide_variant,
96
+ wildcards: !serializer_disable_wildcards
97
+ )
98
+ end
99
+
100
+ def output_alias_optional(
101
+ media_type_identifier,
102
+ view: nil,
103
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
104
+ hide_variant: false
105
+ )
106
+ validator = serializer_validator.view(view).override_suffix(suffix)
73
107
  victim_identifier = validator.identifier
74
108
 
75
- serializer_output_registration.register_alias(self, media_type_identifier, victim_identifier, true, hide_variant, wildcards: !self.serializer_disable_wildcards)
109
+ serializer_output_registration.register_alias(
110
+ self,
111
+ media_type_identifier,
112
+ victim_identifier,
113
+ true,
114
+ hide_variant,
115
+ wildcards: !serializer_disable_wildcards
116
+ )
76
117
  end
77
118
 
78
119
  def input(view: nil, version: nil, versions: nil, &block)
@@ -89,31 +130,53 @@ module MediaTypes
89
130
  end
90
131
  end
91
132
 
92
- def input_raw(view: nil, version: nil, versions: nil, &block)
133
+ def input_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
93
134
  versions = [version] if versions.nil?
94
135
  raise VersionsNotAnArrayError unless versions.is_a? Array
95
136
 
96
137
  raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?
97
138
 
98
139
  versions.each do |v|
99
- validator = serializer_validator.view(view).version(v)
140
+ validator = serializer_validator.view(view).version(v).override_suffix(suffix)
100
141
 
101
142
  serializer_input_registration.register_block(self, validator, v, block, true)
102
143
  end
103
144
  end
104
145
 
105
- def input_alias(media_type_identifier, view: nil)
106
- validator = serializer_validator.view(view)
146
+ def input_alias(
147
+ media_type_identifier,
148
+ view: nil,
149
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
150
+ )
151
+ validator = serializer_validator.view(view).override_suffix(suffix)
107
152
  victim_identifier = validator.identifier
108
153
 
109
- serializer_input_registration.register_alias(self, media_type_identifier, victim_identifier, false, true, wildcards: false)
110
- end
111
-
112
- def input_alias_optional(media_type_identifier, view: nil)
113
- validator = serializer_validator.view(view)
154
+ serializer_input_registration.register_alias(
155
+ self,
156
+ media_type_identifier,
157
+ victim_identifier,
158
+ false,
159
+ true,
160
+ wildcards: false
161
+ )
162
+ end
163
+
164
+ def input_alias_optional(
165
+ media_type_identifier,
166
+ view: nil,
167
+ suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
168
+ )
169
+ validator = serializer_validator.view(view).override_suffix(suffix)
114
170
  victim_identifier = validator.identifier
115
171
 
116
- serializer_input_registration.register_alias(self, media_type_identifier, victim_identifier, true, true, wildcards: false)
172
+ serializer_input_registration.register_alias(
173
+ self,
174
+ media_type_identifier,
175
+ victim_identifier,
176
+ true,
177
+ true,
178
+ wildcards: false
179
+ )
117
180
  end
118
181
 
119
182
  def serialize(victim, media_type_identifier, context:, dsl: nil, raw: nil)
@@ -135,6 +198,8 @@ module MediaTypes
135
198
  end
136
199
 
137
200
  def self.inherited(subclass)
201
+ super
202
+
138
203
  subclass.extend(ClassMethods)
139
204
  subclass.instance_eval do
140
205
  class << self
@@ -22,6 +22,7 @@ class FakeValidator
22
22
 
23
23
  def override_suffix(suffix)
24
24
  suffixes[[internal_view, internal_version]] = suffix
25
+ FakeValidator.new(prefix, internal_view, version, suffixes)
25
26
  end
26
27
 
27
28
  def identifier
@@ -36,7 +36,7 @@ module MediaTypes
36
36
 
37
37
  unless registrations.key? target_identifier
38
38
  potential_match = registrations.keys.find do |k|
39
- k.starts_with? target_identifier
39
+ k.start_with? target_identifier
40
40
  end
41
41
  raise VersionedAliasDefinitionError.new(target_identifier, inout, potential_match) unless potential_match.nil?
42
42
  raise UnbackedAliasDefinitionError.new(target_identifier, inout)
@@ -152,7 +152,7 @@ module MediaTypes
152
152
  self.raw = raw
153
153
  super(serializer, inout, validator, display_identifier)
154
154
  end
155
-
155
+
156
156
  def merge(other)
157
157
  return nil unless other.is_a?(SerializationAliasRegistration)
158
158
 
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Serialization
3
- VERSION = '1.3.1'.freeze
3
+ VERSION = '1.3.6'.freeze
4
4
  end
5
5
  end
@@ -345,7 +345,10 @@ module MediaTypes
345
345
 
346
346
  MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED = ::Object.new
347
347
 
348
- def render_media(obj = MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED, serializers: nil, not_acceptable_serializer: nil, **options, &block)
348
+ def render_media(obj = MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED, **options, &block)
349
+ serializers = options.delete(:serializers)
350
+ not_acceptable_serializer = options.delete(:not_acceptable_serializer)
351
+
349
352
  if obj == MEDIA_TYPES_SERIALIZATION_OBJ_IS_UNDEFINED && options.keys.any? && !block
350
353
  # options is too greedy :(
351
354
  obj = options
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
37
37
 
38
38
  spec.add_dependency 'actionpack', '>= 4.0.0'
39
39
  spec.add_dependency 'activesupport', '>= 4.0.0'
40
- spec.add_dependency 'media_types', '>= 2.0.0', '< 3.0.0'
40
+ spec.add_dependency 'media_types', '>= 2.1.1', '< 3.0.0'
41
41
 
42
42
  spec.add_development_dependency 'awesome_print'
43
43
  spec.add_development_dependency 'bundler'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_types-serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-08-11 00:00:00.000000000 Z
12
+ date: 2021-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -45,7 +45,7 @@ dependencies:
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: 2.0.0
48
+ version: 2.1.1
49
49
  - - "<"
50
50
  - !ruby/object:Gem::Version
51
51
  version: 3.0.0
@@ -55,7 +55,7 @@ dependencies:
55
55
  requirements:
56
56
  - - ">="
57
57
  - !ruby/object:Gem::Version
58
- version: 2.0.0
58
+ version: 2.1.1
59
59
  - - "<"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.0.0