solidus_api 2.9.5 → 2.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -2
  3. data/app/controllers/spree/api/addresses_controller.rb +1 -1
  4. data/app/controllers/spree/api/base_controller.rb +10 -10
  5. data/app/controllers/spree/api/checkouts_controller.rb +21 -8
  6. data/app/controllers/spree/api/credit_cards_controller.rb +1 -1
  7. data/app/controllers/spree/api/images_controller.rb +1 -1
  8. data/app/controllers/spree/api/inventory_units_controller.rb +1 -1
  9. data/app/controllers/spree/api/option_types_controller.rb +1 -1
  10. data/app/controllers/spree/api/option_values_controller.rb +1 -1
  11. data/app/controllers/spree/api/orders_controller.rb +17 -6
  12. data/app/controllers/spree/api/payments_controller.rb +1 -1
  13. data/app/controllers/spree/api/product_properties_controller.rb +1 -1
  14. data/app/controllers/spree/api/properties_controller.rb +1 -1
  15. data/app/controllers/spree/api/resource_controller.rb +1 -1
  16. data/app/controllers/spree/api/return_authorizations_controller.rb +1 -1
  17. data/app/controllers/spree/api/shipments_controller.rb +1 -1
  18. data/app/controllers/spree/api/stock_items_controller.rb +1 -1
  19. data/app/controllers/spree/api/stock_locations_controller.rb +1 -1
  20. data/app/controllers/spree/api/stores_controller.rb +1 -1
  21. data/app/controllers/spree/api/taxonomies_controller.rb +4 -2
  22. data/app/controllers/spree/api/taxons_controller.rb +1 -1
  23. data/app/controllers/spree/api/variants_controller.rb +1 -1
  24. data/app/controllers/spree/api/zones_controller.rb +1 -1
  25. data/app/helpers/spree/api/api_helpers.rb +2 -2
  26. data/app/views/spree/api/images/_image.json.jbuilder +2 -2
  27. data/app/views/spree/api/orders/_order.json.jbuilder +1 -1
  28. data/app/views/spree/api/shared/_pagination.json.jbuilder +1 -1
  29. data/lib/spree/api.rb +0 -7
  30. data/lib/spree/api/config.rb +9 -0
  31. data/lib/spree/api/engine.rb +4 -4
  32. data/lib/spree/api/responders.rb +3 -2
  33. data/lib/spree/api/responders/{rabl_template.rb → jbuilder_template.rb} +3 -1
  34. data/{app/models → lib}/spree/api_configuration.rb +0 -0
  35. data/openapi/.stoplight.yml +7 -0
  36. data/openapi/api.oas2.yml +6108 -0
  37. data/openapi/authentication.md +25 -0
  38. data/openapi/checkout-flow.md +50 -0
  39. data/openapi/errors.md +3 -0
  40. data/openapi/lint.yml +1 -0
  41. data/openapi/main.hub.yml +65 -0
  42. data/openapi/pagination.md +7 -0
  43. data/openapi/theme.css +0 -0
  44. data/solidus_api.gemspec +1 -1
  45. data/spec/controllers/spree/api/resource_controller_spec.rb +4 -4
  46. data/spec/lib/spree_api_responders_spec.rb +10 -0
  47. data/spec/requests/api/address_books_spec.rb +2 -2
  48. data/spec/requests/{rabl_cache_spec.rb → jbuilder_cache_spec.rb} +2 -2
  49. data/spec/requests/spree/api/checkouts_controller_spec.rb +35 -7
  50. data/spec/requests/spree/api/classifications_controller_spec.rb +1 -1
  51. data/spec/requests/spree/api/option_types_controller_spec.rb +2 -2
  52. data/spec/requests/spree/api/option_values_controller_spec.rb +2 -2
  53. data/spec/requests/spree/api/orders_controller_spec.rb +38 -15
  54. data/spec/requests/spree/api/payments_controller_spec.rb +3 -3
  55. data/spec/requests/spree/api/products_controller_spec.rb +6 -6
  56. data/spec/requests/spree/api/stock_items_controller_spec.rb +5 -5
  57. data/spec/requests/spree/api/stock_locations_controller_spec.rb +4 -4
  58. data/spec/requests/spree/api/taxons_controller_spec.rb +2 -2
  59. metadata +21 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ead99f957c13a661698c4f368979a94f1068be778550779d368dd0859698894
