Skip to content

Commit

Permalink
Remove deprecated api variants routes
Browse files Browse the repository at this point in the history
We want to use the shallow version of routes when possible:

- PATCH api/variants/:id
- DELETE api/variants/:id
- GET api/variants/:id

And the nested route when the option type is mandatory:

- POST api/products/{product_id}/variants

For the index routes instead, we will have both:

- GET api/products/{product_id}/variants
- GET api/variants
  • Loading branch information
kennyadsl committed Apr 18, 2023
1 parent 571657f commit cef126d
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 225 deletions.
20 changes: 0 additions & 20 deletions api/app/controllers/spree/api/variants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ class VariantsController < Spree::Api::BaseController
before_action :product

def create
Spree::Deprecation.warn <<~MSG unless request.path.include?('/products/')
This route is deprecated. Use the route nested within the product resource:
POST api/products/{product_id}/variants
MSG

authorize! :create, Variant
@variant = scope.new(variant_params)
if @variant.save
Expand All @@ -22,8 +16,6 @@ def create
end

def destroy
warn_if_nested_member_route

@variant = scope.accessible_by(current_ability, :destroy).find(params[:id])
@variant.discard
respond_with(@variant, status: 204)
Expand All @@ -50,16 +42,12 @@ def new
end

def show
warn_if_nested_member_route

@variant = scope.includes(include_list)
.find(params[:id])
respond_with(@variant)
end

def update
warn_if_nested_member_route

@variant = scope.accessible_by(current_ability, :update).find(params[:id])
if @variant.update(variant_params)
respond_with(@variant, status: 200, default_template: :show)
Expand All @@ -70,14 +58,6 @@ def update

private

def warn_if_nested_member_route
Spree::Deprecation.warn <<~MSG if request.path.include?('/products/')
This route is deprecated. Use shallow version instead:
#{request.method.upcase} api/variants/:id
MSG
end

def product
@product ||= Spree::Product.accessible_by(current_ability, :show).friendly.find(params[:product_id]) if params[:product_id]
end
Expand Down
174 changes: 0 additions & 174 deletions api/openapi/solidus-api.oas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -667,121 +667,6 @@ paths:
properties:
image:
$ref: '#/components/schemas/image-input'
'/products/{product_id}/variants/{id}':
get:
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/variant'
'401':
$ref: '#/components/responses/invalid-api-key'
'404':
$ref: '#/components/responses/not-found'
summary: Get product variant
description: |-
**Deprecation Warning**: Use [shallow version](/docs/solidus/87df124706c5f-get-variant) instead
Retrieves a product's variant.
operationId: get-product-variant
tags:
- Variants
security:
- api-key: []
parameters:
- name: product_id
in: path
required: true
schema:
type: string
- name: id
in: path
required: true
schema:
type: string
delete:
responses:
'204':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/variant'
'401':
$ref: '#/components/responses/invalid-api-key'
'404':
$ref: '#/components/responses/not-found'
'422':
$ref: '#/components/responses/delete-restriction'
summary: Delete product variant
description: |-
**Deprecation Warning**: Use [shallow version](/docs/solidus/82ccaada99139-delete-variant) instead
Deletes a product's variant.
operationId: delete-product-variant
tags:
- Variants
security:
- api-key: []
put:
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/variant'
'401':
$ref: '#/components/responses/invalid-api-key'
'404':
$ref: '#/components/responses/not-found'
'422':
$ref: '#/components/responses/unprocessable-entity'
summary: Update product variant
description: |-
**Deprecation Warning**: Use [shallow version](/docs/solidus/ce338b5f3b940-update-variant) instead
Updates a product's variant.
operationId: update-product-variant
tags:
- Variants
security:
- api-key: []
requestBody:
content:
application/json:
schema:
type: object
properties:
variant:
$ref: '#/components/schemas/variant-input'
examples:
Example with Option Value Ids:
value:
variant:
price: '11.22'
cost_price: '9'
position: 1
track_inventory: true
sku: AWSOME-1122
cost_currency: USD
option_value_ids:
- 1
- 2
Example with Option Value Text:
value:
variant:
price: '11.22'
cost_price: '9'
position: 1
track_inventory: true
sku: AWSOME-1122
cost_currency: USD
options:
name: Color
value: White
/states:
get:
responses:
Expand Down Expand Up @@ -852,65 +737,6 @@ paths:
- $ref: '#/components/parameters/q'
security:
- api-key: []
post:
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/variant'
'401':
$ref: '#/components/responses/invalid-api-key'
'422':
$ref: '#/components/responses/unprocessable-entity'
summary: Create variant
description: |-
**Deprecation Warning**: Use [nested version](/docs/solidus/681aa6cb75b1e-create-product-variant) instead
Creates a variant. Only users with `can :create, Variant` permissions can perform this action.
operationId: create-variant
tags:
- Variants
security:
- api-key: []
requestBody:
content:
application/json:
schema:
type: object
properties:
product_id:
type: integer
variant:
$ref: '#/components/schemas/variant-input'
examples:
Example with Option Value Ids:
value:
product_id: 1
variant:
price: '11.22'
cost_price: '9'
position: 1
track_inventory: true
sku: AWSOME-1122
cost_currency: USD
option_value_ids:
- 1
- 2
Example with Option Value Text:
value:
product_id: 1
variant:
price: '11.22'
cost_price: '9'
position: 1
track_inventory: true
sku: AWSOME-1122
cost_currency: USD
options:
name: Color
value: White
'/variants/{id}':
get:
responses:
Expand Down
31 changes: 0 additions & 31 deletions api/spec/requests/spree/api/variants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,6 @@ module Spree::Api
:option_type_id])
end

