iknow_view_models 3.14.4 → 3.15.1
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 +4 -4
- data/lib/iknow_view_models/version.rb +1 -1
- data/lib/view_model/active_record/collection_nested_controller.rb +1 -1
- data/lib/view_model/active_record/controller.rb +1 -33
- data/lib/view_model/active_record/controller_base.rb +0 -9
- data/lib/view_model/controller.rb +53 -7
- data/lib/view_model/deserialization_error.rb +30 -1
- data/lib/view_model/migratable_view.rb +2 -2
- data/lib/view_model/references.rb +8 -0
- data/lib/view_model/serialize_context.rb +1 -1
- data/test/unit/view_model/controller_test.rb +1 -0
- data/test/unit/view_model/deserialization_error/database_constraint_test.rb +37 -0
- data/test/unit/view_model/error_wrapping_test.rb +34 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c397b7573d9b99f8d3eff86fc5d5a1d5d3b884b0e094adc6693154e8d5556cf7
|
|
4
|
+
data.tar.gz: 900b604ddb7002b0ff4dc585ff18718014dce2f4c80cc6db710ef005692fd781
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fc257d7566cccfdc25abb2200df10d195a193473b3908290fdfa7a9594db9d1d2930c233f0ba1dc78f45022a19002fec2b2359a9f53c9d33ef5260cd5fdc2b6a
|
|
7
|
+
data.tar.gz: 544d64b10b9472af49691ad92279d2fee297a551b51057e757aaf67034896e8ef82a1a7fe7239d0f46e7d18214f393b115e8d3a0d0aa3c0693e4a283921741c3
|
|
@@ -76,7 +76,7 @@ module ViewModel::ActiveRecord::CollectionNestedController
|
|
|
76
76
|
owner_viewmodel.transaction do
|
|
77
77
|
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner)
|
|
78
78
|
owner_view.delete_associated(association_name, viewmodel_id, type: viewmodel_class, deserialize_context: deserialize_context)
|
|
79
|
-
render_viewmodel(nil)
|
|
79
|
+
render_viewmodel(nil, serialize_context: serialize_context)
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
require 'view_model/active_record/controller_base'
|
|
4
4
|
require 'view_model/active_record/collection_nested_controller'
|
|
5
5
|
require 'view_model/active_record/singular_nested_controller'
|
|
6
|
-
require 'view_model/controller/migration_versions'
|
|
7
6
|
|
|
8
7
|
# Controller for accessing an ViewModel::ActiveRecord
|
|
9
8
|
# Provides for the following routes:
|
|
@@ -17,7 +16,6 @@ module ViewModel::ActiveRecord::Controller
|
|
|
17
16
|
include ViewModel::ActiveRecord::ControllerBase
|
|
18
17
|
include ViewModel::ActiveRecord::CollectionNestedController
|
|
19
18
|
include ViewModel::ActiveRecord::SingularNestedController
|
|
20
|
-
include ViewModel::Controller::MigrationVersions
|
|
21
19
|
|
|
22
20
|
def show(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class))
|
|
23
21
|
view = nil
|
|
@@ -65,33 +63,7 @@ module ViewModel::ActiveRecord::Controller
|
|
|
65
63
|
view.destroy!(deserialize_context: deserialize_context)
|
|
66
64
|
end
|
|
67
65
|
end
|
|
68
|
-
render_viewmodel(nil)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
included do
|
|
72
|
-
etag { migrated_deep_schema_version }
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
def parse_viewmodel_updates
|
|
76
|
-
super.tap do |update_hash, refs|
|
|
77
|
-
if migration_versions.present?
|
|
78
|
-
migrator = ViewModel::UpMigrator.new(migration_versions)
|
|
79
|
-
migrator.migrate!({ 'data' => update_hash, 'references' => refs })
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def prerender_viewmodel(...)
|
|
85
|
-
super do |jbuilder|
|
|
86
|
-
yield(jbuilder) if block_given?
|
|
87
|
-
|
|
88
|
-
# migrate the resulting structure before it's serialized to a json string
|
|
89
|
-
if migration_versions.present?
|
|
90
|
-
tree = jbuilder.attributes!
|
|
91
|
-
migrator = ViewModel::DownMigrator.new(migration_versions)
|
|
92
|
-
migrator.migrate!(tree)
|
|
93
|
-
end
|
|
94
|
-
end
|
|
66
|
+
render_viewmodel(nil, serialize_context: serialize_context)
|
|
95
67
|
end
|
|
96
68
|
|
|
97
69
|
private
|
|
@@ -119,8 +91,4 @@ module ViewModel::ActiveRecord::Controller
|
|
|
119
91
|
def viewmodel_id
|
|
120
92
|
parse_param(:id, with: ViewmodelIdSerializer)
|
|
121
93
|
end
|
|
122
|
-
|
|
123
|
-
def migrated_deep_schema_version
|
|
124
|
-
ViewModel::Migrator.migrated_deep_schema_version(viewmodel_class, migration_versions, include_referenced: true)
|
|
125
|
-
end
|
|
126
94
|
end
|
|
@@ -9,15 +9,6 @@ module ViewModel::ActiveRecord::ControllerBase
|
|
|
9
9
|
|
|
10
10
|
protected
|
|
11
11
|
|
|
12
|
-
# Override (pre)render_viewmodel to use the default serialization context from this controller.
|
|
13
|
-
def render_viewmodel(viewmodel, serialize_context: new_serialize_context, **args)
|
|
14
|
-
super(viewmodel, serialize_context: serialize_context, **args)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def prerender_viewmodel(viewmodel, serialize_context: new_serialize_context, **args)
|
|
18
|
-
super(viewmodel, serialize_context: serialize_context, **args)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
12
|
def new_deserialize_context(viewmodel_class: self.viewmodel_class, access_control: self.access_control.new, **args)
|
|
22
13
|
viewmodel_class.new_deserialize_context(access_control: access_control, **args)
|
|
23
14
|
end
|
|
@@ -6,48 +6,86 @@ require 'oj'
|
|
|
6
6
|
module ViewModel::Controller
|
|
7
7
|
extend ActiveSupport::Concern
|
|
8
8
|
|
|
9
|
+
require 'view_model/controller/migration_versions'
|
|
10
|
+
include ViewModel::Controller::MigrationVersions
|
|
11
|
+
|
|
9
12
|
included do
|
|
10
13
|
rescue_from ViewModel::AbstractError, with: ->(ex) do
|
|
11
14
|
render_error(ex.view, ex.status)
|
|
12
15
|
end
|
|
16
|
+
|
|
17
|
+
etag { migrated_deep_schema_version }
|
|
13
18
|
end
|
|
14
19
|
|
|
15
|
-
def render_viewmodel(viewmodel, status: nil, serialize_context
|
|
20
|
+
def render_viewmodel(viewmodel, status: nil, serialize_context:, &block)
|
|
16
21
|
prerender = prerender_viewmodel(viewmodel, serialize_context: serialize_context, &block)
|
|
17
22
|
render_json_string(prerender, status: status)
|
|
18
23
|
end
|
|
19
24
|
|
|
20
25
|
# Render viewmodel(s) to a JSON API response as a String
|
|
21
|
-
def prerender_viewmodel(viewmodel, status: nil, serialize_context:
|
|
26
|
+
def prerender_viewmodel(viewmodel, status: nil, serialize_context:)
|
|
22
27
|
encode_jbuilder do |json|
|
|
23
28
|
json.data do
|
|
24
29
|
ViewModel.serialize(viewmodel, json, serialize_context: serialize_context)
|
|
25
30
|
end
|
|
26
31
|
|
|
32
|
+
yield(json) if block_given?
|
|
33
|
+
|
|
27
34
|
if serialize_context && serialize_context.has_references?
|
|
28
35
|
json.references do
|
|
29
36
|
serialize_context.serialize_references(json)
|
|
30
37
|
end
|
|
31
38
|
end
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
# migrate the resulting structure before it's serialized to a json string
|
|
41
|
+
if migration_versions.present?
|
|
42
|
+
tree = json.attributes!
|
|
43
|
+
migrator = ViewModel::DownMigrator.new(migration_versions)
|
|
44
|
+
migrator.migrate!(tree)
|
|
45
|
+
end
|
|
34
46
|
end
|
|
35
47
|
end
|
|
36
48
|
|
|
37
49
|
# Render an arbitrarily nested tree of hashes and arrays with pre-rendered
|
|
38
50
|
# JSON string terminals. Useful for rendering cached views without parsing
|
|
39
51
|
# then re-serializing the cached JSON.
|
|
40
|
-
def render_json_view(json_view, json_references: {}, status: nil, &block)
|
|
41
|
-
prerender = prerender_json_view(json_view, json_references: json_references, &block)
|
|
52
|
+
def render_json_view(json_view, json_references: {}, status: nil, serialize_context:, &block)
|
|
53
|
+
prerender = prerender_json_view(json_view, json_references: json_references, serialize_context: serialize_context, &block)
|
|
42
54
|
render_json_string(prerender, status: status)
|
|
43
55
|
end
|
|
44
56
|
|
|
45
|
-
def prerender_json_view(json_view, json_references: {})
|
|
57
|
+
def prerender_json_view(json_view, json_references: {}, serialize_context:)
|
|
46
58
|
json_view = wrap_json_view(json_view)
|
|
47
59
|
json_references = wrap_json_view(json_references)
|
|
48
60
|
|
|
49
61
|
encode_jbuilder do |json|
|
|
50
62
|
json.data json_view
|
|
63
|
+
|
|
64
|
+
if block_given?
|
|
65
|
+
yield(json)
|
|
66
|
+
|
|
67
|
+
# The block can contribute attributes directly to the jbuilder and
|
|
68
|
+
# references via the serialize context. If it did, we need to serialize,
|
|
69
|
+
# migrate, and merge them.
|
|
70
|
+
block_attributes = json.attributes!.except('data')
|
|
71
|
+
|
|
72
|
+
if serialize_context && serialize_context.has_references?
|
|
73
|
+
block_references = serialize_context.serialize_references_to_hash
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
has_migrations = defined?(migration_versions) && migration_versions.present?
|
|
77
|
+
|
|
78
|
+
if has_migrations && (block_attributes.present? || block_references.present?)
|
|
79
|
+
migrator = ViewModel::DownMigrator.new(migration_versions)
|
|
80
|
+
tree = block_attributes.merge({ 'references' => block_references })
|
|
81
|
+
migrator.migrate!(tree)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
if block_references.present?
|
|
85
|
+
json_references = json_references.reverse_merge(block_references)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
51
89
|
if json_references.present?
|
|
52
90
|
json.references do
|
|
53
91
|
json_references.sort.each do |key, value|
|
|
@@ -55,7 +93,6 @@ module ViewModel::Controller
|
|
|
55
93
|
end
|
|
56
94
|
end
|
|
57
95
|
end
|
|
58
|
-
yield(json) if block_given?
|
|
59
96
|
end
|
|
60
97
|
end
|
|
61
98
|
|
|
@@ -83,6 +120,11 @@ module ViewModel::Controller
|
|
|
83
120
|
update_hash = _extract_update_data(data_param)
|
|
84
121
|
refs = _extract_param_hash(refs_param)
|
|
85
122
|
|
|
123
|
+
if migration_versions.present?
|
|
124
|
+
migrator = ViewModel::UpMigrator.new(migration_versions)
|
|
125
|
+
migrator.migrate!({ 'data' => update_hash, 'references' => refs })
|
|
126
|
+
end
|
|
127
|
+
|
|
86
128
|
return update_hash, refs
|
|
87
129
|
end
|
|
88
130
|
|
|
@@ -128,6 +170,10 @@ module ViewModel::Controller
|
|
|
128
170
|
end
|
|
129
171
|
end
|
|
130
172
|
|
|
173
|
+
def migrated_deep_schema_version
|
|
174
|
+
ViewModel::Migrator.migrated_deep_schema_version(viewmodel_class, migration_versions, include_referenced: true)
|
|
175
|
+
end
|
|
176
|
+
|
|
131
177
|
def encode_jbuilder
|
|
132
178
|
builder = Jbuilder.new do |json|
|
|
133
179
|
yield json
|
|
@@ -377,7 +377,36 @@ class ViewModel
|
|
|
377
377
|
when PG::UniqueViolation, PG::ExclusionViolation
|
|
378
378
|
UniqueViolation.from_postgres_error(exception.cause, nodes)
|
|
379
379
|
else
|
|
380
|
-
self.new(exception
|
|
380
|
+
self.new(redact_failing_row(exception), nodes)
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
class << self
|
|
385
|
+
FAILING_ROW_PREFIX = 'Failing row contains '
|
|
386
|
+
REDACTED_FAILING_ROW = 'Failing row contains (redacted).'
|
|
387
|
+
|
|
388
|
+
private
|
|
389
|
+
|
|
390
|
+
# Postgres reports CHECK and NOT NULL violations with "Failing row
|
|
391
|
+
# contains (...)" including every column of the row being written.
|
|
392
|
+
# Redact them.
|
|
393
|
+
def redact_failing_row(exception)
|
|
394
|
+
message = exception.message
|
|
395
|
+
|
|
396
|
+
pg_error = exception.cause
|
|
397
|
+
row_detail = pg_error.result&.error_field(PG::PG_DIAG_MESSAGE_DETAIL) if pg_error.is_a?(PG::Error)
|
|
398
|
+
|
|
399
|
+
if row_detail&.start_with?(FAILING_ROW_PREFIX) && message.include?(row_detail)
|
|
400
|
+
# With a structured error, replace the exact detail reported by
|
|
401
|
+
# Postgres rather than parsing the message.
|
|
402
|
+
message.sub(row_detail) { REDACTED_FAILING_ROW }
|
|
403
|
+
elsif (index = message.index(FAILING_ROW_PREFIX))
|
|
404
|
+
# Without a structured error we can't find the end of the message,
|
|
405
|
+
# so drop everything after it.
|
|
406
|
+
message[0, index] + REDACTED_FAILING_ROW
|
|
407
|
+
else
|
|
408
|
+
message
|
|
409
|
+
end
|
|
381
410
|
end
|
|
382
411
|
end
|
|
383
412
|
end
|
|
@@ -10,9 +10,9 @@ module ViewModel::MigratableView
|
|
|
10
10
|
extend ActiveSupport::Concern
|
|
11
11
|
|
|
12
12
|
class_methods do
|
|
13
|
-
def inherited(
|
|
13
|
+
def inherited(subclass)
|
|
14
14
|
super
|
|
15
|
-
|
|
15
|
+
subclass.initialize_as_migratable_view
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def initialize_as_migratable_view
|
|
@@ -30,6 +30,14 @@ class ViewModel
|
|
|
30
30
|
ref
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
def add_preserialized_reference(ref, literal_value)
|
|
34
|
+
return ref if @value_by_ref.has_key?(ref)
|
|
35
|
+
|
|
36
|
+
@ref_by_value[literal_value] = ref
|
|
37
|
+
@value_by_ref[ref] = literal_value
|
|
38
|
+
ref
|
|
39
|
+
end
|
|
40
|
+
|
|
33
41
|
def clear!
|
|
34
42
|
@ref_by_value.clear
|
|
35
43
|
@value_by_ref.clear
|
|
@@ -19,7 +19,7 @@ class ViewModel::SerializeContext < ViewModel::TraversalContext
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
delegate :references, :flatten_references, to: :shared_context
|
|
22
|
-
delegate :add_reference, :has_references?, to: :references
|
|
22
|
+
delegate :add_reference, :add_preserialized_reference, :has_references?, to: :references
|
|
23
23
|
|
|
24
24
|
# Return viewmodels referenced during serialization and clear @references.
|
|
25
25
|
def extract_referenced_views!
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../../../test_helper'
|
|
4
|
+
|
|
5
|
+
require 'pg'
|
|
6
|
+
require 'view_model'
|
|
7
|
+
require 'view_model/deserialization_error'
|
|
8
|
+
|
|
9
|
+
class ViewModel::DeserializationError::DatabaseConstraintTest < ActiveSupport::TestCase
|
|
10
|
+
extend Minitest::Spec::DSL
|
|
11
|
+
|
|
12
|
+
describe 'from_exception' do
|
|
13
|
+
let(:error) { ViewModel::DeserializationError::DatabaseConstraint.from_exception(exception) }
|
|
14
|
+
|
|
15
|
+
describe 'without a Postgres error' do
|
|
16
|
+
let(:exception) do
|
|
17
|
+
ActiveRecord::StatementInvalid.new(
|
|
18
|
+
%(PG::CheckViolation: ERROR: new row for relation "t" violates check constraint "c"\nDETAIL: Failing row contains (1, SECRET).\n))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'redacts everything from the failing row' do
|
|
22
|
+
expect(error.detail).to eq(
|
|
23
|
+
%(PG::CheckViolation: ERROR: new row for relation "t" violates check constraint "c"\nDETAIL: Failing row contains (redacted).))
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe 'without a failing row' do
|
|
28
|
+
let(:exception) do
|
|
29
|
+
ActiveRecord::StatementInvalid.new(%(PG::ForeignKeyViolation: ERROR: insert or update on table "t" violates foreign key constraint "fk"))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'retains the message' do
|
|
33
|
+
expect(error.detail).to eq(exception.message)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -101,6 +101,40 @@ class ViewModel::ErrorWrappingTest < ActiveSupport::TestCase
|
|
|
101
101
|
|
|
102
102
|
expect(ex.message).to match(/violates check constraint/)
|
|
103
103
|
end
|
|
104
|
+
|
|
105
|
+
it 'redacts the failing row' do
|
|
106
|
+
ex = assert_raises(ViewModel::DeserializationError::DatabaseConstraint) do
|
|
107
|
+
subject.wrap_active_record_errors(blame) do
|
|
108
|
+
model_class.create!(name: "SECRET).\nVALUE")
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
expect(ex.detail).to match(/violates check constraint/)
|
|
113
|
+
expect(ex.detail).to include('Failing row contains (redacted).')
|
|
114
|
+
expect(ex.detail).not_to include('SECRET')
|
|
115
|
+
expect(ex.detail).not_to include('VALUE')
|
|
116
|
+
expect(ex.nodes).to eq([blame])
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
describe 'not-null constraint' do
|
|
121
|
+
def model_attributes
|
|
122
|
+
super.merge(
|
|
123
|
+
schema: ->(t) { t.string :required, null: false },
|
|
124
|
+
)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it 'redacts the failing row' do
|
|
128
|
+
ex = assert_raises(ViewModel::DeserializationError::DatabaseConstraint) do
|
|
129
|
+
subject.wrap_active_record_errors(blame) do
|
|
130
|
+
model_class.create!(name: 'SECRET')
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
expect(ex.detail).to match(/null value in column "required" .* violates not-null constraint/)
|
|
135
|
+
expect(ex.detail).to include('Failing row contains (redacted).')
|
|
136
|
+
expect(ex.detail).not_to include('SECRET')
|
|
137
|
+
end
|
|
104
138
|
end
|
|
105
139
|
|
|
106
140
|
describe 'unique constraint' do
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: iknow_view_models
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.15.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- iKnow Team
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: actionpack
|
|
@@ -529,6 +529,7 @@ files:
|
|
|
529
529
|
- test/unit/view_model/active_record_test.rb
|
|
530
530
|
- test/unit/view_model/callbacks_test.rb
|
|
531
531
|
- test/unit/view_model/controller_test.rb
|
|
532
|
+
- test/unit/view_model/deserialization_error/database_constraint_test.rb
|
|
532
533
|
- test/unit/view_model/deserialization_error/unique_violation_test.rb
|
|
533
534
|
- test/unit/view_model/error_wrapping_test.rb
|
|
534
535
|
- test/unit/view_model/garbage_collection_test.rb
|
|
@@ -593,6 +594,7 @@ test_files:
|
|
|
593
594
|
- test/unit/view_model/active_record_test.rb
|
|
594
595
|
- test/unit/view_model/callbacks_test.rb
|
|
595
596
|
- test/unit/view_model/controller_test.rb
|
|
597
|
+
- test/unit/view_model/deserialization_error/database_constraint_test.rb
|
|
596
598
|
- test/unit/view_model/deserialization_error/unique_violation_test.rb
|
|
597
599
|
- test/unit/view_model/error_wrapping_test.rb
|
|
598
600
|
- test/unit/view_model/garbage_collection_test.rb
|