4
- data.tar.gz: 63f6e8abe0a910222163a7c3afcb9f9b55ee00b15c9b23205a35b7ca14efe067
3
+ metadata.gz: b7afcbef6dfdc19f02d26c7b3e410f072dc0d6be5a5b7d476b64b3725b1b24b7
4
+ data.tar.gz: ac898e6b9d5df7e526a5cd9af63a240aed5e2ab46c7ac75ca583b0fb7eb599b1
5
5
  SHA512:
6
- metadata.gz: 3b0b734a3d20c74295e479717df705bd2212e8bbb323a4bbbb7965187c72419f458f8e2763e1a39909c629c4e395015836d80cbf33d8ba9556362a069dee0ffb
7
- data.tar.gz: 6bf3a472fee1cfbcbff63977120e48a75b0b979c53b62702331bcd7bb7bf41be1b064db5dda40c8b9edb6854db7ad6e81f3882dd9c248e4f595b2eaec1d7cd89
6
+ metadata.gz: ce175516b04998e778b53e3d1ae47fc1a04c2ef4146a68000bc8745d5e267e423054d0e96fb1b02a03fa26ac186210012b84b54fa7592d15df7cf4fcf8e8d39a
7
+ data.tar.gz: 197562fcd0ea03c6593c21bfb8cb6f8d2424c6ddc3b12cbbc5b83a501976fe724c448a341de063a630daa3cb0f1309687503f5f35ee27536ad4d0a715392c3ea
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # solidus\_api
1
+ # solidus_api
2
2
 
3
- API contains the controllers and rabl views implementing the REST API of Solidus.
3
+ API contains the controllers and Jbuilder views implementing the REST API of
4
+ Solidus.
4
5
 
5
6
  ## Testing
6
7
 
@@ -9,3 +10,35 @@ Run the API tests:
9
10
  ```bash
10
11
  bundle exec rspec
11
12
  ```
13
+
14
+ ## Documentation
15
+
16
+ The API documentation is in the [openapi][docs-dir] directory. It follows the
17
+ OpenAPI specification and it is hosted on [Stoplight Docs][live-docs].
18
+
19
+ If you want to contribute, you can use [Stoplight Studio][studio]. Simply
20
+ follow these steps:
21
+
22
+ 1. Create a new Stoplight Studio project
23
+ 2. Copy-paste the content of `openapi/api.oas2.yml` into your project
24
+ 3. Edit the endpoints and models as needed
25
+ 4. Copy-paste the result back into `openapi/api.oas2.yml`
26
+ 5. Open a PR!
27
+
28
+ **Note: Only use embedded models in Stoplight Studio, as Stoplight Docs is
29
+ not compatible with externally-defined models!**
30
+
31
+ CircleCI automatically syncs our Git repo with Stoplight Docs when a PR is
32
+ merged, and automatically publishes a new version on Docs when a new Solidus
33
+ version is released.
34
+
35
+ ## Related projects
36
+
37
+ - [solidus-sdk](https://gitlab.com/deseretbook/packages/solidus-sdk): created
38
+ by Joel Saupe at [Deseret Book](https://deseretbook.com/), this is a JS SDK
39
+ that allows you to use the Solidus API. It even supports plug-ins, so you can
40
+ easily extend it with the endpoints provided by your Solidus extensions!
41
+
42
+ [docs-dir]: https://github.com/solidusio/solidus/tree/master/api/openapi
43
+ [live-docs]: https://solidus.docs.stoplight.io
44
+ [studio]: https://stoplight.io/p/studio
@@ -15,7 +15,7 @@ module Spree
15
15
  authorize! :update, @order, order_token
16
16
  find_address
17
17
 
18
- if @order.update_attributes({ "#{@order_source}_attributes" => address_params })
18
+ if @order.update({ "#{@order_source}_attributes" => address_params })
19
19
  @address = @order.send(@order_source)
20
20
  respond_with(@address, default_template: :show)
21
21
  else
@@ -49,9 +49,9 @@ module Spree
49
49
  def authenticate_user
50
50
  unless @current_api_user
51
51
  if requires_authentication? && api_key.blank? && order_token.blank?
52
- render "spree/api/errors/must_specify_api_key", status: 401
52
+ render "spree/api/errors/must_specify_api_key", status: :unauthorized
53
53
  elsif order_token.blank? && (requires_authentication? || api_key.present?)
54
- render "spree/api/errors/invalid_api_key", status: 401
54
+ render "spree/api/errors/invalid_api_key", status: :unauthorized
55
55
  end
56
56
  end
57
57
  end
@@ -65,7 +65,7 @@ module Spree
65
65
  end
66
66
 
67
67
  def unauthorized
68
- render "spree/api/errors/unauthorized", status: 401
68
+ render "spree/api/errors/unauthorized", status: :unauthorized
69
69
  end
70
70
 
71
71
  def gateway_error(exception)
@@ -78,7 +78,7 @@ module Spree
78
78
  exception: exception.message,
79
79
  error: exception.message,
80
80
  missing_param: exception.param
81
- }, status: 422
81
+ }, status: :unprocessable_entity
82
82
  end
