media_types-serialization 1.3.6 → 1.3.7

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: d53689a0c81861b7305fc606a2b73b7866e4b46915284a716951735926b5bb73
4
- data.tar.gz: 29add50810aa2bf8b3792d38eebd50eca96570c716a33be6ef58a2f64813be81
3
+ metadata.gz: 159a20aec1547f40a318c666acc8616e3b3396317b552f0a8cf7501fae609b5f
4
+ data.tar.gz: 485188d6f57ec55c684952fe9c00051ac1dbcbdd93dbc8136f26dd5e8ce351d3
5
5
  SHA512:
6
- metadata.gz: 56db0c8b8f39b108e80d028459b0fc7e788bc9cbfd3b7a1e26dfbab993883d7b19735f2d638e8f17edfbcfb9837f610f30ea46ec61bf84c4c141f45000ae0c36
7
- data.tar.gz: 2985c51bc0f5b59df56d11fe665dc5be479fa5cf494dbd8edf5e57961464bb5e413f82af8e9e867557bb15dd715542aa5f6f10cd9cd9fb12028899fa00d11bf8
6
+ metadata.gz: 932707eb3baa03f83ea24d7a70cdc3750b41122ab62446d5d4a1956d6eb1b13a2bec4e86b3ed4e2f4689608f2589bc40aae92add3e4a81a016fa7ef5684e944a
7
+ data.tar.gz: e083a0e056227b9fe46c0a380c37cf5af8cdd3a9835b216f6b5aa2248669987be6b4ad8ff533f855f064cad59bc052e60a8201cebf738389b8c462cc7e1ae732
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.7
4
+
5
+ - 🐛 Fix execution context for `output_error`
6
+
3
7
  ## 1.3.6
4
8
 
5
9
  - 🐛 Fix issue with `override_detail` of `Problem`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types-serialization (1.3.6)
4
+ media_types-serialization (1.3.7)
5
5
  actionpack (>= 4.0.0)
6
6
  activesupport (>= 4.0.0)
7
7
  media_types (>= 2.1.1, < 3.0.0)
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Serialization
3
- VERSION = '1.3.6'.freeze
3
+ VERSION = '1.3.7'.freeze
4
4
  end
5
5
  end
@@ -100,7 +100,6 @@ module MediaTypes
100
100
 
101
101
  # rubocop:disable Metrics/BlockLength
102
102
  class_methods do
103
-
104
103
  def not_acceptable_serializer(serializer, **filter_opts)
105
104
  before_action(**filter_opts) do
106
105
  raise SerializersAlreadyFrozenError if defined? @serialization_frozen
@@ -315,22 +314,26 @@ module MediaTypes
315
314
  def freeze_io!(**filter_opts)
316
315
  before_action :serializer_freeze_io_internal, **filter_opts
317
316
 
318
- output_error MediaTypes::Serialization::NoInputReceivedError do |p, error|
317
+ output_error MediaTypes::Serialization::NoInputReceivedError do |p, _error|
319
318
  p.title 'Providing input is mandatory. Please set a Content-Type', lang: 'en'
320
319
 
321
320
  p.status_code :bad_request
322
321
  end
323
322
  end
324
323
 
325
- def output_error(klazz, &block)
324
+ def output_error(klazz, additional_serializers: [])
326
325
  rescue_from klazz do |error|
327
326
  problem = Problem.new(error)
328
- block.call(problem, error) unless block.nil?
327
+ instance_exec { yield problem, error, self } if block_given?
329
328
 
330
329
  serializer = MediaTypes::Serialization::Serializers::ProblemSerializer
331
330
  registrations = serializer.outputs_for(views: [:html, nil])
332
331
 
333
- render_media(problem, serializers: [registrations], status: problem.response_status_code)
332
+ render_media(
333
+ problem,
334
+ serializers: additional_serializers.concat([registrations]),
335
+ status: problem.response_status_code
336
+ )
334
337
  end
335
338
  end
336
339
  end
@@ -363,7 +366,9 @@ module MediaTypes
363
366
 
364
367
  raise SerializersNotFrozenError unless defined? @serialization_frozen
365
368
 
366
- not_acceptable_serializer ||= @serialization_not_acceptable_serializer if defined? @serialization_not_acceptable_serializer
369
+ if defined? @serialization_not_acceptable_serializer
370
+ not_acceptable_serializer ||= @serialization_not_acceptable_serializer
371
+ end
367
372
 
368
373
  @serialization_output_registrations ||= SerializationRegistration.new(:output)
369
374
  registration = @serialization_output_registrations
@@ -469,7 +474,14 @@ module MediaTypes
469
474
  obj = { request: request, registrations: registrations }
470
475
  new_registrations = serializer.outputs_for(views: [nil])
471
476
 
472
- serialization_render_resolved(obj: obj, serializer: serializer, identifier: identifier, registrations: new_registrations, options: {})
477
+ serialization_render_resolved(
478
+ obj: obj,
479
+ serializer: serializer,
480
+ identifier: identifier,
481
+ registrations: new_registrations,
482
+ options: {}
483
+ )
484
+
473
485
  response.status = :not_acceptable
