Prefer "listing" to "classified_listing" in the api (#17021)

This aligns our behavior with some of the documentation examples,
while permitting the original name (don't break the api).

Fixes #16927
This commit is contained in:
Daniel Uber 2022-03-28 13:40:00 -05:00 committed by GitHub
parent 2c73745a26
commit 6c319b8eb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View file

@ -89,11 +89,12 @@ module Api
render "show", status: :ok
end
# NOTE: when doing the big listings refactoring we decided not to break
# this API. Since other code assumes the params will be under listing,
# we're copying them over.
# Since our documentation examples now use "listing", prefer that,
# but permit the legacy parameter "classified_listing",
# since this was a published API before the refactoring renamed
# ClassifiedListing to Listing in https://github.com/forem/forem/pull/7910
def listing_params
params["listing"] = params["classified_listing"]
params["listing"] ||= params["classified_listing"]
if (category_id = find_category_id(params.dig("listing", "category")))
params["listing"]["listing_category_id"] = category_id
end

View file

@ -237,7 +237,7 @@ RSpec.describe "Api::V0::Listings", type: :request do
describe "POST /api/listings" do
def post_listing(key: api_secret.secret, **params)
headers = { "api-key" => key, "content-type" => "application/json" }
post api_listings_path, params: { classified_listing: params }.to_json, headers: headers
post api_listings_path, params: { listing: params }.to_json, headers: headers
end
describe "user cannot proceed if not properly unauthorized" do
@ -429,6 +429,17 @@ RSpec.describe "Api::V0::Listings", type: :request do
expect(listing.cached_tag_list).to eq("discuss, javascript")
end
it "reads the 'classified_listing' key when listing not present" do
# this is like post_listing but uses the "classified_listing" key
key = api_secret.secret
headers = { "api-key" => key, "content-type" => "application/json" }
expect do
post api_listings_path, params: { classified_listing: listing_params }.to_json, headers: headers
expect(response).to have_http_status(:created)
end.to change(Listing, :count).by(1)
end
end
end