83
83
 
84
84
  def requires_authentication?
@@ -86,7 +86,7 @@ module Spree
86
86
  end
87
87
 
88
88
  def not_found
89
- render "spree/api/errors/not_found", status: 404
89
+ render "spree/api/errors/not_found", status: :not_found
90
90
  end
91
91
 
92
92
  def current_ability
@@ -96,7 +96,7 @@ module Spree
96
96
  def invalid_resource!(resource)
97
97
  Rails.logger.error "invalid_resouce_errors=#{resource.errors.full_messages}"
98
98
  @resource = resource
99
- render "spree/api/errors/invalid_resource", status: 422
99
+ render "spree/api/errors/invalid_resource", status: :unprocessable_entity
100
100
  end
101
101
 
102
102
  def api_key
@@ -112,7 +112,7 @@ module Spree
112
112
 
113
113
  def spree_token
114
114
  token = request.headers["X-Spree-Token"]
115
- return unless token.present?
115
+ return if token.blank?
116
116
 
117
117
  Spree::Deprecation.warn(
118
118
  'The custom X-Spree-Token request header is deprecated and will be removed in the next release.' \
@@ -164,8 +164,8 @@ module Spree
164
164
 
165
165
  def lock_order
166
166
  OrderMutex.with_lock!(@order) { yield }
167
- rescue Spree::OrderMutex::LockFailed => e
168
- render plain: e.message, status: 409
167
+ rescue Spree::OrderMutex::LockFailed => error
168
+ render plain: error.message, status: :conflict
169
169
  end
170
170
 
171
171
  def insufficient_stock_error(exception)
@@ -175,7 +175,7 @@ module Spree
175
175
  errors: [I18n.t(:quantity_is_not_available, scope: "spree.api.order")],
176
176
  type: 'insufficient_stock'
177
177
  },
178
- status: 422
178
+ status: :unprocessable_entity
179
179
  )
180
180
  end
181
181
 
@@ -23,8 +23,8 @@ module Spree
23
23
  authorize! :update, @order, order_token
24
24
  @order.next!
25
25
  respond_with(@order, default_template: 'spree/api/orders/show', status: 200)
26
- rescue StateMachines::InvalidTransition => e
27
- logger.error("invalid_transition #{e.event} from #{e.from} for #{e.object.class.name}. Error: #{e.inspect}")
26
+ rescue StateMachines::InvalidTransition => error
27
+ logger.error("invalid_transition #{error.event} from #{error.from} for #{error.object.class.name}. Error: #{error.inspect}")
28
28
  respond_with(@order, default_template: 'spree/api/orders/could_not_transition', status: 422)
