Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gem "thruster", require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin Ajax possible
# gem "rack-cors"
gem "committee"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ GEM
brakeman (7.0.0)
racc
builder (3.3.0)
committee (5.5.1)
json_schema (~> 0.14, >= 0.14.3)
openapi_parser (~> 2.0)
rack (>= 1.5, < 3.2)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
crass (1.0.6)
Expand All @@ -109,6 +113,7 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.10.2)
json_schema (0.21.0)
kamal (2.5.3)
activesupport (>= 7.0)
base64 (~> 0.2)
Expand Down Expand Up @@ -156,6 +161,7 @@ GEM
racc (~> 1.4)
nokogiri (1.18.3-x86_64-linux-gnu)
racc (~> 1.4)
openapi_parser (2.2.4)
ostruct (0.6.1)
parallel (1.26.3)
parser (3.3.7.1)
Expand Down Expand Up @@ -297,6 +303,7 @@ PLATFORMS
DEPENDENCIES
bootsnap
brakeman
committee
debug
kamal
puma (>= 5.0)
Expand Down
8 changes: 8 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ class Application < Rails::Application
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

config.middleware.use Committee::Middleware::RequestValidation,
schema_path: "docs/openapi.json",
coerce_date_times: true,
params_key: "action_dispatch.request.request_parameters",
query_hash_key: "action_dispatch.request.query_parameters",
strict_reference_validation: true
config.middleware.use Committee::Middleware::ResponseValidation, schema_path: "docs/openapi.json", strict_reference_validation: true
end
end
180 changes: 180 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"openapi": "3.0.0",
"info": {
"title": "API Schema and Validation POC",
"description": "API Schema and Validation POC.",

"version": "1.0.1"
},
"host": "localhost:3000",
"schemes": ["http", "https"],
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {
"/comments": {
"get": {
"summary": "Returns a list of comments",
"description": "A simple endpoint that returns a JSON response with a list of comments.",
"responses": {
"200": {
"description": "A successful response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "number" },
"message": { "type": "string" },
"created_at": { "type": "string", "format": "date-time" },
"updated_at": { "type": "string", "format": "date-time" }
}
}
}
}
}
}
}
},
"post": {
"summary": "Creates a new comment",
"description": "A simple endpoint that creates a new comment.",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"comment": {
"type": "object",
"properties": {
"message": { "type": "string" }
},
"required": ["message"]
}
}
}
}
}
},
"responses": {
"201": {
"description": "Comment created successfully",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": { "type": "number" },
"message": { "type": "string" },
"created_at": { "type": "string", "format": "date-time" },
"updated_at": { "type": "string", "format": "date-time" }
}
}
}
}
}
}
}
},
"/comments/{comment_id}": {
"get": {
"summary": "Returns a specific comment",
"description": "A simple endpoint that returns a JSON response with a specific comment.",
"parameters": [
{
"name": "comment_id",
"in": "path",
"required": true,
"description": "The ID of the comment",
"schema": { "type": "number" }
}
],
"responses": {
"200": {
"description": "A successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": { "type": "number" },
"message": { "type": "string" }
}
}
}
}
}
}
},
"put": {
"summary": "Updates a specific comment",
"description": "A simple endpoint that updates a specific comment.",
"parameters": [
{
"name": "comment_id",
"in": "path",
"required": true,
"description": "The ID of the comment",
"schema": { "type": "string" }
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"comment": {
"type": "object",
"properties": {
"message": { "type": "string" }
},
"required": ["message"]
}
}
}
}
}
},
"responses": {
"200": {
"description": "Comment updated successfully",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": { "type": "number" },
"message": { "type": "string" }
}
}
}
}
}
}
},
"delete": {
"summary": "Deletes a specific comment",
"description": "A simple endpoint that deletes a specific comment.",
"parameters": [
{
"name": "comment_id",
"in": "path",
"required": true,
"description": "The ID of the comment",
"schema": { "type": "string" }
}
],
"responses": {
"204": {
"description": "Comment deleted successfully"
}
}
}
}
}
}