From 2e59812bc1570aff33db9c6373719f95cedb8c8d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 12 Dec 2021 20:56:38 +0000 Subject: [PATCH] Refactor to make more use of schema refs, and regenerate swagger file --- spec/requests/api/v1/customers_spec.rb | 20 +- spec/swagger_helper.rb | 5 +- swagger/v1/swagger.yaml | 629 ++++++------------------- 3 files changed, 163 insertions(+), 491 deletions(-) diff --git a/spec/requests/api/v1/customers_spec.rb b/spec/requests/api/v1/customers_spec.rb index 58c9f7e60a..b84416cf04 100644 --- a/spec/requests/api/v1/customers_spec.rb +++ b/spec/requests/api/v1/customers_spec.rb @@ -19,7 +19,7 @@ describe "Customers", type: :request do response "200", "Customers list" do param(:enterprise_id) { enterprise1.id } - schema CustomerSchema.collection(require_all: true) + schema "$ref": "#/components/schemas/resources/customers_collection" run_test! end @@ -73,14 +73,14 @@ describe "Customers", type: :request do enterprise_id: enterprise1.id.to_s } end - schema CustomerSchema.schema(require_all: true) + schema "$ref": "#/components/schemas/resources/customer" run_test! end response "422", "Unprocessable entity" do param(:customer) { {} } - schema ErrorsSchema.schema + schema "$ref": "#/components/schemas/error_response" run_test! end @@ -95,14 +95,14 @@ describe "Customers", type: :request do response "200", "Customer" do param(:id) { customer1.id } - schema CustomerSchema.schema(require_all: true) + schema "$ref": "#/components/schemas/resources/customer" run_test! end response "404", "Not found" do param(:id) { 0 } - schema ErrorsSchema.schema + schema "$ref": "#/components/schemas/error_response" run_test! do expect(json_error_detail).to eq "The resource you were looking for could not be found." @@ -114,7 +114,7 @@ describe "Customers", type: :request do response "401", "Unauthorized" do param(:id) { customer1.id } - schema ErrorsSchema.schema + schema "$ref": "#/components/schemas/error_response" run_test! do expect(json_error_detail).to eq "You are not authorized to perform that action." @@ -161,7 +161,7 @@ describe "Customers", type: :request do enterprise_id: enterprise1.id.to_s } end - schema CustomerSchema.schema(require_all: true) + schema "$ref": "#/components/schemas/resources/customer" run_test! end @@ -169,7 +169,7 @@ describe "Customers", type: :request do response "422", "Unprocessable entity" do param(:id) { customer1.id } param(:customer) { {} } - schema ErrorsSchema.schema + schema "$ref": "#/components/schemas/error_response" run_test! end @@ -182,7 +182,7 @@ describe "Customers", type: :request do response "200", "Customer deleted" do param(:id) { customer1.id } - schema CustomerSchema.schema(require_all: true) + schema "$ref": "#/components/schemas/resources/customer" run_test! end @@ -197,7 +197,7 @@ describe "Customers", type: :request do response "200", "Customers list" do param(:enterprise_id) { enterprise1.id } - schema CustomerSchema.collection(require_all: true) + schema "$ref": "#/components/schemas/resources/customers_collection" run_test! end diff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rb index d8d5094673..46e7a7760d 100644 --- a/spec/swagger_helper.rb +++ b/spec/swagger_helper.rb @@ -27,7 +27,10 @@ RSpec.configure do |config| components: { schemas: { error_response: ErrorsSchema.schema, - customer: CustomerSchema.schema + resources: { + customer: CustomerSchema.schema(require_all: true), + customers_collection: CustomerSchema.collection(require_all: true) + } }, securitySchemas: { api_key_header: { diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml index 652fef8fb3..b18bed201d 100644 --- a/swagger/v1/swagger.yaml +++ b/swagger/v1/swagger.yaml @@ -23,53 +23,150 @@ components: - detail required: - errors - customer: - type: object - properties: - data: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: + resources: + customer: + type: object + properties: + data: + type: object + properties: + id: + type: string + example: '1' + type: + type: string + example: customer + attributes: + type: object + properties: + id: + type: integer + example: 1 + 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 + required: + - id + - enterprise_id + - first_name + - last_name + - code + - email + relationships: + type: object + properties: + enterprise: + "$ref": "#/components/schemas/relationship" + meta: + type: object + links: + type: object + required: + - data + customers_collection: + type: object + properties: + data: + type: array + items: type: object properties: id: - type: integer - example: 1 - enterprise_id: - type: integer - example: 2 - first_name: type: string - nullable: true - example: Alice - last_name: + example: '1' + type: type: string - nullable: true - example: Springs - code: - type: string - nullable: true - example: BUYER1 - email: - type: string - example: alice@example.com - required: - - enterprise_id - - email - relationships: - type: object - meta: - type: object - links: - type: object - required: - - data + example: customer + attributes: + type: object + properties: + id: + type: integer + example: 1 + 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 + required: + - id + - enterprise_id + - first_name + - last_name + - code + - email + relationships: + type: object + properties: + enterprise: + "$ref": "#/components/schemas/relationship" + meta: + type: object + properties: + pagination: + type: object + properties: + results: + type: integer + example: 250 + pages: + type: integer + example: 5 + page: + type: integer + example: 2 + per_page: + type: integer + example: 50 + required: + - pagination + links: + type: object + properties: + self: + type: string + first: + type: string + prev: + type: string + nullable: true + next: + type: string + nullable: true + last: + type: string + required: + - data + - meta + - links securitySchemas: api_key_header: type: apiKey @@ -103,91 +200,7 @@ paths: content: application/json: schema: - type: object - properties: - data: - type: array - items: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: - type: object - properties: - id: - type: integer - example: 1 - 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 - required: - - id - - enterprise_id - - first_name - - last_name - - code - - email - relationships: - type: object - meta: - type: object - properties: - pagination: - type: object - properties: - results: - type: integer - example: 250 - pages: - type: integer - example: 5 - page: - type: integer - example: 2 - per_page: - type: integer - example: 50 - required: - - pagination - links: - type: object - properties: - self: - type: string - first: - type: string - prev: - type: string - nullable: true - next: - type: string - nullable: true - last: - type: string - required: - - data - - meta - - links + "$ref": "#/components/schemas/resources/customers_collection" post: summary: Create customer tags: @@ -199,78 +212,13 @@ paths: content: application/json: schema: - type: object - properties: - data: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: - type: object - properties: - id: - type: integer - example: 1 - 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 - required: - - id - - enterprise_id - - first_name - - last_name - - code - - email - relationships: - type: object - meta: - type: object - links: - type: object - required: - - data + "$ref": "#/components/schemas/resources/customer" '422': description: Unprocessable entity content: application/json: schema: - type: object - properties: - errors: - type: array - items: - type: object - properties: - title: - type: string - detail: - type: string - source: - type: object - required: - - detail - required: - - errors + "$ref": "#/components/schemas/error_response" requestBody: content: application/json: @@ -315,100 +263,19 @@ paths: content: application/json: schema: - type: object - properties: - data: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: - type: object - properties: - id: - type: integer - example: 1 - 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 - required: - - id - - enterprise_id - - first_name - - last_name - - code - - email - relationships: - type: object - meta: - type: object - links: - type: object - required: - - data + "$ref": "#/components/schemas/resources/customer" '404': description: Not found content: application/json: schema: - type: object - properties: - errors: - type: array - items: - type: object - properties: - title: - type: string - detail: - type: string - source: - type: object - required: - - detail - required: - - errors + "$ref": "#/components/schemas/error_response" '401': description: Unauthorized content: application/json: schema: - type: object - properties: - errors: - type: array - items: - type: object - properties: - title: - type: string - detail: - type: string - source: - type: object - required: - - detail - required: - - errors + "$ref": "#/components/schemas/error_response" put: summary: Update customer tags: @@ -425,78 +292,13 @@ paths: content: application/json: schema: - type: object - properties: - data: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: - type: object - properties: - id: - type: integer - example: 1 - 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 - required: - - id - - enterprise_id - - first_name - - last_name - - code - - email - relationships: - type: object - meta: - type: object - links: - type: object - required: - - data + "$ref": "#/components/schemas/resources/customer" '422': description: Unprocessable entity content: application/json: schema: - type: object - properties: - errors: - type: array - items: - type: object - properties: - title: - type: string - detail: - type: string - source: - type: object - required: - - detail - required: - - errors + "$ref": "#/components/schemas/error_response" requestBody: content: application/json: @@ -543,56 +345,7 @@ paths: content: application/json: schema: - type: object - properties: - data: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: - type: object - properties: - id: - type: integer - example: 1 - 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 - required: - - id - - enterprise_id - - first_name - - last_name - - code - - email - relationships: - type: object - meta: - type: object - links: - type: object - required: - - data + "$ref": "#/components/schemas/resources/customer" "/api/v1/enterprises/{enterprise_id}/customers": get: summary: List customers of an enterprise @@ -611,90 +364,6 @@ paths: content: application/json: schema: - type: object - properties: - data: - type: array - items: - type: object - properties: - id: - type: string - example: '1' - type: - type: string - example: customer - attributes: - type: object - properties: - id: - type: integer - example: 1 - 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 - required: - - id - - enterprise_id - - first_name - - last_name - - code - - email - relationships: - type: object - meta: - type: object - properties: - pagination: - type: object - properties: - results: - type: integer - example: 250 - pages: - type: integer - example: 5 - page: - type: integer - example: 2 - per_page: - type: integer - example: 50 - required: - - pagination - links: - type: object - properties: - self: - type: string - first: - type: string - prev: - type: string - nullable: true - next: - type: string - nullable: true - last: - type: string - required: - - data - - meta - - links + "$ref": "#/components/schemas/resources/customers_collection" servers: - url: "/"