29
29
  end
30
30
 
@@ -42,8 +42,8 @@ module Spree
42
42
  @order.complete!
43
43
  respond_with(@order, default_template: 'spree/api/orders/show', status: 200)
44
44
  end
45
- rescue StateMachines::InvalidTransition => e
46
- logger.error("invalid_transition #{e.event} from #{e.from} for #{e.object.class.name}. Error: #{e.inspect}")
45
+ rescue StateMachines::InvalidTransition => error
46
+ logger.error("invalid_transition #{error.event} from #{error.from} for #{error.object.class.name}. Error: #{error.inspect}")
47
47
  respond_with(@order, default_template: 'spree/api/orders/could_not_transition', status: 422)
48
48
  end
49
49
 
@@ -76,11 +76,24 @@ module Spree
76
76
  end
77
77
 
78
78
  def update_params
79
- if update_params = massaged_params[:order]
80
- update_params.permit(permitted_checkout_attributes)
79
+ state = @order.state
80
+ case state.to_sym
81
+ when :cart, :address
82
+ massaged_params.fetch(:order, {}).permit(
83
+ permitted_checkout_address_attributes
84
+ )
85
+ when :delivery
86
+ massaged_params.require(:order).permit(
87
+ permitted_checkout_delivery_attributes
88
+ )
89
+ when :payment
90
+ massaged_params.require(:order).permit(
91
+ permitted_checkout_payment_attributes
92
+ )
81
93
  else
82
- # We current allow update requests without any parameters in them.
83
- {}
94
+ massaged_params.fetch(:order, {}).permit(
95
+ permitted_checkout_confirm_attributes
96
+ )
84
97
  end
85
98
  end
86
99
 
@@ -18,7 +18,7 @@ module Spree
18
18
  end
19
19
 
20
20
  def update
21
- if @credit_card.update_attributes(credit_card_update_params)
21
+ if @credit_card.update(credit_card_update_params)
22
22
  respond_with(@credit_card, default_template: :show)
23
23
  else
24
24
  invalid_resource!(@credit_card)
@@ -21,7 +21,7 @@ module Spree
21
21
 
22
22
  def update
23
23
  @image = Spree::Image.accessible_by(current_ability, :update).find(params[:id])
24
- @image.update_attributes(image_params)
24
+ @image.update(image_params)
25
25
  respond_with(@image, default_template: :show)
26
26
  end
27
27
 
@@ -14,7 +14,7 @@ module Spree
14
14
  authorize! :update, inventory_unit.order
15
15
 
16
16
  inventory_unit.transaction do
17
- if inventory_unit.update_attributes(inventory_unit_params)
17
+ if inventory_unit.update(inventory_unit_params)
18
18
  fire
19
19
  render :show, status: 200
20
20
  else
@@ -29,7 +29,7 @@ module Spree
29
29
 
30
30
  def update
31
31
  @option_type = Spree::OptionType.accessible_by(current_ability, :update).find(params[:id])
32
- if @option_type.update_attributes(option_type_params)
32
+ if @option_type.update(option_type_params)
33
33
  render :show
34
34
  else
35
35
  invalid_resource!(@option_type)
@@ -29,7 +29,7 @@ module Spree
29
29
 
30
30
  def update
31
31
  @option_value = scope.accessible_by(current_ability, :update).find(params[:id])
32
- if @option_value.update_attributes(option_value_params)
32
+ if @option_value.update(option_value_params)
33
33
  render :show
34
34
  else
35
35
  invalid_resource!(@option_value)
@@ -55,10 +55,9 @@ module Spree
55
55
  def index
56
56
  authorize! :index, Order
57
57
  orders_includes = [
58
- :user,
59
- :payments,
60
- :adjustments,
61
- :line_items
58
+ { user: :store_credits },
59
+ :line_items,
60
+ :valid_store_credit_payments
62
61
  ]
