Skip to content

Commit

Permalink
[Extract to PR?] Remove deprecated api option values 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/option_values/:id
- DELETE api/option_values/:id
- GET api/option_values/:id

And the nested route when the option type is mandatory:

- POST api/option_types/{option_type_id}/option_values

For the index routes instead, we will have both:

- GET api/option_types/{option_type_id}/option_values
- GET api/option_values
  • Loading branch information
kennyadsl committed Mar 21, 2023
1 parent 37f9408 commit bf2793a
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 183 deletions.
21 changes: 0 additions & 21 deletions api/app/controllers/spree/api/option_values_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,11 @@ def index
end

def show
warn_if_nested_member_route

@option_value = scope.find(params[:id])
respond_with(@option_value)
end

def create
Spree::Deprecation.warn <<~MSG unless request.path.include?('/option_types/')
This route is deprecated, as it'll be no longer possible to create an
option_value without an associated option_type. Please, use instead:
POST api/option_types/{option_type_id}/option_values
MSG

authorize! :create, Spree::OptionValue
@option_value = scope.new(option_value_params)
if @option_value.save
Expand All @@ -37,8 +28,6 @@ def create
end

def update
warn_if_nested_member_route

@option_value = scope.accessible_by(current_ability, :update).find(params[:id])
if @option_value.update(option_value_params)
render :show
Expand All @@ -48,8 +37,6 @@ def update
end

def destroy
warn_if_nested_member_route

@option_value = scope.accessible_by(current_ability, :destroy).find(params[:id])
@option_value.destroy
render plain: nil, status: 204
Expand All @@ -68,14 +55,6 @@ def scope
def option_value_params
params.require(:option_value).permit(permitted_option_value_attributes)
end

def warn_if_nested_member_route
Spree::Deprecation.warn <<~MSG if request.path.include?('/option_types/')
This route is deprecated. Use shallow version instead:
#{request.method.upcase} api/option_values/:id
MSG
end
end
end
end
7 changes: 2 additions & 5 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@
end

resources :option_types do
# TODO: Use shallow option on Solidus v4.0
resources :option_values
resources :option_values, shallow: true
end
# TODO: Use only: :index on Solidus v4.0 and use shallow option on the nested routes
# within option_types
resources :option_values
resources :option_values, only: :index

get '/orders/mine', to: 'orders#mine', as: 'my_orders'
get "/orders/current", to: "orders#current", as: "current_order"
Expand Down
125 changes: 0 additions & 125 deletions api/openapi/solidus-api.oas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2541,38 +2541,6 @@ paths:
- Option values
security:
- api-key: []
post:
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/option-value'
'401':
$ref: '#/components/responses/invalid-api-key'
'422':
$ref: '#/components/responses/unprocessable-entity'
summary: Create option value
description: |-
**Deprecation Warning**: Use [nested version](/docs/solidus/810154673c613-create-option-type-value) instead
Creates an option value.
Only users with the `create` permission on `Spree::OptionValue` can perform this action.
operationId: create-option-value
tags:
- Option values
security:
- api-key: []
requestBody:
content:
application/json:
schema:
type: object
properties:
option_value:
$ref: '#/components/schemas/option-value-input'
'/option_values/{id}':
get:
responses:
Expand Down Expand Up @@ -2715,99 +2683,6 @@ paths:
properties:
option_value:
$ref: '#/components/schemas/option-value-input'
'/option_types/{option_type_id}/option_values/{id}':
get:
responses:
'200':
description: ''
content:
application/json:
schema: {}
'401':
$ref: '#/components/responses/invalid-api-key'
'404':
$ref: '#/components/responses/not-found'
summary: Get option type value
description: |-
**Deprecation Warning**: Use [shallow version](/docs/solidus/cbbc403ed08a3-get-option-value) instead
Retrieves an option type's value.
operationId: get-option-type-value
tags:
- Option values
security:
- api-key: []
parameters:
- name: option_type_id
in: path
required: true
schema:
type: string
description: 'The ID of the Spree::OptionType'
- name: id
in: path
required: true
schema:
type: string
description: The ID of the OptionValue
delete:
responses:
'204':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/option-value'
'401':
$ref: '#/components/responses/invalid-api-key'
'404':
$ref: '#/components/responses/not-found'
'422':
$ref: '#/components/responses/delete-restriction'
summary: Delete option type value
description: |-
**Deprecation Warning**: Use [shallow version](/docs/solidus/0742e63219b1f-delete-option-value) instead
Deletes an option type's value.
operationId: delete-option-type-value
tags:
- Option values
security:
- api-key: []
patch:
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/option-value'
'401':
$ref: '#/components/responses/invalid-api-key'
'404':
$ref: '#/components/responses/not-found'
'422':
$ref: '#/components/responses/unprocessable-entity'
summary: Update option type value
description: |-
**Deprecation Warning**: Use [shallow version](/docs/solidus/b43f889175ebb-update-option-value) instead
Updates an option type's value.
Only users with the `update` permission on the option value can perform this action.
operationId: update-option-type-value
tags:
- Option values
security:
- api-key: []
requestBody:
content:
application/json:
schema:
type: object
properties:
option_value:
$ref: '#/components/schemas/option-value-input'
'/products/{product_id}/product_properties':
get:
responses:
Expand Down
32 changes: 0 additions & 32 deletions api/spec/requests/spree/api/option_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ module Spree::Api
expect(json_response).to have_attributes(attributes)
end

it "deprecates listing a single option value through a nested route" do
expect(Spree::Deprecation).to receive(:warn).with(%r[deprecated.*GET api/option_values/:id]m)

get spree.api_option_type_option_value_path(option_type, option_value)
end

it "cannot create a new option value" do
post spree.api_option_type_option_values_path(option_type), params: {
option_value: {
Expand Down Expand Up @@ -116,26 +110,6 @@ module Spree::Api
expect(option_value.name).to eq("Option Value")
end

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

put spree.api_option_type_option_value_path(option_type, option_value), params: { option_value: {
name: "Option Value"
} }
end

it "can create but deprecates creating an option value without option type" do
expect(Spree::Deprecation).to receive(:warn).with(/deprecated/).at_least(:once)

post spree.api_option_values_path, params: { option_value: {
name: "Option Value",
presentation: 'option value'
} }
expect(response.status).to eq(201)

expect(json_response).to have_attributes(attributes)
end

it "permits the correct attributes" do
expect_any_instance_of(Spree::Api::OptionValuesController).to receive(:permitted_option_value_attributes)
put spree.api_option_value_path(option_value), params: { option_value: { name: "" } }
Expand All @@ -152,12 +126,6 @@ module Spree::Api
delete spree.api_option_value_path(option_value)
expect(response.status).to eq(204)
end

it "deprecates deleting an option value through a nested route" do
expect(Spree::Deprecation).to receive(:warn).with(%r[deprecated.*DELETE api/option_values/:id]m)

delete spree.api_option_type_option_value_path(option_type, option_value)
end
end
end
end
Expand Down

0 comments on commit bf2793a

Please sign in to comment.