Skip to content

Commit 8875c5c

Browse files
committed
make it so that payloads are symbolized by default and headers are also normalized by default as well
1 parent b229697 commit 8875c5c

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

lib/hooks/app/api.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ def validate_request(payload, headers, endpoint_config)
8989
end
9090

9191
# Parse request payload
92-
def parse_payload(raw_body, headers)
93-
content_type = headers["Content-Type"] || headers["CONTENT_TYPE"]
92+
def parse_payload(raw_body, headers, symbolize: true)
93+
content_type = headers["Content-Type"] || headers["CONTENT_TYPE"] || headers["content-type"] || headers["HTTP_CONTENT_TYPE"]
9494

9595
# Try to parse as JSON if content type suggests it or if it looks like JSON
9696
if content_type&.include?("application/json") || (raw_body.strip.start_with?("{", "[") rescue false)
9797
begin
98-
return JSON.parse(raw_body)
98+
parsed_payload = JSON.parse(raw_body)
99+
parsed_payload = parsed_payload.transform_keys(&:to_sym) if symbolize && parsed_payload.is_a?(Hash)
100+
return parsed_payload
99101
rescue JSON::ParserError
100102
# If JSON parsing fails, return raw body
101103
end
@@ -197,12 +199,15 @@ def determine_error_code(exception)
197199
log.info "validating request (id: #{request_id}, handler: #{handler_class_name})"
198200
validate_request(raw_body, headers, endpoint_config) if endpoint_config[:request_validator]
199201

200-
# Parse payload
201-
payload = parse_payload(raw_body, headers)
202+
# Parse payload (symbolize_payload is true by default)
203+
payload = parse_payload(raw_body, headers, symbolize: config[:symbolize_payload])
202204

203205
# Load and instantiate handler
204206
handler = load_handler(handler_class_name, config[:handler_dir])
205207

208+
# Normalize the headers based on the endpoint configuration (normalization is the default)
209+
headers = Hooks::Plugins::Utils::Normalize.headers(headers) if config[:normalize_headers]
210+
206211
# Call handler
207212
response = handler.call(
208213
payload:,

lib/hooks/core/config_loader.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class ConfigLoader
1818
environment: "production",
1919
production: true,
2020
endpoints_dir: "./config/endpoints",
21-
use_catchall_route: false
21+
use_catchall_route: false,
22+
symbolize_payload: true,
23+
normalize_headers: true
2224
}.freeze
2325

2426
# Load and merge configuration from various sources

lib/hooks/core/config_validator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ValidationError < StandardError; end
2121
optional(:environment).filled(:string, included_in?: %w[development production])
2222
optional(:endpoints_dir).filled(:string)
2323
optional(:use_catchall_route).filled(:bool)
24+
optional(:symbolize_payload).filled(:bool)
25+
optional(:normalize_headers).filled(:bool)
2426
end
2527

2628
# Endpoint configuration schema

spec/acceptance/handlers/team1_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def call(payload:, headers:, config:)
1414

1515
# Process the payload based on type
1616
if payload.is_a?(Hash)
17-
event_type = payload["event_type"] || "unknown"
17+
event_type = payload[:event_type] || "unknown"
1818

1919
case event_type
2020
when "deployment"

0 commit comments

Comments
 (0)