63
62
  @orders = paginate(
64
63
  Spree::Order
@@ -131,7 +130,13 @@ module Spree
131
130
  end
132
131
 
133
132
  def normalize_params
134
- params[:order][:payments_attributes] = params[:order].delete(:payments) if params[:order][:payments]
133
+ if params[:order][:payments]
134
+ payments_params = params[:order].delete(:payments)
135
+ params[:order][:payments_attributes] = payments_params.map do |payment_params|
136
+ payment_params[:source_attributes] = payment_params.delete(:source) if payment_params[:source].present?
137
+ payment_params
138
+ end
139
+ end
135
140
  params[:order][:shipments_attributes] = params[:order].delete(:shipments) if params[:order][:shipments]
136
141
  params[:order][:line_items_attributes] = params[:order].delete(:line_items) if params[:order][:line_items]
137
142
  params[:order][:ship_address_attributes] = params[:order].delete(:ship_address) if params[:order][:ship_address].present?
@@ -168,7 +173,13 @@ module Spree
168
173
  end
169
174
 
170
175
  def find_order(_lock = false)
171
- @order = Spree::Order.find_by!(number: params[:id])
176
+ @order = Spree::Order.
177
+ includes(line_items: [:adjustments, { variant: :images }],
178
+ payments: :payment_method,
179
+ shipments: {
180
+ shipping_rates: { shipping_method: :zones, taxes: :tax_rate }
181
+ }).
182
+ find_by!(number: params[:id])
172
183
  end
173
184
 
174
185
  def order_id
@@ -31,7 +31,7 @@ module Spree
31
31
  authorize! params[:action], @payment
32
32
  if !@payment.pending?
33
33
  render 'update_forbidden', status: 403
34
- elsif @payment.update_attributes(payment_params)
34
+ elsif @payment.update(payment_params)
35
35
  respond_with(@payment, default_template: :show)
36
36
  else
37
37
  invalid_resource!(@payment)
@@ -38,7 +38,7 @@ module Spree
38
38
  def update
39
39
  if @product_property
40
40
  authorize! :update, @product_property
41
- @product_property.update_attributes(product_property_params)
41
+ @product_property.update(product_property_params)
42
42
  respond_with(@product_property, status: 200, default_template: :show)
43
43
  else
44
44
  invalid_resource!(@product_property)
@@ -39,7 +39,7 @@ module Spree
39
39
  def update
40
40
  if @property
41
41
  authorize! :update, @property
42
- @property.update_attributes(property_params)
42
+ @property.update(property_params)
43
43
  respond_with(@property, status: 200, default_template: :show)
44
44
  else
45
45
  invalid_resource!(@property)
@@ -43,7 +43,7 @@ class Spree::Api::ResourceController < Spree::Api::BaseController
43
43
  def update
44
44
  authorize! :update, @object
45
45
 
46
- if @object.update_attributes(permitted_resource_params)
46
+ if @object.update(permitted_resource_params)
47
47
  respond_with(@object, status: 200, default_template: :show)
48
48
  else
49
49
  invalid_resource!(@object)
@@ -50,7 +50,7 @@ module Spree
50
50
 
51
51
  def update
52
52
  @return_authorization = @order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
53
- if @return_authorization.update_attributes(return_authorization_params)
53
+ if @return_authorization.update(return_authorization_params)
54
54
  respond_with(@return_authorization, default_template: :show)
55
55
  else
56
56
  invalid_resource!(@return_authorization)
@@ -146,7 +146,7 @@ module Spree
146
146
  end
147
147
 
148
148
  def update_shipment
149
- @shipment.update_attributes(shipment_params)
149
+ @shipment.update(shipment_params)
150
150
  @shipment.reload
151
151
  end
152
152
 
@@ -40,7 +40,7 @@ module Spree
40
40
  adjustment -= @stock_item.count_on_hand if params[:stock_item][:force]
41
41
 
42
42
  Spree::StockItem.transaction do
43
- if @stock_item.update_attributes(stock_item_params)
43
+ if @stock_item.update(stock_item_params)
44
44
  adjust_stock_item_count_on_hand(adjustment)
45
45
  respond_with(@stock_item, status: 200, default_template: :show)
46
46
  else
@@ -33,7 +33,7 @@ module Spree
33
33
 
34
34
  def update
35
35
  authorize! :update, stock_location
36
- if stock_location.update_attributes(stock_location_params)
36
+ if stock_location.update(stock_location_params)
37
37
  respond_with(stock_location, status: 200, default_template: :show)
38
38
  else
39
39
  invalid_resource!(stock_location)
@@ -24,7 +24,7 @@ module Spree
24
24
 
25
25
  def update
26
26
  authorize! :update, @store
27
- if @store.update_attributes(store_params)
27
+ if @store.update(store_params)
28
28
  respond_with(@store, status: 200, default_template: :show)
29
29
  else
30
30
  invalid_resource!(@store)
@@ -33,7 +33,7 @@ module Spree
33
33
 
34
34
  def update
35
35
  authorize! :update, taxonomy
36
- if taxonomy.update_attributes(taxonomy_params)
36
+ if taxonomy.update(taxonomy_params)
37
37
  respond_with(taxonomy, status: 200, default_template: :show)
38
38
  else
39
39
  invalid_resource!(taxonomy)
@@ -58,7 +58,9 @@ module Spree
58
58
  end
59
59
 
60
60
  def taxonomy
61
- @taxonomy ||= Spree::Taxonomy.accessible_by(current_ability, :read).find(params[:id])
61
+ @taxonomy ||= Spree::Taxonomy.accessible_by(current_ability, :read).
62
+ includes(root: :children).
63
+ find(params[:id])
62
64
  end
63
65
 
64
66
  def taxonomy_params
@@ -56,7 +56,7 @@ module Spree
56
56
 
57
57
  def update
58
58
  authorize! :update, taxon
59
- if taxon.update_attributes(taxon_params)
59
+ if taxon.update(taxon_params)
60
60
  respond_with(taxon, status: 200, default_template: :show)
61
61
  else
62
62
  invalid_resource!(taxon)
@@ -43,7 +43,7 @@ module Spree
43
43
 
44
44
  def update
45
45
  @variant = scope.accessible_by(current_ability, :update).find(params[:id])
46
- if @variant.update_attributes(variant_params)
46
+ if @variant.update(variant_params)
47
47
  respond_with(@variant, status: 200, default_template: :show)
48
48
  else
49
49
  invalid_resource!(@product)
@@ -37,7 +37,7 @@ module Spree
37
37
 
38
38
  def update
39
39
  authorize! :update, zone
40
- if zone.update_attributes(zone_params)
40
+ if zone.update(zone_params)
41
41
  respond_with(zone, status: 200, default_template: :show)
42
42
  else
43
43
  invalid_resource!(zone)
@@ -40,7 +40,7 @@ module Spree
40
40
 
41
41
  def required_fields_for(model)
42
42
  required_fields = model._validators.select do |_field, validations|
43
- validations.any? { |v| v.is_a?(ActiveModel::Validations::PresenceValidator) }
43
+ validations.any? { |validation| validation.is_a?(ActiveModel::Validations::PresenceValidator) }
44
44
  end.map(&:first) # get fields that are invalid
45
45
  # Permalinks presence is validated, but are really automatically generated
46
46
  # Therefore we shouldn't tell API clients that they MUST send one through
@@ -177,7 +177,7 @@ module Spree
177
177
  ]
178
178
 
179
179
  def variant_attributes
180
- if @current_user_roles && @current_user_roles.include?("admin")
180
+ if @current_user_roles&.include?("admin")
181
181
  @@variant_attributes + [:cost_price]
182
182
  else
183
183
  @@variant_attributes
@@ -2,6 +2,6 @@
2
2
 
3
3
  json.(image, *image_attributes)
4
4
  json.(image, :viewable_type, :viewable_id)
5
- Spree::Image.attachment_definitions[:attachment][:styles].each do |k, _v|
6
- json.set! "#{k}_url", image.attachment.url(k)
5
+ Spree::Image.attachment_definitions[:attachment][:styles].each do |key, _value|
6
+ json.set! "#{key}_url", image.attachment.url(key)
7
7
  end