Skip to content

Commit 6a772bb

Browse files
committed
Use Committee middleware to validate Chat API responses
This adds a new initializer for Committee that sets up the middleware to validate the responses from the Chat API against the OpenAPI specification. It is scoped to the Chat API V0 namespace and is configured to raise errors if the response does not match the specification so we're alerted via sentry rather than it failing silently. I've set strict_reference_validation to true to ensure that our OpenAPI schema is valid. This will be enforced in the next major release of Committee. You can read more about the configuration here: https://github.com/interagent/committee?tab=readme-ov-file#committeemiddlewareresponsevalidation I've also added a test to ensure that the middleware is being applied to the endpoint. We can extract this into a shared example for future endpoints.
1 parent 9902fa0 commit 6a772bb

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

config/initializers/committee.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "committee"
2+
3+
Rails.application.config.middleware.use Committee::Middleware::ResponseValidation,
4+
schema_path: "docs/api_openapi_specification.yml",
5+
prefix: "/api/v0",
6+
strict_reference_validation: true,
7+
raise: true

spec/requests/api/v0/conversations_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,20 @@
5252
expect(response).to have_http_status(:forbidden)
5353
end
5454
end
55+
56+
context "when the response returned does not conform to the OpenAPI specification" do
57+
it "raises an error and returns the invalid params in the error message" do
58+
answer = create(:answer, question:)
59+
eager_loaded_answer = Answer.includes(:sources, :feedback).find(answer.id)
60+
answer_blueprint = AnswerBlueprint.render_as_hash(eager_loaded_answer)
61+
answer_blueprint.delete(:created_at)
62+
allow(AnswerBlueprint).to receive(:render).and_return(answer_blueprint)
63+
64+
expect { get api_v0_answer_question_path(conversation, question) }
65+
.to raise_error(Committee::InvalidResponse) do |error|
66+
expect(error.message).to match(/missing required parameters: created_at/)
67+
end
68+
end
69+
end
5570
end
5671
end

0 commit comments

Comments
 (0)