it "deprecates showing a variant through a nested route" do
expect(Spree::Deprecation).to receive(:warn).with(%r[deprecated.*GET api/variants]m)

get spree.api_product_variant_path(product, variant)

expect(json_response).to have_attributes(show_attributes)
end

it "can see a single variant with images" do
variant.images.create!(attachment: image("blank.jpg"))

Expand Down Expand Up @@ -323,13 +315,6 @@ module Spree::Api
expect(variant.product.variants.count).to eq(1)
end

it "deprecates creating a variant through a shallow route" do
expect(Spree::Deprecation).to receive(:warn).with(%r[deprecated.*api/products/{product_id}/variants]m)

post spree.api_variants_path, params: { variant: { sku: "12345", "product_id": product.id } }
expect(json_response).to have_attributes(new_attributes)
end

it "creates new variants with nested option values" do
option_values = create_list(:option_value, 2)
expect do
Expand Down Expand Up @@ -369,28 +354,12 @@ module Spree::Api
expect(response.status).to eq(200)
end

it "deprecates updating a variant through a nested route" do
expect(Spree::Deprecation).to receive(:warn).with(%r[deprecated.*PUT api/variants]m)

put spree.api_product_variant_path(product, variant), params: { variant: { sku: "12345" } }

expect(json_response).to have_attributes(show_attributes)
end

it "can delete a variant" do
delete spree.api_variant_path(variant)
expect(response.status).to eq(204)
expect { Spree::Variant.find(variant.id) }.to raise_error(ActiveRecord::RecordNotFound)
end

it "deprecates updating a variant through a nested route" do
expect(Spree::Deprecation).to receive(:warn).with(%r[deprecated.*DELETE api/variants]m)

delete spree.api_product_variant_path(product, variant)

expect { Spree::Variant.find(variant.id) }.to raise_error(ActiveRecord::RecordNotFound)
end

it 'variants returned contain cost price data' do
get spree.api_variants_path
expect(json_response["variants"].first.key?(:cost_price)).to eq true
Expand Down

0 comments on commit cef126d

Please sign in to comment.