From cf4cd311b345a9f65e2c75a7a799a8474bdbc57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Fri, 31 Oct 2025 14:07:47 +0100 Subject: [PATCH] Use customer-nested params for Customer v1 API writing operations --- .../api/v1/customers_controller.rb | 9 +- spec/requests/api/v1/customers_spec.rb | 47 ++-- swagger/v1.yaml | 206 +++++++++--------- 3 files changed, 147 insertions(+), 115 deletions(-) diff --git a/app/controllers/api/v1/customers_controller.rb b/app/controllers/api/v1/customers_controller.rb index 5353c6d39f..d342f05ef8 100644 --- a/app/controllers/api/v1/customers_controller.rb +++ b/app/controllers/api/v1/customers_controller.rb @@ -88,17 +88,20 @@ module Api attributes = params.require(:customer).permit( :email, :enterprise_id, :code, :first_name, :last_name, - :billing_address, shipping_address: [ + :billing_address, + shipping_address: [ :phone, :latitude, :longitude, :first_name, :last_name, :street_address_1, :street_address_2, :postal_code, :locality, { region: [:name, :code], country: [:name, :code] }, - ] + ], + tags: [], ).to_h attributes.merge!(created_manually: true) - attributes.merge!(tag_list: params[:tags]) if params.key?(:tags) + tags = attributes.delete(:tags) + attributes.merge!(tag_list: tags) if tags.present? transform_address!(attributes, :billing_address, :bill_address) transform_address!(attributes, :shipping_address, :ship_address) diff --git a/spec/requests/api/v1/customers_spec.rb b/spec/requests/api/v1/customers_spec.rb index 9f158e1de4..0dea8f4a4e 100644 --- a/spec/requests/api/v1/customers_spec.rb +++ b/spec/requests/api/v1/customers_spec.rb @@ -173,15 +173,23 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do parameter name: :customer, in: :body, schema: { type: :object, - properties: CustomerSchema.writable_attributes, - required: CustomerSchema.required_attributes + properties: { + customer: { + type: :object, + properties: CustomerSchema.writable_attributes, + required: CustomerSchema.required_attributes + } + }, + required: ['customer'] } response "201", "Minimal customer created" do let(:customer) do { - email: "test@example.com", - enterprise_id: enterprise1.id.to_s + customer: { + email: "test@example.com", + enterprise_id: enterprise1.id.to_s + } } end schema '$ref': "#/components/schemas/customer" @@ -199,12 +207,15 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do response "201", "Example customer created" do let(:customer) do - CustomerSchema.writable_attributes.transform_values do |attribute| - attribute[:example] - end.merge( - enterprise_id: enterprise1.id, - ) + { + customer: CustomerSchema.writable_attributes.transform_values do |attribute| + attribute[:example] + end.merge( + enterprise_id: enterprise1.id, + ) + } end + schema '$ref': "#/components/schemas/customer" run_test! do @@ -223,9 +234,11 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do response "422", "Unpermitted parameter" do let(:customer) do { - email: "test@example.com", - enterprise_id: enterprise1.id.to_s, - allow_charges: true, + customer: { + email: "test@example.com", + enterprise_id: enterprise1.id.to_s, + allow_charges: true, + } } end schema '$ref': "#/components/schemas/error_response" @@ -347,8 +360,14 @@ RSpec.describe "Customers", swagger_doc: "v1.yaml", feature: :api_v1 do parameter name: :customer, in: :body, schema: { type: :object, - properties: CustomerSchema.writable_attributes, - required: CustomerSchema.required_attributes + properties: { + customer: { + type: :object, + properties: CustomerSchema.writable_attributes, + required: CustomerSchema.required_attributes + } + }, + required: ['customer'] } response "200", "Customer updated" do diff --git a/swagger/v1.yaml b/swagger/v1.yaml index a9305be001..837a15fcfa 100644 --- a/swagger/v1.yaml +++ b/swagger/v1.yaml @@ -347,57 +347,62 @@ paths: schema: type: object properties: - enterprise_id: - type: integer - example: 2 - first_name: - type: string - nullable: true - example: Alice - last_name: - type: string - nullable: true - example: Springs - code: - type: string - nullable: true - example: BUYER1 - email: - type: string - example: alice@example.com - tags: - type: array - items: - type: string - example: - - staff - - discount - billing_address: + customer: type: object - nullable: true - example: - shipping_address: - type: object - nullable: true - example: - phone: 0404 333 222 111 - latitude: -37.8173751 - longitude: 144.964803195704 - first_name: Alice - last_name: Springs - street_address_1: 1 Flinders Street - street_address_2: '' - postal_code: '1234' - locality: Melbourne - region: - code: Vic - name: Victoria - country: - code: AU - name: Australia + properties: + enterprise_id: + type: integer + example: 2 + first_name: + type: string + nullable: true + example: Alice + last_name: + type: string + nullable: true + example: Springs + code: + type: string + nullable: true + example: BUYER1 + email: + type: string + example: alice@example.com + tags: + type: array + items: + type: string + example: + - staff + - discount + billing_address: + type: object + nullable: true + example: + shipping_address: + type: object + nullable: true + example: + phone: 0404 333 222 111 + latitude: -37.8173751 + longitude: 144.964803195704 + first_name: Alice + last_name: Springs + street_address_1: 1 Flinders Street + street_address_2: '' + postal_code: '1234' + locality: Melbourne + region: + code: Vic + name: Victoria + country: + code: AU + name: Australia + required: + - enterprise_id + - email required: - - enterprise_id - - email + - customer "/api/v1/customers/{id}": get: summary: Show customer @@ -570,57 +575,62 @@ paths: schema: type: object properties: - enterprise_id: - type: integer - example: 2 - first_name: - type: string - nullable: true - example: Alice - last_name: - type: string - nullable: true - example: Springs - code: - type: string - nullable: true - example: BUYER1 - email: - type: string - example: alice@example.com - tags: - type: array - items: - type: string - example: - - staff - - discount - billing_address: + customer: type: object - nullable: true - example: - shipping_address: - type: object - nullable: true - example: - phone: 0404 333 222 111 - latitude: -37.8173751 - longitude: 144.964803195704 - first_name: Alice - last_name: Springs - street_address_1: 1 Flinders Street - street_address_2: '' - postal_code: '1234' - locality: Melbourne - region: - code: Vic - name: Victoria - country: - code: AU - name: Australia + properties: + enterprise_id: + type: integer + example: 2 + first_name: + type: string + nullable: true + example: Alice + last_name: + type: string + nullable: true + example: Springs + code: + type: string + nullable: true + example: BUYER1 + email: + type: string + example: alice@example.com + tags: + type: array + items: + type: string + example: + - staff + - discount + billing_address: + type: object + nullable: true + example: + shipping_address: + type: object + nullable: true + example: + phone: 0404 333 222 111 + latitude: -37.8173751 + longitude: 144.964803195704 + first_name: Alice + last_name: Springs + street_address_1: 1 Flinders Street + street_address_2: '' + postal_code: '1234' + locality: Melbourne + region: + code: Vic + name: Victoria + country: + code: AU + name: Australia + required: + - enterprise_id + - email required: - - enterprise_id - - email + - customer delete: summary: Delete customer tags: