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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/lib/media_types/serialization/version.rb +1 -1
- data/lib/media_types/serialization.rb +52 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 159a20aec1547f40a318c666acc8616e3b3396317b552f0a8cf7501fae609b5f
|
4
|
+
data.tar.gz: 485188d6f57ec55c684952fe9c00051ac1dbcbdd93dbc8136f26dd5e8ce351d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 932707eb3baa03f83ea24d7a70cdc3750b41122ab62446d5d4a1956d6eb1b13a2bec4e86b3ed4e2f4689608f2589bc40aae92add3e4a81a016fa7ef5684e944a
|
7
|
+
data.tar.gz: e083a0e056227b9fe46c0a380c37cf5af8cdd3a9835b216f6b5aa2248669987be6b4ad8ff533f855f064cad59bc052e60a8201cebf738389b8c462cc7e1ae732
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -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,
|
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,
|
324
|
+
def output_error(klazz, additional_serializers: [])
|
326
325
|
rescue_from klazz do |error|
|
327
326
|
problem = Problem.new(error)
|
328
|
-
|
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(
|
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
|
-
|
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(
|
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(
|
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 =
|
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
|
-
|
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
|
-
|
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.
|
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 =
|
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
|