474
486
  end
475
487
 
@@ -518,7 +530,11 @@ module MediaTypes
518
530
  if input_is_allowed && !request.content_type.blank?
519
531
  begin
520
532
  input_data = request.body.read
521
- @serialization_decoded_input = @serialization_input_registrations.decode(input_data, request.content_type, self)
533
+ @serialization_decoded_input = @serialization_input_registrations.decode(
534
+ input_data,
535
+ request.content_type,
536
+ self
537
+ )
522
538
  rescue InputValidationFailedError => e
523
539
  serializers = @serialization_input_validation_failed_serializer || [
524
540
  MediaTypes::Serialization::Serializers::ProblemSerializer,
@@ -532,7 +548,7 @@ module MediaTypes
532
548
  input = {
533
549
  identifier: request.content_type,
534
550
  input: input_data,
535
- error: e,
551
+ error: e
536
552
  }
537
553
 
538
554
  render_media nil, serializers: [registrations], status: :unprocessable_entity do
@@ -554,9 +570,17 @@ module MediaTypes
554
570
 
555
571
  # All endpoints have endpoint description.
556
572
  # Placed in front of the list to make sure the api viewer doesn't pick it.
557
- @serialization_output_registrations = description_serializer.outputs_for(views: [nil]).merge(@serialization_output_registrations)
573
+ @serialization_output_registrations =
574
+ description_serializer
575
+ .outputs_for(views: [nil])
576
+ .merge(@serialization_output_registrations)
577
+
578
+ endpoint_matched_identifier = resolve_media_type(
579
+ request,
580
+ description_serializer.serializer_output_registration,
581
+ allow_last: false
582
+ )
558
583
 
559
- endpoint_matched_identifier = resolve_media_type(request, description_serializer.serializer_output_registration, allow_last: false)
560
584
  if endpoint_matched_identifier
561
585
  # We picked an endpoint description media type
562
586
  #
@@ -566,7 +590,7 @@ module MediaTypes
566
590
 
567
591
  input = {
568
592
  api_viewer: @serialization_api_viewer_enabled,
569
- actions: @serialization_available_serializers,
593
+ actions: @serialization_available_serializers
570
594
  }
571
595
 
572
596
  serialization_render_resolved(
@@ -583,13 +607,19 @@ module MediaTypes
583
607
  resolved_identifier = resolve_media_type(request, @serialization_output_registrations)
584
608
 
585
609
  not_acceptable_serializer = nil
586
- not_acceptable_serializer = @serialization_not_acceptable_serializer if defined? @serialization_not_acceptable_serializer
610
+
611
+ if defined? @serialization_not_acceptable_serializer
612
+ not_acceptable_serializer = @serialization_not_acceptable_serializer
613
+ end
614
+
587
615
  not_acceptable_serializer ||= MediaTypes::Serialization::Serializers::FallbackNotAcceptableSerializer
588
616
 
589
617
  can_satisfy_allow = !resolved_identifier.nil?
590
618
  can_satisfy_allow ||= @serialization_output_allow_all if defined?(@serialization_output_allow_all)
591
619
 
592
- serialization_render_not_acceptable(@serialization_output_registrations, not_acceptable_serializer) unless can_satisfy_allow
620
+ unless can_satisfy_allow
621
+ serialization_render_not_acceptable(@serialization_output_registrations, not_acceptable_serializer)
622
+ end
593
623
  end
594
624
 
595
625
  def serialization_render_resolved(obj:, identifier:, serializer:, registrations:, options:)
@@ -601,14 +631,19 @@ module MediaTypes
601
631
  if links.any?
602
632
  items = links.map do |l|
603
633
  href_part = "<#{l[:href]}>"
604
- tags = l.to_a.select { |k,_| k != :href }.map { |k,v| "#{k}=#{v}" }
634
+ tags = l.to_a.reject { |k, _| k == :href }.map { |k, v| "#{k}=#{v}" }
605
635
  ([href_part] + tags).join('; ')
606
636
  end
607
637
  response.set_header('Link', items.join(', '))
608
638
  end
609
639
 
610
640
  if vary.any?
611
- current_vary = (response.headers['Vary'] || "").split(',').map { |v| v.strip }.reject { |v| v.empty? }.sort
641
+ current_vary =
642
+ (response.headers['Vary'] || '')
643
+ .split(',')
644
+ .map(&:strip)
645
+ .reject(&:empty?)
646
+ .sort
612
647
  merged_vary = (vary.sort + current_vary).uniq
613
648
 
614
649
  response.set_header('Vary', merged_vary.join(', '))
@@ -619,7 +654,7 @@ module MediaTypes
619
654
  identifier: identifier,
620
655
  registrations: registrations,
621
656
  output: result,
622
- links: links,
657
+ links: links
623
658
  }
624
659
  wrapped = @serialization_wrapping_renderer.serialize input, '*/*', context: self
625
660
  render body: wrapped
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.6
4
+ version: 1.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld